cosc 2007 data structures ii chapter 12 advanced implementation of tables ii
TRANSCRIPT
COSC 2007 Data Structures II
Chapter 12Advanced Implementation of
Tables II
2
Topics
AVL trees Definition Operations
Insertion Deletion Traversal Search
3
AVL (Adel'son-Vel'skii & Landis) Trees
AVL Tree Balanced BST: Adelson_Velskii and Landii Height is guaranteed to be O(log2 n) After each insertion & deletion, the tree is checked to
see if it is still an AVL tree or not Tree is rebalanced by rearranging its nodes by some
rotations around some pivot Advantages:
Permit insertion, deletion, and retrieval to be done in a dynamic application
Worst-case performance of O(log n)
4
AVL Trees
Terminology & Definitions Height-balanced p-tree:
For each node in the tree, the difference in height of its two subtrees is at most p
Height-balanced 1-tree: For each node in the tree, the difference in height of
its two subtrees is at most 1. AVL-tree is a height-balanced 1-tree
60
7040
30 50
5645
5
AVL Trees
Terminology & Definitions Balance Difference (BD)
Height of the node's right subtree - Height of the node's left subtree
Pivot node (first bad node) Deepest node on a search path, after inserting the new node
that has a BD value +2, -2
Search Path: Path from the root to the point which a node is to be inserted or
found
60
7040
30 50
5645
6
AVL Trees: Example BST PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X. 6
2
4
3
1
8
9
7
AVL Trees: Example
BST PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.
AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.6
2
4
3
1
8
9
8
AVL Trees: Example AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.
height(X) = max(height(left(X)), height(right(X))) + 1
6
2
4
3
1
8
9
9
ADT AVL Tree
Operations: Same as BST Only the type of the tree will be AVL-Tree
instead of a BST Insert Delete Retrieve Traverse
6
2
4
3
1
8
9
10
ADT AVL Tree
Insertion: Search path:
Path from the root to the inserted node Idea:
Attach the new node as in a BST. Start from the new node Find the deepest node on the search path that has a BD value =
+2 or –2 after insertion of the new node. Call it the Pivot Node. Adjust the tree so that the values of balance are correct by rotating
and it is still an AVL tree. We are done, and do not need to continue up the tree
Adding a node to the short subtree change the BAL value from the pivot down to the node
Adding a node to the tall subtree of the pivot node requires adjustment to the tree's structure
6
2
4
3
1
8
9
11
ADT AVL Tree Insert operation:
Case 1: Every node on the search path is balanced, having
BD=0. No possibility that the tree will be non-AVL after insert
Just adjust the tree by assigning new BD values to each node on the search path
8040
600
00 8040
60
20
Insert 20
0-1
0
-1
700
8040
60
20
Insert 70
-1-1
0
0
12
ADT AVL Tree
Insertion: Case 2:
New node is added to the short subtree of the pivot node
No rotation is required Adjusting tree by changing value of BD for each
node on the search path starting with the pivot node
700
8040
60
20
1--1
0
0
70
0
8040
60
20
Insert 50
1-0
0
0
50
0
70
0
8040
60
20
Insert 90
00
0
0
20 90
00
13
ADT AVL Tree
Insertion: Case 3:
New node is added to the tall subtree of the pivot node
Adding the new node unbalances the tree (not an AVL tree)
Rotation is required
14
ADT AVL Tree Insertion:
Case 3-a: Single Rotation
10020 50
10
8040
60
30
Insert 5
0
10020 50
10
8040
60
30
5
-1
-1
0
-2
Pivot100
20
50
10
80
40
60
3050 0
-1
-1
0
0
00
0
+1
Rebalance
15
ADT AVL Tree
Insert operation: Case 3-b: Double rotation
The new node is added to the tall subtree of the pivot node Adding the new node unbalances the tree (not an AVL tree) Example: Insert 35
100
30
50
20
80
40
60
35100
-1
-1
0
0
00
0
+1
Double Rotation
10020 50
10
8040
60
30
35 0
0
+1
+1
-2 (Pivot)
Insert 35
16
ADT AVL Tree
General Rebalancing Idea: Single Rotation
Longer path is on the outside of the tree Some rotations decrease tree height
0
Single rotation20
40
-1
-2 (Pivot)
hh+1
h
Tree height = h+3
20
400
0
hh+1
h
Tree height = h+2
17
ADT AVL Tree
General Rebalancing Idea: Single Rotation
Some rotations keep the tree height
Single rotation
0
20
40
-1
-2 (Pivot)
h+1
h
Tree height = h+3
h+1
20
400
0
h+1h
Tree height = h+3
h+1
18
ADT AVL Tree
General Rebalancing Idea: Double Rotation
Longer path is on the inside of the tree 3 nodes are involved
Double rotation20
40
+1
-2 (Pivot)
h
+1
h
Tree height = h+4
h
30
h
+1
-1200 0
h
+1
40
h
Tree height = h+3
h
30
h
+1
-1
19
Node Deletion in AVL Trees
There are 4 cases: Case 1:
No node in the tree contains the data to be deleted No deletion occurs
Case 2: The node to be deleted has no children.
Deletion occurs. Check for BAL of all nodes should be performed. If rebalancing is required, perform it
Case 3: The node to be deleted has one child
Connect this child to the previous parent of the deleted node and then check for balancing
Case 4: The node to be deleted has two children
Search for the rightmost (or the leftmost) node in the left (in the right) subtree of the node removed and replace the removed node with this node. Then check for balancing
20
Efficiency of AVL Trees
Changes required by insertion into or deletion from an AVL tree are limited to a path between the root and the leaf Time complexity?
21
AVL Trees
Traversal Time complexity?
Search