DS Unit-5 Trees
DS Unit-5 Trees
SYLLABUS
UNIT 1: Data Structures and Algorithms Basics (8 Hours)
Introduction: Basic terminologies, elementary data organizations, data structure operations;
abstract data types (ADT) and their characteristics.
Algorithms: Definition, characteristics, analysis of an algorithm, asymptotic notations, time
and space trade-offs.
Array ADT: Definition, operations and representations – row-major and column- major.
Trees
Course Outcomes
7 8
9 10 10
Object
Data Structures Using C++
...
Introduction Analysis of Algorithms Index Array Throwable Rectangle String
13 14
15 16
17 18
3. Parent 4. Child
In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. The node which has a link from its parent node is called as child node.
In simple words, the node which has branch from it to any other node is called as parent node. In a tree, any parent node can have any number of child nodes.
Parent node can also be defined as "The node which has child / children".
In a tree, all the nodes except root are child nodes.
19 20
21 22
B 2 C 0 D 0 E 3
F 1 G 0 H 1 I 0 J 1
K 0 L 0 M 0
23 24
Level 0
Level 1
height=3
Level 2
Level 3
25 26
27 28
29 30
31 32
B C B C
D E F
D E F G H
A is the root
B and C are A’s children
A is B’s parent G This is a general tree because C has three child nodes
B & C are the root of two subtrees
D, E, and G are leaves
33 34
D E F G
D E F
39 40
B C
PRE-ORDER TRAVERSAL IN-ORDER TRAVERSAL
D E F
Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
POST-ORDER TRAVERSAL
41 42
B C B C
D E F D E F
Preorder: A Preorder: AB
(visit each node as your reach it) G (visit each node as your reach it) G
43 44
B C B C
D E F D E F
45 46
B C B C
D E F D E F
47 48
B C B C
D E F D E F
49 50
B C B C
D E F D E F
Postorder: Postorder: D
G G
51 52
B C B C
D E F D E F
53 54
B C B C
D E F D E F
55 56
B C B C
D E F D E F
57 58
B C B C
D E F D E F
59 60
B C B C
D E F D E F
G G
In Order: In Order:
61 62
B C B C
D E F D E F
G G
In Order: D In Order: DB
63 64
B C B C
D E F D E F
G G
In Order: DBE In Order: DBEA
65 66
B C B C
D E F D E F
G G
In Order: DBEA In Order: DBEAF
67 68
B C B C
D E F D E F
G G
In Order: DBEAFG In Order: DBEAFGC
69 70
A A
B C B C
D E
D E F G
F
Preorder: ABDECFG
Postorder: DEBGFCA Preorder: ABDFHIECG
G
In Order: DBEAFGC Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
71 72
A A
B C B C
D E D E
G G
F F
Preorder: A Preorder: AB
H I H I
73 74
B C B C
D E D E
G G
F F
Preorder: ABD Preorder: ABDF
H I H I
75 76
B C B C
D E D E
G G
F F
Preorder: ABDFH Preorder: ABDFHI
H I H I
77 78
B C B C
D E D E
G G
F F
Preorder: ABDFHIE Preorder: ABDFHIEC
H I H I
79 80
B C B C
D E D E
G G
F F
Preorder: ABDFHIECG
Postorder:
H I H I
81 82
B C B C
D E D E
G G
F F
Postorder: Postorder:
H I H I
83 84
B C B C
D E D E
G G
F F
Postorder: Postorder: H
H I H I
85 86
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
Postorder: HIFDEBGCA
H I H I In Order:
95 96
B C B C
D E D E
G G
F F
H I In Order: H I In Order: D
97 98
B C B C
D E D E
G G
F F
H I In Order: D H I In Order: DH
99 100
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
B C B C
D E D E
G G
F F
B C
Preorder traversal (NLR)
D E 16,10,5,14,25,18,90
G Inorder traversal(LNR)
F 5,10,14,16,18,25,90
Postorder traversal(LRN)
5,14,10,18,90,25,16
H I In Order: DHFIBEACG
107 108
B C
Preorder traversal (NLR) D E
P,H,A,D,K,M,S,R,T,W
G
Inorder traversal(LNR)
A,D,H,K,M,P,R,T,S,W F
Postorder traversal(LRN) Preorder: ABDFHIECG
D,A,M,K,H,T,R,W,S,P
Postorder: HIFDEBGCA
H I
In Order: DHFIBEACG
109 110
113 114
115 116
Tree Program
// Main function to demonstrate tree insertion A case study (Application) :: Expression Tree
// Function to print the tree in-order (for verification) int main() {
void inOrderTraversal(struct TreeNode* root) { struct TreeNode* root = NULL; *
if (root != NULL) {
inOrderTraversal(root->left); // Insert values into the tree
printf("%d ", root->value); root = insert(root, 45); + -
Represents the expression:
inOrderTraversal(root->right); root = insert(root, 30); (a + b) * (c – (d + e))
} root = insert(root, 97);
} root = insert(root, 12);
root = insert(root, 43); a b c +
root = insert(root, 69);
root = insert(root, 83);
d e
// Print the in-order traversal of the tree
printf("In-order traversal of the binary tree:\n");
inOrderTraversal(root); Preorder traversal :: * + a b - c + d e
printf("\n");
Postorder traversal :: a b + c d e + - *
return 0;
} 117 118
Right threaded binary tree (one way threading) Left threaded binary tree (one way threading)
A
A
B C
B C
X F G X D E X
X F G X D X E X
H X
X H
121 122
Fully threaded binary tree (two way threading) A Threaded Binary Tree
root
A A
dangling
B C
B C
X F G D E X
dangling F G
D E
H
inorder traversal:
H, D, I, B, E, A, F, C, G
H I
123 124
Heap Sort
Max Heap Example Min heap example
A max heap provides an efficient way to retrieve the maximum elements from an array.
The root of the heap is always the largest element since all child nodes must have smaller values.
19 1
Definition
• Max Heap
12 16 4 16
– Store data in ascending order
– Has property of
12 19
A[Parent(i)] ≥ A[i] (Parent Node is ≥ Child Nodes) 4 7
7
1
• Min Heap
– Store data in descending order
19 12 16 1 4 7 1 4 16 7 12 19
– Has property of
Array A
A[Parent(i)] ≤ A[i] (Parent Node is ≤ Child Nodes) Array A
129 130
131 132
133 134
12 14
1 4 7 1 4 7 17
8 14 8 12 Insert 17
19
Blue node does not have heap property Blue node has heap property
12 17
swap
• This is sometimes called sifting up
1 4 7 16
1 12 19 1 12 7
4 7
16 19
swap swap
12 19 12 16
1 12 19
1 4 7 1 4 7
137 138
HEAPIFY() HEAPIFY() 7
swap 12 1 4
7 swap swap
4 HEAPIFY() swap
HEAPIFY()
7 1 4 1
7 4 7 4
12 4
12 7
1
1
1
Sorted:
Array A
12 Sorted: Sorted:
7 4 1 12 16 19 Array A Array A
Sorted:
Array A 1 7 4 7 4 1 7 12 16 19
12 16 19
4 12 7 1 16 19
141 142
Example of Heap Sort Example of Heap Sort (Max Heap) (Alternate Method)
Algorithms Steps:
swap 1 4
19
Step1: Make the max heap tree as per given array
HEAPIFY() Step2: Write the tree array as level wise sorted (array A)
4 1
12 16 Step3: Swap last node value with first node value (array A)
Step4: Delete last node from (array A) and store in sorted
array in reverse order.
Sorted: Sorted: 1 Step5: Repeat steps 1-4 till no element in (array A)
Array A Array A 4 7
1 4 7 12 16 19
1 4 7 12 16 19
Sorted:
Array A
Sorted: 19 12 16 1 4 7 19
1
1 4 7 12 16 19
143 144
1 1 1
4
1 4
Sorted:
Sorted: Array A Sorted:
Array A Array A
4 12 7 1 16 19
Sorted: 12 4 7 1 19
16 12 7 1 4 16 19 12 16
19
145 146
swap 1 4
4 7 4 1
HEAPIFY()
4 1
Sorted: Sorted:
Sorted: Array A Array A
Sorted: Array A
Array A 1 4 7 12 16 19
7 4 1 7 12 16 19 4 1 7 12 16 19
1 4 7 12 16 19
Sorted:
1
1 4 7 12 16 19
147 148
151 152
36 63 0
height difference between its left 36
0
0
subtree and right subtree does not 1 27
39 54 72 0
63
0
exceed mod (1). 0
0
0 0
27
• While in figure (b), if you check the 0 18
39
54 72
-1
node with value 3, its left subtree 45
subtree’s height=0. 36 63
0 1
• So, the difference is mod (2-0) = 2. 27
0 0
72
155 156
Node 6 has a balance factor of -2 after the insertion of node 10. After a single rotation towards Node 3 has a balance factor of 2, so it is unbalanced. After a single rotation towards the right,
the left, node 8 becomes the root and node 6 becomes its left child. node 2 becomes the root and node 3 becomes its right child.
By doing this, the BST property also remains intact, and the tree becomes height-balanced. And the new root, i.e., node 2, has a balance factor of 0. Hence, the tree becomes balanced.
157 158
Fig 5 Fig 6 4 7
1 3
4 5
5 Fig 11
165 166
4 4
4 4
2 6 2 6
2 2 7
6
1 3 7 1 3 7
15 3 15
16 16 1 3 1 6
5 5 5
Fig 12
Fig 13 16
15
4 7 14 16
5
Fig 15
2 6 14 Fig 16
1 3 15
5
16
7
Fig 14 167 Deletions can be done with similar rotations 168
171 172
Application of B-Tree
Another example of B-Tree
173
It designed to work on magnetic disks or other (direct access) secondary storage devices. 174
Properties of B-Tree
• A B-Tree T is a rooted tree having the following properties: B-Tree Insertion
1. Every node x has the following fields:
1. n[x] the no. of keys currently stored in node x
1) B-tree starts with a single root node (which is also a leaf node) at level 0.
2. The n[x] keys themselves, stored in non-decreasing order, so that key1[x] ≤ key2[x]…… ≤keyn- 2) Once the root node is full with d–1 search key values and when attempt to
1[x] ≤ keyn[x].
insert another entry in the tree, the root node splits into two nodes at level 1.
3. Leaf[x], a Boolean value that is TRUE if x is a leaf and FALSE if x is an internal node. 3) Only the middle value is kept in the root node, and the rest of the values are
2. Each internal node x also contains n[x]+1 pointers (Childs) c1[x], c2[x],--------- split evenly between the other two nodes.
cn[x]+1[x]. 4) When a non-root node is full and a new entry is inserted into it, that node is
3. All leaves have the same depth, which is the tree’s height h. split into two nodes at the same level, and the middle entry is moved to the
parent node along with two pointers to the new split nodes.
4. There are lower and upper bounds on the no. of keys a node can contains: these
bounds can be expressed in terms of a fixed integer t≥2 called the minimum degree
5) If the parent node is full, it is also split.
of B-Tree. 6) Splitting can propagate all the way to the root node, creating a new level if
– Every node other than the root must have at least t-1 keys, then root has at least t children if the tree is
the root is split.
non empty the root must have at least one key.
– Every node can contain at most 2t-1 keys. Therefore, an internal node can have at most 2t children we
say that a node is full if it contains exactly 2t-1 keys. 175 176
Constructing a B-tree 1
12 Constructing a B-tree
• Suppose we start with an empty B-tree and keys arrive in the 8
2
following order: 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 25 Add 6 to the tree
6
26 29 53 55 45 14
• We want to construct a B-tree of degree 5 (max d–1 key values) 28
17
• The first four items go into the root: 7
52 Exceeds Order. Promote
16 middle and split.
48
1 2 8 12 68 1 2 8 12 25
3
• To put the fifth item in the root would violate condition 5 26
29
• Therefore, when 25 arrives, pick the middle key to make a new 53
55
root 45
177 178
1 1
12
8
Constructing a B-tree (contd.) 12
8
Constructing a B-tree (contd.)
2 2
25 25
6 8 6 Adding 17 to the right leaf node would over-fill it, so we take
14 14 the middle key, promote it (to the root) and split the leaf
28 28
17 17
7 7 8
52 1 2 12 25 52
16 16
48 6, 14, 28 get added to the leaf nodes: 48
68 68
3 8 3 1 2 62 25 28 28
12 14 17
26 26
29 29
53 53
55 55
45 1 21 62 12 14
25 28 45
179 180
1 1
12
8
Constructing a B-tree (contd.) 12
8
Constructing a B-tree (contd.)
2 2
25 7, 52, 16, 48 get added to the leaf nodes 25
6 6 Adding 68 causes us to split the right most leaf,
14 14
28 28
promoting 48 to the root
17 17
7 8 17 7
52 52
16 16
48 48 8 17
68 68
3 1 2 76 12 1416 25 28 52
48 3
26 26
29 29 1 2 6 7 12 14 16 25 28 48 52 68
53 53
55 55
45 45
181 182
1 1
12
8
Constructing a B-tree (contd.) 12
8
Constructing a B-tree (contd.)
2 2
25 25
6 Adding 3 causes us to split the left most leaf 6 Add 26, 29, 53, 55 then go into the leaves
14 14
28 28
17 8 17 48 17 3 8 17 48
7 7
52 52
16 16
48 48
68 1 23 6 7 12 14 16 25 28 52 68 68 1 2 6 7 12 14 16 25262829 52536855
3 3
26 26
29 29
53 53
55 55
45 45
183 184
1
12 Constructing a B-tree (contd.) Constructing a B+ Tree
8
2
• Suppose we start with an empty B-tree and keys arrive in the
25 Exceeds Order. following order: 1 12 8 2 25 6 14 28 17 7 52 16
6 Add 45 increases the trees level Promote middle and
14 split. • We want to construct a B+ tree of degree 5
28
17 • The first four items go into the root:
7
52 Exceeds Order.
16 3 8 17 48
Promote middle and 1 2 8 12
48 split.
68
3 • To put the fifth item in the root would violate condition 5
26
29 1 2 6 7 12 14 16 25 26 28 29 45 52 53 55 68 • Therefore, when 25 arrives, pick the middle key to make a new
53
55
root
45
185 186
187 188
193