Tree
Tree
Tree
Level 0
A
Level 1
B C
D
Level 2
A
The height of this tree
B C is 2
Degree of a node: number of children of that node. e.g: Degree of A is 2 and Degree of
B is 1.
level 2
level 3
Full Binary Tree: A full binary tree (sometimes proper binary tree or 2-tree) is a
tree in which every node other than the leaves has two children
Perfect Binary Tree: Binary tree in which all interior nodes have two children and
all leaves have the same depth or same level.
Answer : 2(h+1) -1
Answer : n -1
D
E F
Index of H is 14… what is the index of
its parent?
G H
Maximum size of the array to store tree is : 1(level 0)+ 2(level 1) + 4 (level 2) + 8 (level 3) = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A B C D E - F G - - - - - - H
) )
return
return
return
return
return
return
return
return
3 2
2
3
1
Search for 12
Ref: https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9
(D) 9 8 6 4 2 3 0 1 5 7
inorder(root);
return 0;
}
(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9
(D) 9 8 6 4 2 3 0 1 5 7
return current;
}
root->key = temp->key;
Answer: (B)
(A) 8, 7, 6, 5, 4, 3, 2, 1
(B) 1, 2, 3, 4, 8, 7, 6, 5
(C) 2, 1, 4, 3, 6, 7, 8,5
(D) 2, 1, 4, 3, 7, 8, 6, 5
Answer: (D)
(A) {10, 75, 64, 43, 60, 57, 55} (B) {90, 12, 68, 34, 62, 45,
55}
(C) {9, 85, 47, 68, 43, 57, 55} (D) {79, 14, 72, 56, 16, 53,
55}
Answer: (C)
Answer: (D)
Answer: (D)
Prepared By : Vaishali Koria Data Structure and Algorithms
GATE 2008
A Binary Search Tree (BST) stores values in the range 37 to 573.
Consider the following sequence of keys.
I. 81, 537, 102, 439, 285, 376, 305 II. 52, 97, 121, 195, 242,
381, 472
III. 142, 248, 520, 386, 345, 270, 307 IV. 550, 149, 507, 395,
463, 402, 270
Which of the following statements is TRUE?
Minimum no of nodes is 7.
h=3
30 60
24 67
22
• If balanced factor is affected after doing insertion, we have to perform some rotations
to restore the balanced factor of node.
0
J
X Y
0 0
RR Rotation
-1 1 14 1
14 LL rotation
1 64
14 1-2= -1 1-2= -1
14
14 Insert 26
Insert 13
1 64 1 1 64 1
1 64
0 -
1
26 13 26
0
0 0
14 0
1-2= -1 14 2-3= -1
14
98
14 2-3= -1 14 2-4= -2
Which rotations are
required?
Insert 85
1 64 - 1 64 -2
1
- - Balance factor of 110 is 2,
1 1
LL rotation is required.
13 26 110 13 26 110
1 2
0 0 0 0
98 98 1
0
85
0
1 64 -2 1 64 -1
-1 -1
13 26 110 13 26 98
2 0
0 0 0 0
98 1 85 110
LL rotation
0 0
85
0
0 C
A D
D
C D
A C
0
D
D C
C C
A E
D E
0 0
D E E D