cse 100: avl trees · balanced binary trees • deterministic balancing • change insert and...
TRANSCRIPT
CSE 100: AVL TREES
READING QUIZ – NO TALKING – NO NOTES
Q1: What is the definition of a balanced tree?
A. All nodes either have two children or they are leaves
B. The number of nodes to the left of the root is equal to the number of nodes to the right of the root
C. The height of the tree is O(log2N), where N is the number of nodes in the tree
D. The tree is a binary tree and it is completely full
READING QUIZ – NO TALKING – NO NOTES
Q2: Which of the following is a benefit of randomized methods for balancing BSTs over deterministic methods?
A. Balance is guaranteed B. Operations are simpler to implement C. Balance is not guaranteed
READING QUIZ – NO TALKING – NO NOTES
Q3: True or false: AVL trees are globally rebalanced periodically.
A. True B. False
READING QUIZ – NO TALKING – NO NOTES
Q4: What is a balance factor in an AVL tree?
A. A number in a node that indicates the difference between the height of the left and right subtrees of that node
B. The global amount that the tree is (or is not) out of balance
C. The number of rotations that it will take to bring the tree back into balance
Balanced Binary Trees Recall: • Worst case for find in a BST is :
• Worst case for find in a Balanced BST is:
6
Balanced Binary Trees • Deterministic Balancing
• Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees, red-black trees
• Randomized Balancing • Use random numbers to determine the values of the nodes,
independent from the keys • The resulting tree will probably be balanced (e.g., more like the
average case), but not guaranteed
TODAY
LATER
7
Are AVL trees new to you? A. Yes B. No
Height of a node Mark the height of each node in the tree.
42
10 67
53 90
What is the height of node with key 53? A. Zero B. One C. Two D. Three
8
Height of a node =
Depth of a node=
Local computation of height of a node
42
10 67
53 90
Height of a node= max{ Height of left child, Height of right child} +1
• Goal of AVL trees: For each node, maintain the difference between height of left and right children to within +/-1
• Each node maintains then a balance factor: • Balance factor= height of the right child – height of left child
9
Which of the following is/are balanced trees? And thus can become AVL trees by adding the balance factors
42
10 67
53 90
87
42
10 67
53 90
87
11
42
10 67
53 90
A B C
D. A&C E. A&B&C
Annotate the trees with balance factors (for those that are balanced)
10
Finding an element in a AVL Tree is worst case O(logN)! How would you prove this (we won’t, but imagine we wanted to)? A. Come up with a formula that shows that the height of the tallest AVL tree
with N nodes is never bigger than c*logN + k, for some c and k (assuming large N).
B. Come up with a formula that shows that the number of nodes, N, in an AVL tree is never bigger than c*logN + k for some c and k (assuming large N)
C. Come up with a formula that shows that the height of the tree never grows by more than log(N) when you insert a node.
D. Run some empirical tests to show that for any size tree, the time to find an element is never more than some formula c*logN + k for some c and k (assuming large N)
11
An AVL Tree is worst case O(logN) to find an element! (We might prove this later)
But how does the tree stay balanced as new nodes are inserted??
How would you prove this? A. Come up with a formula that shows that the height of the tallest AVL tree
with N nodes is never bigger than c*logN + k, for some c and k (assuming large N).
The key to this proof is showing that the height stays “small”, no matter how legally “unbalanced” the tree is.
12
Inserting and rebalancing 30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-1 0
0
-1
0 -1
0 0 0 0
0 0
Insert 3
13
Inserting and rebalancing 30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-1 0
0
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0 Propagate news up
14
Inserting and rebalancing 30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-1 0
-1
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
We’re OK, Keep going up
15
Inserting and rebalancing 30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-2 0
-1
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
Uh oh!
16
Rotations
17
X
y
C A
B
X
y
C A B
Inserting and rebalancing
30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-2 0
-1
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
Rotate! Left or right?
cut
18
Which of following is needed to rebalance? A. Rotate 5 left B. Rotate 10 left C. Rotate 5 right D. Rotate 10 right
Inserting and rebalancing 30
15 70
10 20 60 85
5 50 65 80 90
40 55
1
-1
-2 0
-1
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
lift
19
Inserting and rebalancing 30
15 70
10
20 60 85 5
50 65 80 90
40 55
1
-1
0
0 0
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
Adjust and reattach
20
We just did a single rotation of 5 around 10 A.k.a. a single right rotation
30
15 70
10
20 60 85 5
50 65 80 90
40 55
1
-1
0
0 0
-1
0 -1
0 0 0 0
0 0
Insert 3
3
0
Done!
21
Single rotation practice
30
15 70
20 60
0
1
0
-1
0
What could you insert into this AVL tree that would result in a single right rotation? A. 71 B. 10 C. 50 D. 66
22
Single rotation practice
30
15 70
20 60
0
1
0
-1
0
Insert 50. Draw the resulting AVL tree. (Don’t peek)
23
Single rotation practice
30
15 70
20 60
0
1
0
-2
-1
After insertion
50
0
24
Single rotation practice
30
15
70 20
60
0
1
0 0
0
After rotation
50
0
25
Single rotation is not enough
30
15 70
20 60
0
1
0
-1
0
What happens if we insert 66?
26
Single rotation is not enough
30
15 70
20 60
0
1
0
-2
1
66
0
cut
27
Is this a valid rotation?
30
15 70
20 60
0
1
0
-2
1
66
0
lift
70
60
0
0
66
0
A. Yes B. No
28
Invalid rotation!
30
15
20
0
1
0
70
60
-2
1
66
0
UH OH!
29
Double rotation to the rescue
30
15 70
20 60
0
1
0
-2
1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0
30
Double rotation to the rescue
30
15 70
20 60
0
1
0
-2
1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0
cut
31
Double rotation to the rescue
30
15 70
20 60
0
1
0
-2
1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0 lift
32
Double rotation to the rescue
30
15 70
20
60
0
1
0
-2
-1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0 adjust
33
Double rotation to the rescue
30
15 70
20
60
0
1
0
-2
-1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0 reattach
34
Double rotation to the rescue
30
15 70
20
60
0
1
0
-2
-1
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0
Where in the tree above should I cut to start this rotation?
A
B
C
35
Double rotation to the rescue
30
15
70 20 60
0
1
0
0
0
Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.
66
0
36
It’s sometimes even more complicated
30
15
70 60
0
1 0
0
Insert 64… do we need a double or a single rotation? A. Double B. Single C. No rotation needed
66
0
37
Where is the tree out of balance?
30
15
70 60
66
64
A. Node 30 B. Node 66 C. Node 60 D. Node 64
38
Rotate right around 66 to make a straight line
30
15
70 60
+2
1 -1
0
66
0
64
0
39
Rotate right around 66 to make a straight line
30
15
70 60
1
66
64
40
Rotate right around 66 to make a straight line
30
15
70
60
1
66
64
UH OH! Where do we put 64?? Are we stuck?
41
Rotate right around 66 to make a straight line
30
15
70
60
0
1 0
0
66
0
64
0
Will 64 always reattach there?
A. No, sometimes this doesn’t work B. Yes, this will always work
42
Finishing the rotation to balance the tree
30
15 70
60 -1
0
0
0
66
0
64
0
43