lecture 10: bst and avl
TRANSCRIPT
Lecture 10: BST and AVL Trees
CSE 373: Data Structures and Algorithms
CSE 373 19 SP - KASEY CHAMPION 1
Warm Up
CSE 373 19 SP - KASEY CHAMPION 2
8
6 11
157
9 9 > 8
6
2 8
1 7 124
10 13
Is valid BST?Height?
Is valid BST?Height?
No
Yes
2
3
Administrivia
CSE 373 19 SP - KASEY CHAMPION 3
Trees
CSE 373 SP 18 - KASEY CHAMPION 4
Binary Search TreesA binary search tree is a binary tree that contains comparable items such that for every node, all children to the left contain smaller data and all children to the right contain larger data.
CSE 373 SP 18 - KASEY CHAMPION 5
10
9 15
7 12 18
8 17
Implement DictionaryBinary Search Trees allow us to:- quickly find what we’re looking for- add and remove values easily
Dictionary Operations:Runtime in terms of height, “h”get() – O(h)put() – O(h)remove() – O(h)
What do you replace the node with?Largest in left sub tree or smallest in right sub tree
CSE 373 SP 18 - KASEY CHAMPION 6
10
“foo”
7
“bar”
12
“baz”
9
“sho”
5
“fo”
15
“sup”
13
“boo”
8
“poo”
1
“burp”
Height in terms of NodesFor “balanced” trees h ≈ logc(n) where c is the maximum number of children
Balanced binary trees h ≈ log2(n)
Balanced trinary tree h ≈ log3(n)
Thus for balanced trees operations take Θ(logc(n))
CSE 373 SP 18 - KASEY CHAMPION 7
Unbalanced TreesIs this a valid Binary Search Tree?
Yes, but…
We call this a degenerate treeFor trees, depending on how balanced they are,
Operations at worst can be O(n) and at best
can be O(logn)
How are degenerate trees formed?- insert(10)- insert(9)- insert(7)- insert(5)
CSE 373 SP 18 - KASEY CHAMPION 8
10
9
7
5
Implementing Dictionary with BSTpublic boolean contains(K key, BSTNode node) {
if (node == null) {
return false;
}
int compareResult = compareTo(key, node.data);
if (compareResult < 0) {
returns contains(key, node.left);
} else if (compareResult > 0) {
returns contains(key, node.right);
} else {
returns true;
}
}
CSE 373 SP 18 - KASEY CHAMPION 9
+C1
+C3
+C2+T(n/2) best+ T(n-1) worst+T(n/2) best+ T(n-1) worst ! " = $
% &'(" " < * +, -(. /+0"1! "2 + % +4'(,&56(
! " = 7% &'(" " < * +, -(. /+0"1! " − 9 + % +4'(,&56(
Best Case (assuming key is at the bottom)
Worst Case (assuming key is at the bottom)
2 Minutes
Measuring BalanceMeasuring balance:
For each node, compare the heights of its two sub trees
Balanced when the difference in height between sub trees is no greater than 1
CSE 373 SP 18 - KASEY CHAMPION 10
10
15
12 18
8
7
78
7 9
Balanced
Unbalanced
Balanced
Balanced
2 Minutes
Meet AVL TreesAVL Trees must satisfy the following properties: - binary trees: all nodes must have between 0 and 2 children- binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be
larger than the root node- balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right.
Math.abs(height(left subtree) – height(right subtree)) ≤ 1
AVL stands for Adelson-Velsky and Landis (the inventors of the data structure)
CSE 373 SP 18 - KASEY CHAMPION 11
Is this a valid AVL tree?
CSE 373 SP 18 - KASEY CHAMPION 12
7
4 10
3 9 125
8 11 13
14
2 6
Is it…- Binary- BST- Balanced?
yesyesyes
Is this a valid AVL tree?
CSE 373 SP 18 - KASEY CHAMPION 13
6
2 8
1 7 124
9
10 13
11
3 5
Is it…- Binary- BST- Balanced?
yesyesno
Height = 2Height = 0
2 Minutes
Is this a valid AVL tree?
CSE 373 SP 18 - KASEY CHAMPION 14
8
6 11
2 157
-1 9
Is it…- Binary- BST- Balanced?
yesnoyes
9 > 85
2 Minutes
Implementing an AVL tree dictionaryDictionary Operations:
get() – same as BST
containsKey() – same as BST
put() - ???
remove() - ???
CSE 373 SP 18 - KASEY CHAMPION 15
Add the node to keep BST, fix AVL property if necessary
Replace the node to keep BST, fix AVL property if necessary
1
2
3
Unbalanced!
2
1 3
Rotations!
CSE 373 SP 18 - KASEY CHAMPION 16
a
b
X
c
Y Z
a
b
X
Y Z c
a
b
X Y
Z
Insert ‘c’
Balanced!Unbalanced!
Rotate Left
CSE 373 SP 18 - KASEY CHAMPION 17
a
b
X
c
Y Z
a
b
X
Y Z c
a
b
X Y
Z
Insert ‘c’
Unbalanced!Balanced!
parent’s right becomes child’s left, child’s left becomes its parent
Rotate Right
CSE 373 SP 18 - KASEY CHAMPION 18
15
8 22
4 2410
3
19
6 2017
put(16);
16
0 0
1 0
2
0 0
01
2
3 height
0
1
2
3
4
Unbalanced!
parent’s left becomes child’s right, child’s right becomes its parent
CSE 373 SP 18 - KASEY CHAMPION 19
15
8
224
24
10
3
19
6 20
17
put(16);
160 0
1 0
2
3
1
0 0 0
1
2
height
Rotate Rightparent’s left becomes child’s right, child’s right becomes its parent
So much can go wrong
CSE 373 SP 18 - KASEY CHAMPION 20
1
3
2
Unbalanced!3
1
2
Rotate Left
Unbalanced!
Rotate Right
1
3
2
Unbalanced!
Parent’s left becomes child’s rightChild’s right becomes its parent
Parent’s right becomes child’s leftChild’s left becomes its parent
Two AVL Cases
CSE 373 SP 18 - KASEY CHAMPION 21
1
3
2
1
2
3
Line CaseSolve with 1 rotation
Kink CaseSolve with 2 rotations
3
2
1
Rotate RightParent’s left becomes child’s rightChild’s right becomes its parent
Rotate LeftParent’s right becomes child’s leftChild’s left becomes its parent
3
1
2
Right Kink ResolutionRotate subtree leftRotate root tree right
Left Kink ResolutionRotate subtree rightRotate root tree left
Double Rotations 1
CSE 373 SP 18 - KASEY CHAMPION 22
a
e
Wd
Y
Z
a
e
X
X
Z
Insert ‘c’
Unbalanced!
d
X
Y
c
a
d
W
Y
ZX
e
c
Double Rotations 2
CSE 373 SP 18 - KASEY CHAMPION 23
a
d
W
Y
ZX
e
c
Unbalanced!
a
d
W Y ZX
e
c
Implementing Dictionary with AVLpublic boolean contains(K key, AVLNode node) {
if (node == null) {
return false;
}
int compareResult = compareTo(key, node.data);
if (compareResult < 0) {
returns contains(key, node.left);
} else if (compareResult > 0) {
returns contains(key, node.right);
} else {
returns true;
}
}
CSE 373 SP 18 - KASEY CHAMPION 24
+C1
+C3
+C2
+T(n/2)
+T(n/2)
! " = $% &'(" " < * +, -(. /+0"1
! "2 + % +4'(,&56(
Worst Case Improvement Guaranteed with AVL
2 Minutes
How long does AVL insert take?
AVL insert time = BST insert time + time it takes to rebalance the tree
= O(log n) + time it takes to rebalance the tree
How long does rebalancing take?- Assume we store in each node the height of its subtree.- How long to find an unbalanced node:
- Just go back up the tree from where we inserted.
- How many rotations might we have to do?- Just a single or double rotation on the lowest unbalanced node.
AVL insert time = O(log n)+ O(log n) + O(1) = O(log n)
ß O(log n)
ß O(1)
CSE 373 AU 18 – SHRI MARE
Pros:
- O(logn) worst case for find, insert, and delete operations.
- Reliable running times than regular BSTs (because trees are balanced)
Cons:
- Difficult to program & debug [but done once in a library!]
- (Slightly) more space than BSTs to store node heights.
AVL wrap up
CSE 373 AU 18 – SHRI MARE 26
Lots of cool Self-Balancing BSTs out there!
Popular self-balancing BSTs include:
AVL tree
Splay tree
2-3 tree
AA tree
Red-black tree
Scapegoat tree
Treap
(From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations)
(Not covered in this class, but several are in the textbook and all of them are online!)
CSE 373 SU 17 – LILIAN DE GREEF