This document discusses different types of balanced binary search trees including AVL trees, B-trees, and B+-trees. It provides examples of how to construct and perform operations on each type of tree. AVL trees balance after each insertion or deletion by performing tree rotations if needed. B-trees allow nodes to have more than two children and remain balanced by splitting full nodes. B+-trees are like B-trees but also allow sequential record traversal through leaf nodes. Threaded binary trees add threads to leaf nodes for efficient in-order traversal.
AVL Tree (Adelson,Velski, Landis)
AVL tree is a special kind tree in which the balance
factor of each can not be more than 0, 1 or -1.
It also called as height balanced tree.
Or we can say that height of two child sub tree of any
node differ at most by one where left and right sub
trees again AVL Tree.
By Prof. Raj Sarode 2
Balance Factor (BF) = Height of the left sub tree -
Height of the Right Sub tree
Or
BF = HL – HR
3.
By Prof. RajSarode 3
E.g.
A
B
C
D
BF = 0 - 3 = -3
BF = 0 -2 = - 2
BF = 0 - 1 = -1
BF = 0 - 0 = 0
C
A
B
D
E
BF = 0 - 0 = 0
BF = 0 - 1 = -1
BF = 0 - 0 = 0
BF = 0 - 1 = -1
BF = 0 - 0 = 0
(a) Unbalance BST (b) Balance BST
Note:
BF = -1 i.e. right sub tree is higher than left sub tree or it is called right heavy tree
BF = 1 i.e. left sub tree is higher than right sub tree or it is called as left heavy tree.
BF = 0 i.e. Both sub tree are on same height.
4.
By Prof. RajSarode 4
Representation of AVL Tree
AVL Tree can also be represented or implemented as other tree in memory
with an additional field to keep track of balance factor.
The balance factor of node represents the difference of height between the
left sub tree and right sub tree of the node.
Left Info Right BF
Structure of AVL Tree node
Struct Node
{
Node * Left;
Int Info;
Node * Right;
Int BF;
};
Node * root;Building AVL Tree or Height Balance Tree
AVL Tree is constructed by using the same process as applied to BST.
The only thing we need to remember that it follow the property of height
balance tree (BF).
The balance factor of each node can be 0, 1 or -1.
While the insertion of deletion is preformed, the tree become unbalanced and
violate the property of AVL tree that time we have to apply different rotation on
the AVL Tree
5.
AVL Tree Rotation
ByProf. Raj Sarode 5
A
B
C
BF = 0 -2 = - 2
BF = 0 - 1 = -1
BF = 0 - 0 = 0
R R
A
B
CBF = 0 - 0 = 0
BF = 1 - 1 = 0
BF = 0 - 0 = 0
A
B
C
BF = 2 - 0 = 2
BF = 1 - 0 = 1
BF = 0 - 0 = 0
L L
C
B
ABF = 0 - 0 = 0
BF = 1 - 1 = 0
BF = 0 - 0 = 0
1. Left to Left Rotation
2. Right to Right Rotation
6.
AVL Tree Rotation
ByProf. Raj Sarode 6
A
B
C
BF = 0 - 2 = - 2
BF = 1 - 0 = 1
BF = 0 - 0 = 0
R L
A
C
BBF = 0 - 0 = 0
BF = 1 - 1 = 0
BF = 0 - 0 = 0
A
B
C
BF = 2 - 0 = 2
BF = 0 - 1 = -1
BF = 0 - 0 = 0
L R
B
C
ABF = 0 - 0 = 0
BF = 1 - 1 = 0
BF = 0 - 0 = 0
3. Left to Right Rotation
4. Right to Left Rotation
7.
By Prof. RajSarode 7
Sr.
No.
I/P Node Tree Rotation
1 Tree is balanced
2 Tree is balanced
3 Tree is balanced
23 BF = 0 - 0 = 023
12
23
12
1
0
82 23
12
1
0
82
0
e.g. 23, 12, 82, 15, 16, 57, 50
8.
By Prof. RajSarode 8
Sr.
No.
I/P Node Tree Rotation
4 Tree is balanced
5
Now Tree is balanced
15
16
23
12
1
-1
82
0
15
0
23
12
1
-2
82
0
15
-1
16
0
23
12
8215
16
R R
0 0
00
1
e.g. 23, 12, 82, 15, 16, 57, 50
9.
By Prof. RajSarode 9
e.g. 23, 12, 82, 15, 16, 57, 50
Sr.
No.
I/P Node Tree Rotation
6 Tree is balanced
7
Now Tree is balanced
57
23
12
8215
16 57
0 0 0
0 1
0
50 23
12
8215
16 57
50
0 0
0
0
1
2
-1
23
12 82
15
16
57
50
0 0
0
0 0
0
0
L L
10.
Expression Tree
By Prof.Raj Sarode 10
All arithmetic operation are unary or binary so expression tree are generally binary
Left child represent the left operand.
Right child represent the right operand.
In case of unary operator a node can have only one child.
Root node always contains the operator.
Parenthesis are evaluated first then the exponent, followed by multiplication or
division & then addition or subtraction.
E.g.
A + B
A
+
B
11.
By Prof. RajSarode 11
E.g. ( A – B * C ) / ( D + E / F )
B
*
C
Step :1 First parenthesis is evaluated so construct the tree for (A – B * C )
B
*
C
A
-
B * C Then (A - B * C)
Step :2 Second parenthesis is evaluated so construct the tree for (D + E / F )
E
/
F
E
/
F
D
+
E / F Then (D + E / F )
12.
By Prof. RajSarode 12
E.g. ( A – B * C ) / ( D + E / F )
Step :3 Result of First parenthesis is divided by second parenthesis
B
*
C
A
-
E
/
F
D
+
/
In-order: (A-B*C)/(D+E/F)
Pre-order: / + D / E F – A * B C
Post- order: D E F / + A B C * - /
Since expression is also binary tree so it can also be traversed in three different ways
13.
B Tree
By Prof.Raj Sarode 13
In previous section we learn BST, AVL Tree and Expression Tree but the problem is
that no. of node is more then height of tree is increased also and will consume
more time for different operation.
So to remove such drawback we introduce M way tree or B Tree having following
properties
1. Each node can contain N-1 key elements, where N is the order of tree
2. Each node can have N branches.
3. The root is either a leaf or it has 2 to n sub tree, where N is the order of B tree.
4. All nodes expect the root and leaves can have minimum N/2 and Maximum N
Branches.
5. All the leaf nodes are on the same level so tree is balanced.
6. When the new node is inserted into full node (with N-1 elements), then the
node is split in to two new node at the same level and key with the median
value is inserted in the parent node in case the parent node is the root, a new
root will be created.
7. Full node condition is called the overflow.
14.
By Prof. RajSarode 14
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
1
1 6
1 6 8
2 6 81 2
6
81
2
6
81 9
Insert 1
Insert 6
Insert 8
Insert 2
Insert 9
15.
By Prof. RajSarode 15
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
2
6
81 9 12
Insert 12
2
6
81 9 12 15
Insert 15
2
6
81
9
12 15
2
6
81
9
12 15
Insert 7
7
2
6
81
9
12 157
Insert 18
18
16.
By Prof. RajSarode 16
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
3
6
82
9
12 157
Insert 3
181
4
6
82
9
12 157
Insert 4
181 3
4
6
8
2 9
12 157 181 3
17.
By Prof. RajSarode 17
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
Insert 20
4
6
8
2 9
12 157 181 3 20
4
6
8
2 9
12
15
7 181 3 20
4
6
8
2 9
12
15
7 181 3 20 Final B Tree
18.
By Prof. RajSarode 18
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
Delete 15
4
6
8
2 9
12
15
7 181 3 20
Final B Tree
4
6
8
2 9
12
18
7 201 3
19.
By Prof. RajSarode 19
Construct the B-Tree of order 4 for the following elements
1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20
Delete 12
Final B Tree
4
6
8
2 9
12
18
7 201 3
4
6
8
2 9 18
7 201 3
20.
B+ Tree
By Prof.Raj Sarode 20
In B Tree we can access record only in random , we cannot traverse the records
sequentially.
After finding particular record we can not get to get the previous or next record but B+
tree has both properties, random access as well as sequential traversal.
In B+ tree the structure of leaf node & internal node is different
Internal node only store keys and child pointer.
Leaf node store keys & Corresponding data items so data items present only in leaf
node
21.
Threaded Binary Tree
ByProf. Raj Sarode 21
Binary trees have a lot of wasted space: the leaf nodes each have 2 null
pointers
We can use these pointers to help us in in-order traversals
Thread: a pointer to other nodes in the tree for replacing the 0-link.
We have the pointers reference the next node in an in-order traversal;
called threads
We need to know if a pointer is an actual link or a thread, so we keep a
Boolean for each pointer