Dsa L8
Dsa L8
Mohammed Al-Hubaishi
1
DR . MOHAMMED AL-HUBAISHI
Data Structures: problems
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".
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.
•Acyclic: No cycles.
•Connected: There is exactly one path between any two nodes.
•Rooted Tree: Tree with a designated root node.
► Root
► Parents
► Children
► Leaves
► Internal nodes
► Siblings
► Ancestor
► depth
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
https://onlinegdb.com/arO
Edges = Node numbers -1
OF1TiN4
12
Tree: outdegree, Indegree, and total degree
17
Complete Binary Tree
18
Perfect Binary Tree
19
Balanced Binary Tree
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
24
Binary Tree Implementation in C++
struct Node {
int item;
Node* left;
Node* right;
Node(int val) {
item = val;
left = right = nullptr;
}
};
• Insertion of a node
• Deletion of a node
• Searching for a node
• Traversing the tree
• Finding height/depth
• Counting nodes
• Finding maximum/minimum values
30
Binary Search: key = 60
31
Dr. Mohammed Al-Hubaishi
Binary Search: key = 45
Not fund
https://onlinegdb.com/Bas7vmKCP
Low = l
High = h
Mid = m
m = (2 l + h -l)/2
m = l + (h - l)
Low = l
High = h
Mid = m
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
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