0% found this document useful (0 votes)
6 views49 pages

Dsa L8

The document provides an overview of tree data structures, detailing their properties, types, and applications. It explains basic terminology, operations, and specific types of trees such as binary trees and binary search trees, along with their characteristics and time complexities. The content is aimed at understanding the hierarchical representation of data and efficient operations related to trees.

Uploaded by

seunadepoju64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views49 pages

Dsa L8

The document provides an overview of tree data structures, detailing their properties, types, and applications. It explains basic terminology, operations, and specific types of trees such as binary trees and binary search trees, along with their characteristics and time complexities. The content is aimed at understanding the hierarchical representation of data and efficient operations related to trees.

Uploaded by

seunadepoju64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Dr.

Mohammed Al-Hubaishi
1

Data Structures & Algorithms

Tree Data Structure

DR . MOHAMMED AL-HUBAISHI
Data Structures: problems

2 Dr. Mohammed Al-Hubaishi


Data Structures: cons and pros

3 Dr. Mohammed Al-Hubaishi


Tree Data Structure

Time complexity
Search-Remove-Add : O(log n)

A tree data structure can be defined recursively as a collection of nodes, where each
node is a data structure consisting of a value and a list of references to nodes. The
start of the tree is the "root node" and the reference nodes are the "children".

4 Dr. Mohammed Al-Hubaishi


Introduction to Trees

• A tree is a hierarchical data structure


• Consists of nodes connected by edges
• Has a root node and multiple child nodes
• Used to represent hierarchical relationships
• Common applications: file systems, organization
charts, XML/HTML parsing

5 Dr. Mohammed Al-Hubaishi


Introduction to Trees

What is a Tree?
• A non-linear data structure.
• Represents hierarchical data.
• Consists of nodes connected by edges.
Why Trees?
• Efficient for many operations like insertion, deletion, and
searching.
• Used in various applications like file systems,
organization charts, etc.

6 Dr. Mohammed Al-Hubaishi


Properties of Trees

•Acyclic: No cycles.
•Connected: There is exactly one path between any two nodes.
•Rooted Tree: Tree with a designated root node.

7 Dr. Mohammed Al-Hubaishi


Basic Tree Terminology

• Node: Basic unit containing data and references


• Root: Topmost node of the tree
• Parent: Node with child nodes
• Child: Node connected to parent node
• Leaf: Node with no children
• internal node: a node with a child
• Siblings: Nodes with same parent
• Height: Length of path from root to deepest node
• depth of a node: the length of the path from the node to the root
depth (or height!) of the tree: the maximum node depth

8 Dr. Mohammed Al-Hubaishi


Tree example

► Root
► Parents
► Children
► Leaves
► Internal nodes
► Siblings
► Ancestor
► depth

9 Dr. Mohammed Al-Hubaishi


Tree : levels

https://onlinegdb.com/H1uUt5-cq

1. First group {B,E,F}: These are siblings as they share the same parent 'A‘
2. Second group {C,D}: These are siblings as they share parent 'B‘
3. Third group {G,H,I}: These are siblings sharing parent 'F'
The siblings are grouped in curly braces {{B,E,F}, {C,D}, {G,H,I}}

Height(A) = 2:
•The height is calculated as the number of edges from root to the deepest leaf
•From A → B → C/D or A → F → G/H/I is 2 edges

Dr. Mohammed Al-Hubaishi


10
Types of Trees

Common Tree Types:


• Binary Trees
• Binary Search Trees
Special Characteristics:
• AVL Trees • Different balancing properties
• Red-Black Trees • Varying number of children
• B-Trees • Specific ordering rules
• Different applications

11 Dr. Mohammed Al-Hubaishi


Edges of Tree example

https://onlinegdb.com/arO
Edges = Node numbers -1
OF1TiN4

12
Tree: outdegree, Indegree, and total degree

13 Dr. Mohammed Al-Hubaishi


Tree: Parenthetical notation

14 Dr. Mohammed Al-Hubaishi


Is this binary Tree ?

15 Dr. Mohammed Al-Hubaishi


Binary Trees Overview

• Each node has at most two children


• Children are referred to as left and right

16 Dr. Mohammed Al-Hubaishi


Full (Strictly) Binary Tree

If every node has zero or two children

17
Complete Binary Tree

•All levels are completely filled except the last level.


•All nodes are as left as possible in the last level.

18
Perfect Binary Tree

Perfect Binary Tree (All levels are completely filled)


1. Every node has two children.
2. All leaves are at the same level.

19
Balanced Binary Tree

Balanced Binary Tree


The height of the tree = O(log₂ n).

20
Balanced Binary Tree

-1 OR 0 OR 1

21
Degenerate Tree
A degenerate (or pathological) Tree
• Every parent node has only one child either left or right.
• Such trees are performance-wise same as linked list.

22
Binary Tree Properties

• Maximum nodes at level L = 2^L


• Maximum nodes in tree of height h = 2^(h+1) - 1
• Minimum height = floor(log2n)
• For n nodes, maximum height = n-1
• Perfect binary tree has 2^h leaves

23 Dr. Mohammed Al-Hubaishi


Binary Tree

24
Binary Tree Implementation in C++

struct Node {
int item;
Node* left;
Node* right;

Node(int val) {
item = val;
left = right = nullptr;
}
};

25 Dr. Mohammed Al-Hubaishi


Binary Tree Implementation in C++

26 Dr. Mohammed Al-Hubaishi


Basic Operations on Binary Trees

• Insertion of a node
• Deletion of a node
• Searching for a node
• Traversing the tree
• Finding height/depth
• Counting nodes
• Finding maximum/minimum values

27 Dr. Mohammed Al-Hubaishi


Binary Search Trees (BST)

• Special type of binary tree


• Left subtree contains nodes with smaller values
• Right subtree contains nodes with larger values
• No duplicate values allowed
• Enables efficient searching, insertion, and deletion
• Average time complexity: O(log n)

28 Dr. Mohammed Al-Hubaishi


Binary Search: key = 22
best case

29 Dr. Mohammed Al-Hubaishi


Binary Search: key =

30
Binary Search: key = 60

31
Dr. Mohammed Al-Hubaishi
Binary Search: key = 45
Not fund

https://onlinegdb.com/Bas7vmKCP

32 Dr. Mohammed Al-Hubaishi


Binary Search: using loop

Low = l
High = h
Mid = m

m = (2 l + h -l)/2
m = l + (h - l)

33 Dr. Mohammed Al-Hubaishi


Binary Search: using recurrence

Low = l
High = h
Mid = m

34 Dr. Mohammed Al-Hubaishi


Binary Search : Time complexity

• We started from index 7

• we have reduced the number of


comparisons
• O( log n)
• Using the equation in the array
for Binary Search, we do not
need more space from memory.

35 Dr. Mohammed Al-Hubaishi


Binary Search : Time complexity

36 Dr. Mohammed Al-Hubaishi


Add item into BST

37
BST Operations

38
adding a sorted items into BST

39
What is the Path for 35?

40
Find Path

41
when we delete the root , we can select the minimum from right
subtree

42
Delete: Find Successor

Right and left

43
Find successor of a Node

44
Find successor of a Node

45
Find successor of a Node

46
Find successor of a Node

47
Book References 48

Dr. Mohammed Al-Hubaishi


49

Dr. Mohammed Al-Hubaishi

You might also like