CS 261 –Data Structures - College of...
Transcript of CS 261 –Data Structures - College of...
CS 261 – Data Structures
AVL Trees
1
Binary Search Tree
• Complexity of BST operations:
–proportional to the length of the path from a node to the root
• Unbalanced tree: operations may be O(n)
–E.g.: adding elements in a sorted order
2
Balanced Binary Search Tree
• Balanced tree: the length of the longest path
is roughly log n
• BALANCE IS IMPORTANT!
3
Complete Binary Tree is Balanced
• Has the smallest height for any binary tree with the same number of nodes
• The longest path guaranteed to be ≤ log n
• => Keep the tree complete
4
Requiring Complete Trees
• However, it is very costly to maintain a
complete binary treeAlex
Abner Angela
Adela Alice
Adam
Abigail
Add to tree5
Requiring Complete Trees
• However, it is very costly to maintain a
complete binary treeAlex
Abner Angela
Adela Alice
Adam
Abigail
6
Height-Balanced Trees
•For each node, the height difference between the left and right subtrees is ≤ 1
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
indicates maximum
height
7
Height-Balanced Trees
• Are locally balanced, but globally (slightly)
unbalanced
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
8
Height-Balanced Trees
• Mathematically, the longest path has been shown to be, at worst, 44% longer than log n
• Algorithms that run in time proportional to the path length are still O(log n)
–Why?9
AVL Trees
• Named after the inventors’ initials:
–Adelson-Velskii and Landis
• Maintain the height balanced property of Binary Search Trees
10
AVL Trees
• Add an integer height field to each node:–Null child has a height of –1–A node is unbalanced when the absolute height difference between the left and right subtrees is greater than one
1 (2)
Node data Height field11
AVL Implementation
struct AVLNode {
TYPE val;
struct AVLNode *left;
struct AVLNode *rght;
int hght; /* Height of node*/
};
12
Get Height
int _height(struct AVLNode *cur)
{
if(cur == 0)
return -1
else return cur->hght;
}
13
Compute Height
void _setHeight(struct AVLNode *cur) {
int lh = _height(cur->left);
int rh = _height(cur->rght);
if(lh < rh)
cur->hght = 1 + rh;
else
cur->hght = 1 + lh;
}
14
Maintaining the Height Balanced Property
• When unbalanced, perform a “rotation” to balance the tree
3(0)
2(1)
1(2)
3(0)1(0)
2(1)
Rotateleft
Unbalancednode
15
Left Rotation1.Input: current2.New top = current's right child
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
4(?)
Current
New top
16
Rotateleft
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
2(?)
6(0)
4(?)
5(1)
New top
Current
17
Rotateleft
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current4.Current’s new right child = new top's left child
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(?)
2(?)
6(0)3(?)
4(?)
5(1)
New topCurrent
18
Rotateleft
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current4.Current’s new right child = new top's left child5.Set height of current6.Set height of new top node
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(0)
2(1)
6(0)3(0)
4(2)
5(1)
19
Rotateleft
Left Rotation
1(0)
2(1)
6(0)3(0)
4(2)
5(1)
20
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
Rotateleft
Right Rotation
2(0)
3(1)
7(0)5(0)
4(2)
6(1)
21
7(0)
6(3)
2(0)
3(1)
4(2)
5(0)
Right Rotation1.Input: current2.New top = current's left child3.New top’s right child = current4.Current’s new left child = new top's right child5.Set height of current6.Set height of new top node
7(0)
6(3)
2(0)
3(1)
4(2)
5(0) 2(0)
3(1)
7(0)5(0)
4(2)
6(1)
22
Double Rotation Left
• A single rotation may not fix the problem:
– When the right child is heavy, i.e.,• its parent is unbalanced• has only a right subtree
2(0)
1(2)
Unbalanced“top” node
3(1)“Heavy”
right child 2(0)
3(2)
1(1)Rotate
left
Doesn’twork!!!
23
Double Rotation Left
• Rotate the child before the regular rotation:1.Rotate the heavy right child to the right 2.Rotate the “top” node to the left
2(0)
1(2)Unbalanced“top” node
3(1)“Heavy”
right childRotate heavy
child right3(0)
2(1)
1(2)
3(0)1(0)
2(1)
Rotate topnode left
24
Double Rotation
• A single rotation may not fix the problem:
– When the left child is heavy, i.e.,• its parent in unbalanced from the left• has only a left subtree
2(0)
1(1)
Unbalanced“top” node 3(2)
“Heavy”left child 2(0)
1(2)
3(1)Rotateright
Doesn’twork!!!
25
Double Rotation Right
• This case requires rotating the child before the regular rotation:1.Rotate the heavy left child to the left 2.Rotate the “top” node to the right
Unbalanced“top” node
“Heavy”left child
Rotate left the heavy
child 1(0)
2(1)
3(2)
3(0)1(0)
2(1)
Rotate topnode right
2(0)
1(1)
3(2)
26
Balancing an Unbalanced NodeIf left child is taller than right child{/* Rotation right */
If left child is heavy{/* Double rotation right*/Rotate left the heavy left child
} Rotate right the node
}else{ /* Rotation left */If right child is heavy {/* Double rotation left */
Rotate right the heavy right child}Rotate left the node
}Return node
27
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
Example: Add 7 to the tree
Add data: 7
Height-Balanced Tree Unbalanced Tree
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Added to right sideof heavy left child
“Heavy” leftchild
Unbalanced“top” node
28
Tree Still Unbalanced
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Single rightrotation
Example – Suppose We Used Single Rotation
Unbalanced Tree
3(4)
1(0)
2(1)
7(0)
6(1) 9(0)
8(2)
5(3)
4(0)
Unbalanced“top” node(still)
29
Tree Still Unbalanced, but …
Rotate left theheavy left child
Example – Double Rotation Right
“Heavy” leftchild
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(1) 7(0)
6(2)
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Unbalanced Tree
30
Tree Now Balanced
Rotate right top node
Example – Double Rotation RightUnbalanced Tree(after 1st rotation)
3(3)
1(0)
2(1)
7(0)
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(1) 7(0)
6(2) Unbalanced“top” node
4(0)
5(1)
6(2)
9(0)
8(1)
31
Your Turn
• Any questions
• Worksheet:– Start by inserting values 1-7 into an empty AVL tree– Then write code for left and right rotations
32