CS 240 Fall 2015
Mike Lam, Professor
Balanced (AVL) Trees
I find your lack of balance disturbing.
CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance - - PowerPoint PPT Presentation
CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance disturbing. Balanced (AVL) Trees Review Binary Search Trees (BSTs) Ordered binary tree Operations are O(h) where h is the height of the tree For mostly-random
Mike Lam, Professor
I find your lack of balance disturbing.
– Ordered binary tree – Operations are O(h) where h is the height of the tree – For mostly-random insertions and deletions, h ≈ log n
– For other situations, we need to use a more "balanced"
– How often? How many nodes? How strict? – How do we measure balance?
– This would lead to O(n log n) insertion times – Essentially re-build the tree every time – This seems quite excessive!
– AVL trees (easiest to understand) – Red-Black trees – Many others...
– Inventors G. M. Adelson-Velsky and E. M. Landis (1962) – Balance factor: height(node->left) – height(node->right) – Height-balance property
– Enforce this property using tree rotations
Image from: http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/
– Minor extension: height of an empty tree will be -1 – Applies to whole trees as well as subtrees
Caveat: Heights can't really be negative (what would it mean to have negative edges?), but it does make the math work better...
44 17 8 32 28 29 88 65 97 93 54 82 76 80
44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0
44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1
44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1 h=-1 h=-1
44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1
– Need to re-arrange tree
– Need to re-arrange tree
– Single rotation (below) – Single/double rotations (right)
x x y y
x->right = y->left y->left = x y->left = x->right x->right = y
"right rotation" "left rotation"
– Insert into BST as usual
– Check ancestors of new node for imbalances – Fix imbalances via trinode restructuring
– Remove from BST as usual – Check ancestors of removed node for imbalances – Fix imbalances via trinode restructuring
44 17 32 78 50 88 48 62
h=0 h=0 h=0 h=0 h=1 h=1 h=2 h=3
44 17 32 78 50 88 48 62
h=0 h=0 h=0 h=0 h=1 h=1 h=2 h=3 54 h=0
44 17 32 78 50 88 48 62
h=0 h=1 h=0 h=0 h=1 h=1 h=2 h=3 54 h=0
44 17 32 78 50 88 48 62
h=0 h=1 h=0 h=0 h=2 h=1 h=2 h=3 54 h=0
44 17 32 78 50 88 48 62
h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0
44 17 32 78 50 88 48 62
h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0
44 17 32 78 50 88 48 62
h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0
44 17 32 78 62 88 50 54
h=1 h=0 h=0 h=0 h=2 h=1 h=3 h=4 48 h=0
44 17 32 62 50 78 48 54
h=0 h=0 h=0 h=1 h=1 h=1 h=2 h=3 88 h=0
44 17 32 62 50 78 48 54
h=0 h=0 h=0 h=1 h=1 h=1 h=2 h=3 88 h=0
– Mark rotations as L or R
To check your answers:
http://www.qmatica.com/DataStructures/Trees/AVL/AVLTree.html
– n(h) = number of nodes in AVL tree of height h – Reasoning: AVL tree with minimal number of nodes has one
skip heights)
– n(h) >= Fib(h) = (Φ = golden ratio) – Exponential w.r.t. height: n(h) is Ω(Φh) – Thus, h is O(log n)
Φ
h
√ 5⌋
– Root is colored black – All children of a red node must be colored black (no "double reds") – All nodes with zero or one children have the same number of black-colored ancestors
root to nearest leaf