Professional Documents
Culture Documents
ALGORITHMS
MODULE 4
AVL TREES
HEIGHT BALANCED TREES
BST Insert
7,5,4,8,6,9
4
7
5
6
Tree 1
Tree 2
7
8
BST Insert
4,5,6,7,8,9
7
5
6
Tree 1
6
Tree 2
7
8
9
Search 9
4
7
5
6
Tree 1
6
Tree 2
7
8
9
Search 9
4
7
5
6
Tree 1
6
Tree 2
7
8
3
comparison
s
6
comparison
s
7
5
Tree 1
No. of comparisons
1 <= N <= 3
6
Tree 2
No. of comparisons
1 <= N <= 6
7
8
9
Balance Factor
Balance factor (bf) = Height of the left subtree (hL)
Height of the right subtree (hR)
Height Balanced Binary Search Tree
A binary search tree is said to be height balanced binary
search tree if all its nodes have a balance factor of 1, 0 or -1
|bf|=| hL |-| hR | <= 1
Balance Factor
7
5
Balance Factor
2-2=0
7
1-1=0
0-0 = 04
0-1 = -1
5
6
0-0 = 0
8
9 0-0 = 0
Is it a height
balanced
tree ??
Balance Factor
2-2=0
This is a height
balanced BST
7
1-1=0
0-0 = 04
0-1 = -1
5
6
0-0 = 0
8
9 0-0 = 0
Is it a height
balanced
tree ??
7 0-2 =
-2
0-1 =
8 -1
9 0-0 = 0
7 0-2 =
-2
0-1 =
8 -1
9 0-0 = 0
Balancing a BST
Suppose the BST is balanced, and we need to insert a new node into the tree,
without making it unbalanced.
Steps:
1. Insert node into a BST
2. Compute the balance factors
3. Decide the pivot node
4. Balance the unbalanced tree
Balancing a BST
Steps:
1. Insert node into a BST
AVL rotations
4 possible cases
1.
2.
3.
4.
AVL rotations
1.
2.
3.
4.
AVL rotations
Case 1:
Unbalance occurs due to the insertion in the left subtree of the leftchild of the
pivot node (LEFT-TO-LEFT insertion)
P
A
Pr
Al
Ar
AVL rotations
Case 1:
Unbalance occurs due to the insertion in the left subtree of the leftchild of the
pivot node (LEFT-TO-LEFT insertion)
A
Pr
Al
Ar
Al
Ar
Pr
Algorithm
LeftToLeft Rotation
Input: Pointer Pptr to the pivot node
Output: AVL rotation corresponding to the unbalance due to insertion
in the left subtree of the left child of Pptr
1.
2.
3.
4.
5.
6.
Aptr=pptr->lchild
Pptr->lchild=Aptr->Rchild
Aptr->Rchild=Pptr
Pptr->Height=ComputeHeight(Pptr)
Aptr->Height=ComputeHeight(Aptr)
Stop
1
6
AVL rotations
Case 1: Example
0
2
0
1
1
8
0
0
3
0 9
2
6
AVL rotations
1.
1
2
Insert 0
1
1
1
8
0
3
0 9
2
6
AVL rotations
1.
pivot
2
1
2
1
1
Rotate Right
1
8
0
3
0 9
2
6
AVL rotations
1.
pivot
1
2
1
1
Rotate Right
1
8
0 9
4
0
3
2
6
AVL rotations
1.
pivot
1
2
1
1
Rotate Right
1
8
0 9
4
0
3
2
6
AVL rotations
1.
pivot
1
2
1
1
Rotate Right
1
8
0 9
4
0
3
AVL rotations
2
6
pivot
2
1
2
1
1
1
6
1
8
0
3
1
1
0 9
0
0
1
8
0
3
pivot
4
0 9
0
5
1.
AVL rotations
2
6
pivot
2
1
A
2
1
1
2.
3.
1
8
0
3
1
6
1
8
0
A 2
1
1
0 9
0
0
0
3
pivot
0 9
4
0
5
AVL rotations
Case 2:
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
P
A
Pl
Al
Ar
AVL rotations
Case 2:
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
B
B
Pl
Br
Bl
Br
Pl
Bl
Algorithm
RightToRightRotation
Input: Pointer Pptr to the pivot node
Output: AVL rotation corresponding to the
unbalance due to insertion in the right subtree of
the right child of Pptr
1.
2.
3.
4.
5.
6.
Bptr=pptr->rchild
Pptr->rchild=Bptr->lchild
Bptr->lchild=Pptr
Pptr->Height=ComputeHeight(Pptr)
Bptr->Height=ComputeHeight(Bptr)
Stop
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
1
5
0
2
1
7
4
0
0 9
6
0
0
1
0
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
0
2
2
7
4
0
0 9
6
0
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
Chose pivot node
0
2
2
7 pivot
4
0
0 9
6
0
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
Chose pivot node
Rotate left
0
2
2
7 pivot
0 9
0
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
Chose pivot node
Rotate left
0
2
2
pivot
4
0
7
0 9
0
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
Chose pivot node
Rotate left
2
pivot
4
9
0
2
0
7
0
6
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
2
5
Insert 11
Chose pivot node
Rotate left
2
pivot
4
9
0
2
0
7
0
6
1
1
0
0
1
1
AVL rotations
Case 2: Example
Unbalance occurs due to the insertion in the right subtree of the rightchild of
the pivot node (RIGHT-TO-RIGHT insertion)
1
5
2
5
1
0
2
2
7 pivot
0
2
0 9
0
0
4
pivot
0
7
9
1 1
0
0
1
1
0
6
0
1
1
1 1
1
0
1.
AVL rotations
2.
3.
1
5
2
5
1
0
2
2
7 pivot
0
2
0 9 B
0
0
4
pivot
0
7
9B
1 1
0
0
1
1
0
6
0
1
1
1 1
1
0
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
A
Pr
B
Al
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
Pr
B
Al
Al
Bl
Br
Bl
Br
Pr
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
It involves 2 rotations
Rotation 1
Pr
Pr
Al
Br
Bl
Br
Al
Bl
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
It involves 2 rotations
Rotation 2
Pr
A
Br
Al
Bl
Al
Bl
Br
Pr
Algorithm
LeftToRightRotation
Input: Pointer Pptr to the pivot node
Output: AVL rotation corresponding to the unbalance due to insertion
in the right subtree of the left child of Pptr
1.
2.
3.
4.
Aptr=Pptr->Lchild
RightToRight Rotation(Aptr)
LeftToLeftRotation(Pptr)
Stop
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
1
8
0
1
2
0
1
1
1
0
0
4
9
0
6
10
1
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
2
8
1
1
2
0
1
1
1
0
0
4
1
6
7
10
1
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
A
1
3
1
2
0
1
1
0
B
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
A
1
3
1
2
0
1
1
0
B
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
A
1
3
1
2
0
1
1
0
B
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
0
A
1
1
0
1
B
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
0
5 B
A
1
1
0
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
0
5 B
A
1
1
0
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
0
5 B
A
1
1
0
1
0
4
Pr
10
1
B
Al
Bl
1
6
7
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
5
1A
1
2
0
1
1
0
1 6
3
0
4
9
0
7
Pr
10
1
B
Al
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
8 pivot
1
1
5
1A
1
2
0
1
1
0
1 6
3
0
4
9
0
7
Pr
10
1
B
Al
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
1A
2
0
1
0
4
A
Pr
B
1
0
1 6
3
1
2 1
8 pivot
0
7
Al
10
1
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
1
5
B
2
8 pivot
1A
1 6
3
1
2
0
1
1
0
0
4
0
7
Pr
B
Al
10
1
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
1
5
B
2
8 pivot
1A
1 6
3
1
2
0
1
1
0
0
4
0
7
Pr
B
Al
10
1
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
1
5
B
2
8 pivot
1A
1 6
3
1
2
0
1
1
0
0
4
0
7
Pr
B
Al
10
1
Bl
Br
AVL rotations
Case 3:
Unbalance occurs due to the insertion in the right subtree of the leftchild of
the pivot node (LEFT-TO-RIGHT insertion)
0 B
5
0
A
1 3
1
2
0
1
pivot
0
4 1 6
1
0
0
7
Al
10
1
Bl
Br
Pr
AVL rotations
Case 4:
Unbalance occurs due to the insertion in the left subtree of the rightchild of
the pivot node (RIGHT-TO-LEFT insertion)
A
Pl
B
Ar
Bl
Br
AVL rotations
Case 4:
Unbalance occurs due to the insertion in the left subtree of the rightchild of
the pivot node (RIGHT-TO-LEFT insertion)
A
Pl
B
Ar
Bl
Br
AVL rotations
Case 4:
Unbalance occurs due to the insertion in the left subtree of the rightchild of
the pivot node (RIGHT-TO-LEFT insertion)
Pl
Pl
B
Ar
Bl
A
Bl
Br
Br
Ar
AVL rotations
Case 4:
Unbalance occurs due to the insertion in the left subtree of the rightchild of
the pivot node (RIGHT-TO-LEFT insertion)
B
B
Pl
A
Bl
Pl
Br
Ar
Bl
Br
Ar
Algorithm
RightToLeftRotation
Input: Pointer Pptr to the pivot node
Output: AVL rotation corresponding to the unbalance due to insertion
in the left subtree of the right child of Pptr
1.
2.
3.
4.
Aptr=pptr->Rchild
LeftToLeftRotation(Aptr)
RightToRightRotation(Pptr)
Stop
QUESTION:
Insert the following elements into a height balanced tree
6, 7, 9, 4, 3, 5, 8
6, 7, 9, 4, 3, 5, 8
6 0
6, 7, 9, 4, 3, 5, 8
6
6, 7, 9, 4, 3, 5, 8
6
0
9
6, 7, 9, 4, 3, 5, 8
pivot 6
0
9
6, 7, 9, 4, 3, 5, 8
0
7
pivot
6
0
9
6, 7, 9, 4, 3, 5, 8 1
7
0
9
6, 7, 9, 4, 3, 5, 8 2
7
0
9
6, 7, 9, 4, 3, 5, 8 2
7
pivot
0
9
6, 7, 9, 4, 3, 5, 8 1
7
0
9
pivot
3
6, 7, 9, 4, 3, 5, 8 1
7
0
4
6, 7, 9, 4, 3, 5,pivot
82
7
0
1
6, 7, 9, 4, 3, 5,pivot
82
7
0
1
6, 7, 9, 4, 3, 5,pivot
82
7
0
1
0
3
0
5
6, 7, 9, 4, 3, 5,pivot
82
7
0
1
0
3
0
5
6, 7, 9, 4, 3, 5,pivot
82
7
0
6
0
3
0
5
6, 7, 9, 4, 3, 5, 8
6
pivot
7 1
0
3
0
5
6, 7, 9, 4, 3, 5, 86
pivot
7 2
0
3
6, 7, 9, 4, 3, 5, 86
pivot
7 2
0
3
0
5
1
8
6, 7, 9, 4, 3, 5, 86
0
3
pivot
6, 7, 9, 4, 3, 5, 86
0
3
0
5
???
Search for a node in a BST which contains 1,00,000 nodes
Search the path from root to the node that contains the
specified element.
What if the nodes are located in different blocks in the
memory?
Most of the search time is spent on memory access
Definition:
An m-way search tree is a tree in which
All the nodes are of degree<=m
Each node in the tree contains the following attributes:
P0
K1
P1
K2
P2
Kn
Pn
where
1<=n<m
Ki(1<=i<=n) are key values in the node
Pi(0<=i<=n) are pointers to the sub-trees of T.
Ki<Ki+1,
1<=i<n
All the key values in the sub-tree pointed by Pi are less than the
key values Ki+1, 0<=i<n
All the key values in the sub-tree pointed by Pn is greater than
Kn
All the sub-trees pointed by Pi(o<=i<=n) are also m-way search
trees.
1, 4
6, 7
9, 20
10,
11
21,
22
B TREE
B-Tree of order M
A B-tree of order m is an m-way search tree that is either empty or
B-Tree of order M
A B-tree of order m is an m-way search tree that is either empty or
1
0
1
5
1
2
2
9
1
4
1
9
2
1
2
3
2
7
30
B-Tree of order 3
When a new key is to inserted into a full node, the tree is split into two nodes,
and the key with the median value is inserted in the parent node. In case
the parent node is the root, a new root is created.
20
1
0
1
5
1
2
2
9
1
4
1
9
2
1
2
3
2
7
30
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
1
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
1 5
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
1 5 6
To insert 2
Split the node
at the median
value
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5
1 2
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5
1 2
6 8
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5
1 2
6 8 1
1
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5
1 2
6 8 1
1
To insert 13
Split the node
at the median
value and move
it to the parent
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8
1 2
1
1
To insert 13
Split the node
at the median
value and move
it to the parent
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8
1 2
1
1
1
3
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8
1 2
1
1
1
3
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8
1 2
1
1
1
3
1
8
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8
1 2
1
1
1
3
1
8
To insert 20
Split the node
at the median
value and move
it to the parent
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8 1
3
1 2
1
1
1
8
To insert 20
Split the node
at the median
value and move
it to the parent
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8 1
3
1 2
1
1
1
8
2
0
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8 1
3
1 2
6 7
1
1
1
8
2
0
Btree of order
4
Insert 1,5,6,2,8,11,13,18,20,7,9
5 8 1
3
1 2
6 7
1
1
1
8
2
0
B+ TREE
B+ Tree
All the leaves have been connected to form a doubly linked list.
20
1
0
1
0
1
5
1
2
1
4
2
9
1
5
2
0
2
2
2
1
2
3
2
7
2
9
30
B+ Tree
following properties:
1. All data nodes are at the same level and are leaves. Data
nodes contain elements only
2. The index nodes define a B-Tree of order m, each index node
has keys but not elements
20
1
0
1
0
1
2
1
5
1
4
2
9
1
5
2
0
2
2
2
1
2
3
2
7
2
9
30