Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees Chapter 11
Which STL Container to Use?
Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees 11.1, pgs. 624-628
4 Critically Unbalanced Trees Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child
AVL Tree Example Build an AVL tree from the words: Self-Balancing Search Trees Build an AVL tree from the words: "The quick brown fox jumps over the lazy dog"
AVL Tree Example The Insert The Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The Insert The
AVL Tree Example The +1 quick Insert quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +1 Insert quick quick
AVL Tree Example The +2 -1 quick brown Insert brown Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +2 -1 Insert brown quick brown The overall tree is right-heavy (Right-Left) 1. Rotate right around the child (quick)
AVL Tree Example The brown +2 +1 quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The brown +2 +1 quick Rotate right around the child (quick) Rotate left around the parent (The)
AVL Tree Example brown quick The Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The Insert fox
AVL Tree Example +1 brown The quick fox Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 brown Insert fox The quick fox
AVL Tree Example brown quick The +1 fox Insert jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +1 Insert jumps fox
The tree is now left-heavy about quick (Left-Right case) AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 -2 +1 brown Insert jumps The quick fox The tree is now left-heavy about quick (Left-Right case) jumps
AVL Tree Example brown quick The +2 -2 fox +1 jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 fox +1 Rotate left around the child (fox) jumps
AVL Tree Example brown quick The +2 -2 jumps -1 fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 jumps -1 Rotate left around the child fox Rotate right around the parent
AVL Tree Example brown jumps The +1 fox quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +1 fox quick
We now have a Right-Right imbalance AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 +1 -1 brown Insert over The jumps fox quick We now have a Right-Right imbalance over
1. Rotate left around the parent AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +2 +1 fox quick -1 1. Rotate left around the parent over
AVL Tree Example jumps quick brown -1 The fox over Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown -1 The fox over Insert the
AVL Tree Example jumps brown quick The fox over the Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps Insert the brown quick The fox over the
AVL Tree Example jumps quick brown The fox over the Insert lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown The fox over the Insert lazy
AVL Tree Example +1 -1 jumps brown quick The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 -1 jumps Insert lazy brown quick The fox over the lazy
AVL Tree Example jumps quick brown +1 -1 The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown +1 -1 The fox over the lazy Insert dog
AVL Tree Example -1 +1 jumps brown quick The fox over the dog lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child -1 +1 jumps Insert dog brown quick The fox over the dog lazy
Insert the following into an AVL tree: 14, 17, 11, 7, 53, 4, 13, 12, 8 Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child
Insert the following into an AVL tree: 14, 17, 11, 7, 53, 4, 13, 12, 8 Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child
Implementing an AVL Tree Self-Balancing Search Trees
Performance of the AVL Tree Self-Balancing Search Trees Since each subtree is kept close to balanced, the AVL has expected O(log n). Each subtree is allowed to be out of balance ±1 so the tree may contain some holes. In the worst case (which is rare) an AVL tree can be 1.44 times the height of a full binary tree that contains the same number of items. Ignoring constants, this still yields O(log n) performance. Empirical tests show that on average log2.n + 0.25 comparisons are required to insert the nth item into an AVL tree – close to insertion into a corresponding complete binary search tree.
Pros and Cons of AVL Trees Self-Balancing Search Trees
Pros and Cons of AVL Trees Self-Balancing Search Trees Argument for AVL trees: Search is O(log n) since AVL trees are always balanced. Insertion and deletions are also O(log n). The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: Difficult to program & debug; more space for balance factor. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(n) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).