Ch04 Balanced Search Trees
Total Page:16
File Type:pdf, Size:1020Kb
Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch04 Balanced Search Trees 6 v 3 8 z 4 1 1 Why care about advanced implementations? Same entries, different insertion sequence: Not good! Would like to keep tree balanced. 2 1 Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, and red-black tree. 3 Approaches to balancing trees Don't balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Pretty good balance Only allow a little out of balance Adjust on access Self-adjusting 4 4 2 Balancing Search Trees Many algorithms exist for keeping search trees balanced Adelson-Velskii and Landis (AVL) trees (height-balanced trees) Red-black trees (black nodes balanced trees) Splay trees and other self-adjusting trees B-trees and other multiway search trees 5 5 Perfect Balance Want a complete tree after every operation Each level of the tree is full except possibly in the bottom right This is expensive For example, insert 2 and then rebuild as a complete tree 6 5 Insert 2 & 4 9 complete tree 2 8 1 5 8 1 4 6 9 6 6 3 AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node 7 7 Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basic case: N(0) = 1, N(1) = 2 Inductive case: h N(h) = N(h-1) + N(h-2) + 1 Theorem (from Fibonacci analysis) h N(h) > where 1.618, the golden ratio. h-2 h-1 8 8 4 Height of an AVL Tree h N(h) > (1.618) Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) h n > hence log n > h (relatively well balanced tree!!) h < 1.44 log2n (i.e., Find takes O(logn)) 9 9 Node Heights Tree A (AVL) Tree B (AVL) height=2 BF=1-0=1 2 6 6 1 0 1 1 4 9 4 9 0 0 0 0 0 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1 10 10 5 Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) 2 3 balance factor 1-(-1) = 2 6 6 1 1 1 2 4 9 4 9 0 0 0 0 0 1 -1 1 5 8 1 5 8 0 7 height of node = h balance factor = hleft-hright empty height = -1 11 11 Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft- hright) is 2 or –2, adjust tree by rotation around the node 12 12 6 Single Rotation in an AVL Tree 2 2 6 6 1 2 1 1 4 9 4 8 0 0 1 0 0 0 0 1 5 8 1 5 7 9 0 7 13 13 Insertions in AVL Trees Let the node that needs rebalancing be . Cases: 1 3 4 2 There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . (left-left) 2. Insertion into right subtree of right child of . (right-right) Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . (left-right) 4. Insertion into left subtree of right child of . (right-left) The rebalancing is performed through four separate rotation algorithms. 14 14 7 AVL Insertion: Outside Case h+2 Consider a valid AVL subtree j h+1 k h h h Z X Y Differ by 1 15 15 AVL Insertion: Outside Case j Inserting into X destroys the AVL h+2 property at node j k h h+1 h Z Y X 16 16 8 AVL Insertion: Outside Case j Do a “rotation to right” k h h+1 h Z Y X 17 17 Single right rotation j Do a “right rotation” k h h+1 h Z Y X 18 18 9 Outside Case Completed “Right rotation” done! k (“Left rotation” is mirror symmetric) j h+1 h h X Y Z AVL property has been restored! 19 19 AVL Insertion: Inside Case Consider a valid AVL subtree j k h h h Z X Y 20 20 10 AVL Insertion: Inside Case Inserting into Y Does “right rotation” destroys the j restore balance? AVL property at node j k h h h+1 Z X Y 21 21 AVL Insertion: Inside Case “One rotation” k does not restore balance… now k is h j out of balance X h+1 h Z Y 22 22 11 AVL Insertion: Inside Case Consider the structure of subtree Y… j k h h h+1 Z X Y 23 23 AVL Insertion: Inside Case Y = node i and j subtrees V and W k h h i h+1 Z X h or h-1 V W 24 24 12 AVL Insertion: Inside Case j We will do a left-right “double rotation” . k i Z X V W 25 25 Double rotation : first rotation j left rotation complete i k Z W X V 26 26 13 Double rotation : second rotation j Now do a right rotation i k Z W X V 27 27 Double rotation : second rotation right rotation complete Balance has been i restored k j h h or h-1 h X V W Z 28 28 14 Implementation Class BinaryNode KeyType: Key height int: Height key BinaryNode: LeftChild BinaryNode: RightChild left right Constructor(KeyType: key) Key = key Once you have Height = 0 performed a End Constructor rotation (single or End Class double) you won’t need to go back up the tree 29 29 Java-like Pseudo-Code rotateToRight( BinaryNode: x ) { BinaryNode y = x.LeftChild; x.LeftChild = y.RightChild; y.RightChild = x; return y; } 30 15 Java-like Pseudo-Code rotateToLeft( BinaryNode: x ) { BinaryNode y = x.rightChild; x.rightChild = y.leftChild; y.leftChild = x; return y; } 31 Double Rotation Implement Double Rotation in two lines. n DoubleRotateToLeft(n : binaryNode) { rotateToRight(n.rightChild); rotateToLeft(n); } X DoubleRotateToRight(n : binaryNode) { Z rotateToLeft(n.leftChild); rotateToRight(n); V W } 32 32 16 Insertion in AVL Trees Insert at the leaf (as for all BST) only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft- hright) is 2 or –2, adjust tree by rotation around the node 33 33 Insert in ordinary BST Algorithm insert(k, v) input: insert key k into the tree rooted by v output: the tree root with k adding to v. if isNull (v) return newNode(k) if k ≤ key(v) // duplicate keys are okay leftChild(v) insert (k, leftChild(v)) else if k key(v) rightChild(v) insert (k, rightChild(v)) return v 34 34 17 Insert in AVL trees Insert(v : binaryNode, x : element) : { if v = null then {v new node; v.data x; height 0;} else case v.data = x : ; //Duplicate do nothing v.data > x : v.leftChild Insert(v.leftChild, x); // handle left-right and left-left cases if ((height(v.leftChild)- height(v.rightChild)) = 2)then if (v.leftChild.data > x ) then //outside case v = RotateToRight (v); else //inside case v = DoubleRotateToRightt (v);} v.data < x : v.righChild Insert(v.rightChild, x); // handle right-right and right-left cases … … Endcase v.height max(height(v.left),height(v.right)) +1; return v; } 35 35 Example of Insertions in an AVL Tree 2 20 0 1 Insert 5, 40 10 30 0 0 25 35 36 36 18 Example of Insertions in an AVL Tree 2 3 20 20 1 1 1 2 10 30 10 30 0 0 0 0 0 1 5 25 35 5 25 35 0 40 Now Insert 45 37 37 Single rotation (outside case) 3 3 20 20 1 2 1 2 10 30 10 30 0 2 0 0 0 5 25 35 5 25 40 1 00 40 35 45 Imbalan 1 ce 0 45 Now Insert 34 38 38 19 Double rotation (inside case) 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 5 Imbalance 25 40 5 30 40 1 0 1 35 45 0 0 25 34 45 Insertion of 34 0 34 39 39 In Class Exercises Build an AVL tree with the following values: 15, 20, 24, 10, 13, 7, 30, 36, 25 40 20 15, 20, 24, 10, 13, 7, 30, 36, 25 20 15 15 24 20 10 24 13 20 20 13 24 15 24 10 15 13 10 41 15, 20, 24, 10, 13, 7, 30, 36, 25 20 13 13 24 10 20 10 15 7 15 24 7 30 13 36 10 20 7 15 30 24 36 42 21 15, 20, 24, 10, 13, 7, 30, 36, 25 13 13 10 20 10 20 7 15 30 7 15 24 24 36 30 25 13 25 36 10 24 7 20 30 15 25 36 43 Possible Quiz Questions Build an AVL tree by inserting the following values in the given order: 1, 2, 3, 4, 5, 6.