Escolar Documentos
Profissional Documentos
Cultura Documentos
By
Jyostna Devi Bodapati
Binary Search Tree - Best
Time
• All BST operations are O(d), where d is
tree depth
• minimum d is for a binary tree
with N nodes
› What is the best case tree?
› What is the worst case tree?
• So, best case running time of BST
operations is O(log N)
Binary Search Tree - Worst
Time
• Worst case running time is O(N)
› What happens when you Insert elements in
ascending order?
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
› Problem: Lack of “balance”:
• compare depths of left and right subtree
› Unbalanced degenerate tree
Balanced and unbalanced BST
1 4
2 2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6
1 3 5 7 7
Balanced Binary Trees
› AVL trees
› Red black trees
› Splay trees
› B-trees
› B+ trees
AVLTree
• AVL is invented by G.M. Adelson-Velsky and
E.M. Landis in 1962.
height of node = h
balance factor = hleft-hright
empty height = -1
AVL tree examples
Tree A (AVL) Tree B (not AVL)
balance factor
2 3 1-(-1) = 2
6 6
1 1 1 2
4 9 4 9
0 0 0 0 0 1 -1
1 5 7 1 5 8
0
7
height of node = h
balance factor = hleft-hright
empty height = -1
Examples of AVLTree
Operations on AVLTree
› Search
› Insertion
› Deletion
› Traversal
Search operation on AVLTree
Searching in an AVL tree is performed exactly the
same way as it is performed in a binary search tree.
0 +1
26 72
-1
0 0 -1
25
38 63 94
0
0
3 0 0
37
47 78
-1 +1
26 72
-2
0 0 -1
25
38 63 94
-1
0
3 0 0
37
47 78
0
1
AVL TreesNext
- Lecture
step: Find rebalancing case -->
Find rebalancing case Balance not ok
-1
57
-1 +1
26 72
-1
3 0
37 0 0
47 78
0
1
AVLNext
Trees - Lecture
step: apply Left Left rebalancing -->
Rebalance and recalculate balance factors Balance ok
0
57
0 +1
26 72
0 0 0 -1
3 38 63 94
0 0 0
0 0
1 25 37
47 78
-1
57
+1 +1
26 72
0 -1 0 -1
3 38 63 94
0 0 -1
0 0
1 25 37
47 78
0
30
AVL Trees - Lecture Next step: Insert 32 -->
Insert 32 and recalculate balance factors Balance not ok
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
0 32
AVL TreesNext
- Lecture
step: Find rebalancing case -->
Find rebalancing case Balance not ok
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
Left Right Case
0 32
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 78
0 47
-1 32
0 30
-1 57
+1 26 +1 72
0 3 -1 38 0 63 -1 94
0 1 0 25 0 32 0 47 0 78
0 30 0 37
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
0 35
AVL TreesNext
- Lecture
step: Find rebalancing case -->
Insert 35 Balance not ok
-2 57
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
Left Right Case
0 35
-2
57
+1
+2 72
26
0 -2 0 -1 94
3 38 63
0 0
1 25 -2 0 0
37 47 78
0
32
0 0
30 35
-1
57
+1
26 +1
72
0
3 0 0 -1
37 63 94
0 1 0
25 0 +1 0
32 38 78
0 0 0
30 35 47
-1
57
+1
26 +1
72
-1 94
0
3 0
37
0
63
0 1
0
25 0
32
+1
38
0
78
0 0 0
30 35 47
Exercise: insert 36
AVL Trees - Lecture
Example of Insertions in an
AVL Tree
2
20 Insert 5, 40
0 1
10 30
0 0
25 35
Example of Insertions in an
AVL Tree
2
3
20 20
1 1 1 2
10 30 10 30
0 0 0 1
0 0
5 25 35 5 25 35
0
40
Now Insert 45
Single rotation (outside case)
3
3
20 20
1 2 1 2
10 30 10 30
0 0 2
0 0
5 25 35 5 25 40 1
0 0
35 45
Imbalance 1 40
0 45 Now Insert 34
Double rotation (inside case)
3
3
20 20
1 3 1 2
10 30 10 35
0 0 2
0 1
5 Imbalance 25 40 5 30 40 1
0
1 0 25 34 45
35 45 0
Insertion of 34 0
34
AVL Tree Deletion
• Similar but more complex than insertion
› Rotations and double rotations needed to
rebalance
› Imbalance may propagate upward so that
many rotations may be needed.
Pros and Cons of AVL Trees
Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced.
2. Insertion and deletions are also O(logn)
3. The height balancing adds no more than a constant factor to the
speed of insertion.