BST and AVL Trees, B Tree (1)
BST and AVL Trees, B Tree (1)
X Z
Binary Search Tree
• Values in left sub tree less than parent
• Values in right sub tree greater than parent
• Fast searches in a Binary Search tree,
maximum of log n comparisons
31
21 40
10 22 35 42
Binary Search Trees
• Exampl 5
es
1
2 4
10 0
5
5 4
5 30 3 5
4 0
2 2 3
5 1
2 25 5 0
0
2
Binary 5 Not a
search binary
trees search
tree
Example Binary Searches
• search (root,
25 )
1 5
0 10 < 25, 5 < 25,
right 2 4 right
5 3
0 30 > 25, 5 45 > 25,
left 3 left
2 2 4
5 5 25 = 25, 0 30 > 25,
found 1 left
0
10 < 25,
2 right
5
25 = 25,
found
Algorithm for Binary Search Tree
A) compare ITEM with the root node N of
the tree
i) if ITEM<N, proceed to the left child of N.
ii) if ITEM>N, proceed to the right child of
N.
B) repeat step (A) until one of the
following occurs
i) we meet a node N such that ITEM=N, i.e.
search is successful.
ii) we meet an empty sub tree, i.e. the
search is unsuccessful.
Binary Tree Implementation
typedef struct
node
{
int data;
struct node
*lc,*rc;
};
Iterative Search of Binary Search Tree
search()
{
while (n !=
NULL)
{ (n->data ==
if // Found
item) it
return n; // In left
if (n->data > subtree
item) n = n- // In right
>lc; subtree
else
n = n->rc;
} }
return null;
Insertion in a Binary Search Tree
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in
tree)
3. If X < Y, insert new leaf X as new left
subtree for Y
4. If X > Y, insert new leaf X as new right
subtree for Y
Insertion in a Binary Search Tree
• Insert ( 20 )
10
30 > 20,
5 10 < 20, right
left
30
25 > 20,
2 2 4 Insert 20 on
left
5 5 left
2
0
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
60
Insertion in a Binary Search Tree
40, 60, 50, 33, 55, 11
40
60
50
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
50
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
50
55
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
11 50
55
Deletion in Binary Tree
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else //we must delete internal
node
a) Replace with largest value Y on left
subtree
OR smallest value Z on right
subtree
b) Delete replacement value (Y or Z) from
subtree
Note :-
– Deletions may unbalance tree
Example
Deletion
(Leaf)
• Delete ( 25 )
10 1
10 < 25, 0
right
5 30 > 25, 5 3
30 left 0
2 2 4 25 = 25, 2 4
5 5 delete 5
Example Deletion (Internal Node)
• Delete
( 10 ) 5 5
10
5 3 2 3
5 30 0 0
2 25 2 25 45
2 25
45 45
Deleting
leaf
Replacing Replacing
10 with 5 with
largest largest
value in value in
Example Deletion (Internal Node)
• Delete
( 10 ) 2 2
10 5 5
5 3 5 3
5 30 0 0
2 25 2 4
2 25 45 5
45 Resulting
Deleting tree
Replacing leaf
10 with
smallest
value in
Binary Search Properties
• Time of search
– Proportional to height of tree
– Balanced binary tree
• O( log(n) ) time
– Degenerate tree
• O( n ) time
• Like searching linked list /
unsorted array
AVL Tree
AVL trees are height-balanced binary search
trees
Balance factor of a node=
height(left sub tree) - height(right sub tree)
An AVL tree has balance factor calculated at
every node
For every node, heights of left and right sub
tree can
differ by no more than 1
Store current heights in each node
AVL Tree
A binary tree in which the difference of
height of the right and left subtree of any
node is less than or equal to 1 is known as
AVL Tree.
Height of left subtree – height of right
subtree can be either -1,0,1
AVL Tree
0 0 0
0
AVL Tree
0
-1 0
3 2
0 1 0 0
Balanced as LST-
RST=1
Rotations in AVL
1) x, y, z (L)
y
0-2 = -2
x
x z
0-1=-1
y
0-0=0
z
Rotations in AVL
2) z, y, x (R)
y
z
2-0= 2
x z
y
1-0=1
x
0-0 =0
Rotations in AVL
Left Rotation
3) x, z, y (RL) y
0-2 = -2
x
x x z
Right rotation
z y
1-0 = 1
y
z
0-0 =0
Rotations in AVL
Right rotation
4) z, x, y (LR)
z
z
y
y
x Left rotation
x z
x
y
Example
Insert 3,2,1,4,5,6,7
Example
Insert 3,2,1,4,5,6,7
3
Example
Insert 3,2,1,4,5,6,7
3
2
Example
Insert 3,2,1,4,5,6,7
3
1
Example
Insert 3,2,1,4,5,6,7
Single
rotation
1
Example
Insert 3,2,1,4,5,6,7
2
1
3
Example
Insert 3,2,1,4,5,6,7
2
1
3
4
Example
Insert 3,2,1,4,5,6,7
2
1
3
5
Example
Insert 3,2,1,4,5,6,7
2
Single
1 rotation
3
5
Example
Insert 3,2,1,4,5,6,7
2
1
4
3 5
Example
Insert 3,2,1,4,5,6,7
2
1
4
3 5
6
Example
Insert 3,2,1,4,5,6,7
Single
rotation
2
1
4
3 5
6
Example
Insert 3,2,1,4,5,6,7
4
2
5
1 3 6
Example
Insert 3,2,1,4,5,6,7
4
2
5
1 3 6
7
Example
Insert 3,2,1,4,5,6,7
4
Single
2
5 rotation
1 3 6
7
Example
Insert 3,2,1,4,5,6,7
4
2
6
1 3 5 7
Practice Question
Insert 14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
14
11
19
7 12
17 53
4 8 13
16
20 60
Example
Deletion: 8, 7, 11, 14, 17
16
53
12
4 60
13 19
20
B- Tree (m-way)
• Generalization of BST in which a node can have more than one key and more than
2 children
• All leaf node must be at same level. Insertion will always take place at leaf node
only.
1 2 3
1 3 4
5
2 4
6
1 3 5 6 7
2 6 8
1 3 5 7 9 1
1) Insertion : D, K, P, V, A, G 3-way
3) Insertion 1 to 20 5-way
D K P
D K
A G P V
Insertion: 5, 3, 21, 9 , 1, 13, 2, 7, 10, 12, 4, 8 4-way
9
3 5 21
2 5 9 12
1 2 3 7 10 13
4
5
2 9 12
3 4 7 8 10
Example - 4
Insert : 50, 80, 10, 20, 60, 70, 75, 90, 95, 4, 5, 6, 14, 15, 16, 23, 27,
51, 52, 65, 68, 72, 73, 77, 78, 79, 81, 82, 89, 92, 93, 100, 111, 110
Order 5
Deletion from B-Tree
• Two cases:
1) Leaf node is having more than minimum number of keys.
2) Leaf node is having minimum number of keys
Borrow from
Borrow from If both sibling is having
immediate left
immediate right minimum no. of keys then
node
node Merging either with left or
right node including parent
3) Deletion from internal node:
Deletion
64, 23, 72, 65, 20, 70, 95, 77, 80, 100
50 80
10 16 60 70 77 90 95
81 82 89
4 5 6 20 27
78 79
92 93
14 15 73 75
51 52
65 68
Example - 4
Deletion
64, 23, 72, 65, 20, 70, 95, 77, 80, 100
68
10 50 79 90
73 75 78 92
4 5 6
81 82 89
14 15 16 27
51 52 60
Example - 4
Deletion
64, 23, 72, 65, 20, 70, 95, 77, 80, 100 Order 5
68
10 50 79 90
81 82 89
4 5 6 73 75 78
92 93 110 111
14 15 16 27
51 52 60