Download - CSC 213 – Large Scale Programming
Today’s Goal
Discuss AVL Trees Relationship to BinaryTree and BST What an AVL Tree looks like and how they work Effects on big-Oh notation AVL Tree Limitations
Dictionary ADT
Dictionary ADT maps key to 1 or more valuesUsed wherever search is (e.g., everywhere)Implementation using Sequence takes O(n) timeWith good batch of hash, could get O(1) time
But could still end up with O(n) timeIf data are random, BST takes O(log n) time
But for ordered data, BST still takes O(n) time
There must be a better way!
Binary Search Trees
May not be complete But faster when they are
Use different ordering Lower keys in left
subtree Higher keys in right
subtree Equal keys not specified,
but must be consistent
6
92
41 10
AVL Tree Definition
Another type of BST Algorithm guarantees O(log n) time Does not allow tree to
become linked list Keeps tree in balance
AVL Tree balanced when each Node’s children differ in height by at most 1 Maintains balance through
trinode restructuring
Node heights are shown in red
6
92
41 81
1
1
5
2
3 2
4
Trinode Restructuring
Insertion & removal can unbalance tree Trinode restructuring restores tree’s tao
Used when node’s children’s height differ Takes node, taller child, & taller grandchild Grandchild must be taller of taller child’s children
Move median node to root of subtree Other 2 nodes in restructuring become its children
Trinode Restructuring
Case 1: Single rotation (e.g., 3 in a row) Move child of 3 to root of subtree Subtrees become grandchildren of node No change in the order of subtrees
b
a
c
T0
T1
T2 T3
b
a c
T0 T1 T2 T3
Trinode Restructuring
Case 2: Double rotation Taller child & grandchild go opposite ways Move grandchild up to the root Subtrees become grandchildren of node, but
maintain order
c
b
a
T0
T1 T2
T3
b
ca
T0 T1 T2 T3
1
Insertion in an AVL Tree
Begins like normal BST insertion Example: insert(5)
7
92
41 81
2
1
6
2
3 2
4
51
Insertion in an AVL Tree
Travel up tree checking nodes for balance Must increase tao of unbalanced nodes
7
92
41 8
6
5
Insertion in an AVL Tree
Must walk up tree starting at inserted Node Stop once we hit the root node Update height for each Node along path Check if Node’s children are balanced Perform rotations when balance needs restoration
Not all insertions require rebalancing Will need at most 1 insertion per Node But some insertions need multiple rebalancings
1
Removal in an AVL Tree
Start with normal BST removal But removal may cause drop in the TAO
Example: remove(7)7
92
51 8
64
1
1
12
3 2
4
1
8
1
Removal in an AVL Tree
Start with normal BST removal But removal may cause drop in the TAO
Example: remove(7)7
92
51
64
1
1
2
3
4
1
8
Removal in an AVL Tree
Start with normal BST removal But removal may cause drop in the TAO
Example: remove(7)
7
82
41 6
5
9
Removal in an AVL Tree
Again walk up tree checking for balance Update heights as we go
Only examines Nodes along path Height of nodes not on path cannot be changed
May need multiple restructuring operations Uses same restructuring as insert
Use unbalanced node’s taller child & taller grandchild Does not matter if child & grandchild on path
Restructuring for Dummies
Store the 7+1 Nodes in local variables Plus one records parent Node of this subtree
Set all left, right, & parent from pattern Median node is root; subtrees maintain order
c
b
a
T0
T1 T2
T3
b
ca
T0 T1 T2 T3
b
ca
T0 T1 T2 T3
b
a
c
T0
T1
T2 T3
In the Next Lecture
Discuss even faster ways of searching No difference in big-Oh notation, however
But provide significant speedup in real-life Learn new ways of amortizing costs Discover proper use of the term “splay”
When and why we splay a tree What it means to splay Any other excuses I can think of to say splay