AVL Trees Reading: 9.2
Balance Factor of a Node The difference in height of its two subtrees (h R -h L ) Balanced Node if -1 BF 1 Unbalanced Node if BF 1 h L h R
Balance Factor of a Binar Tree Corresponds to the balance factor of its root node left-heav if BF -1 equal-heav if BF=0 right-heav if BF 1 h L h R AVL trees BF of each node in {-1,0,1}
AVL Trees An AVL Tree is a binar search tree such that for ever internal node v of T, the heights of the children of v can differ b at most 1. 2 4 44 17 78 1 2 32 50 1 1 48 62 3 88 1 AVL = Adelson - Velskii - Landis An eample of an AVL tree where the heights are shown net to the nodes:
Properties of AVL Trees 1. The height h of an AVL tree T storing n items is O(log 2 n) 2. The number of nodes n in an AVL tree of height h is Ω(2 h ) Search-algorithm compleit is O(log 2 n) If insert/remove destros balance, it must be restored
Insertion into an AVL Tree Insertion is as in a binar search tree Alwas done b epanding an eternal node. Eample: 54 44 44 17 78 17 78 32 50 88 32 50 88 48 62 48 62 54 before insertion after insertion
Trinode Restructuring Restructuring must maintain the binar search tree propert Inorder traversal must produce the same result on both original and restructured tree Tpes of rotation: 1. Single Left Rotation 2. Single Right Rotation 3. Double Left-Right Rotation 4. Double Right-Left Rotation
Trinode Restructuring Perform the rotations needed to make the topmost node of the three Single Left Rotation: X L Y L X L Y L Z L Z R Z L Z R X L,,Y L,,Z L,,Z R X L,,Y L,,Z L,,Z R
TriNode Restructuring Double Right-Left Rotation X L Y L Y R Z R X L Y L Y R Z R X L Y L Y R Z R X L,,Y L,,Y R,,Z R X L,,Y L,,Y R,,Z R XL,,YL,,YR,,ZR
Reconstruction Required BF=0 BF=1 BF=0 a BF()=1, new node in Z R BF()=1 BF()=2 X L X R Z L Z R b BF()=1, new node in Z L BF()=-1 BF()=2
Reconstruction Required (cont) BF=0 BF=-1 BF=0 c BF()=-1, new node in X L BF()=-1 BF()=2 X L X R Z L Z R d BF()=-1, new node in X R BF()=1 BF()=-2
a)insert Into Right Subtree of Right Child BF=+1 BF=+2 BF=0 BF=0 BF=0 BF=+1 X L X R X L X R 1 Z L Z R Z L Z R 2 N
a)insert Into Right Subtree of Right Child BF=+2 BF=0 BF=0 BF=+1 BF=0 BF=0 Z R X L X R Z L Z R X L X R Z L N 2 N
a)insert Into Right Subtree of Right Child BF=0 BF=0 BF=0 Z R Notes Tree height has decreased Similarl, for case c) we can appl a single-right rotation X L X R Z L N
b)insert Into Left Subtree of Right Child BF=+1 BF=+2 BF=0 BF=0 BF=0 BF=-1 X L X R X L X R 1 Z L Z R Z L Z R 2 N
b)insert Into Left Subtree of Right Child Z R X L X R Z L Z R 2 N X L X R Z L N 2 Single rotation not sufficient, need to detail Z L
b)insert Into Left Subtree of Right Child X L X L 1 Y L Y R Z R Y L Y R Z R 2 N
b)insert Into Left Subtree of Right Child X L X L 2 Y L N Y R Z R Y L N Y R Z R
b)insert Into Left Subtree of Right Child X L Y L Y R X L Y L Y R N Z R N Z R
b)insert Into Left Subtree of Right Child X L Y L Y R N Z R Notes Tree height has decreased Similarl, for case d) we can appl a left-right rotation
Rotation-Based Algorithm if root R of a subtree becomes unbalanced to the right if BF(rightChild(R))=+1 single-left rotation around R if BF(rightChild(R))=-1 single-right rotation around rightchild(r) single-left rotation around R if root R of a subtree becomes unbalanced to the left if BF(leftChild(R))=-1 single-right rotation around R if BF(leftChild(R))=+1 single-left rotation around leftchild(r) single-right rotation around R
Removals from AVL Trees Removal begins as in a binar search tree, which means the node removed will become an empt eternal node. Its parent, w, ma cause an imbalance. Eample: w 17 44 62 w 17 44 62 32 50 78 50 78 48 54 88 48 54 88 before deletion of 32 after deletion
Restructuring after Removal Let be the first unbalanced node encountered while traveling up the tree from w. We restructure the tree to restore balance at. As this restructuring ma upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 44 62 17 62 44 78 50 78 17 50 88 48 54 88 48 54
Restructuring after Removal Let be the first unbalanced node encountered while traveling up the tree from w. Also Let be the child of with the larger height Let be the child of with the larger height. a= 44 w 17 62 b= 50 78 c= 48 54 88
Restructure Algorithm (tetbook) Algorithm restructure() Input: A node of a binar search tree T that has both a parent and a grandparent Output: Tree T restructured b a rotation (either single or double) involving nodes,, and. 1: Let (a, b, c) be an inorder listing of the nodes,, and, and let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 2. Replace the subtree rooted at with a new subtree rooted at b 3. Let a be the left child of b and let T 0, T 1 be the left and right subtrees of a, respectivel. 4. Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel.
Restructure Algorithm - Eample 17 44 50 62 78 48 54 88
Restructure Algorithm - Eample Let (a,b,c) be the inorder traversal of,, and Let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 17 a 44 50 b 62 c 78 T0 48 54 88 T 2 T 3 T1
Restructure Algorithm - Eample Replace the subtree rooted at with a new subtree rooted at b b 62 17 a 44 50 c 78 T0 48 54 88 T 2 T 3 T1
Restructure Algorithm - Eample Let a be the left child of b, and T 0 and T 1 be the left and right subtrees of a, respectivel b a 62 17 T0 78 88 c T 2 T 3 44 50 48 54 T1
Restructure Algorithm - Eample Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel. b a 44 62 c 78 17 50 48 54 88 T 2 T 3 T0 T1
Restructure Algorithm - Eample 17 44 50 62 78 48 54 88
Restructure Algorithm - Eample Let (a,b,c) be the inorder traversal of,, and Let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 17 a 44 b 50 c 62 78 T0 48 54 88 T1 T 2 T3
Restructure Algorithm - Eample Replace the subtree rooted at with a new subtree rooted at b b 50 17 a 44 62 c 78 T0 48 54 88 T1 T 2 T3
Restructure Algorithm - Eample Let a be the left child of b, and T 0 and T 1 be the left and right subtrees of a, respectivel a 44 b 50 62 c 78 T0 17 48 T1 54 88 T 2 T3
Restructure Algorithm - Eample Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel. 17 a 44 48 b 50 54 62 c 78 88 T0 T1 T 2 T3
Restructure vs. Rotations Advantages No case analsis More elegant General Disadvantage Mode comple to implement Same time compleit
Implementation, Running Times for AVL Trees Linked structures are better, restructure of arra-based implementation ver costl A single restructure is O(1) using a linked-structure binar tree Find is O(log n) height of tree is O(log n), no restructures needed Insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) Remove is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)
AVL Trees - Questions 1. Assume the following tree is a standard BST sorted leicographicall. Draw this tree after the insertion of a new node with element R. 2. Assume the tree is an AVL tree. Draw this tree after the insertion of a new node with element R. 3. Assume the tree is an AVL tree. Draw this tree after the insertion of a new node with element T. B G S P Y
AVL Trees - Eercise Restore the balance of the given AVL tree 4 2 7 1 3 6 15 5 14 16 13
AVL Trees - Eercise 4 1. Identif the newl inserted node that caused the imbalance 1 2 3 6 5 7 13 14 15 16 2. Check BF of all nodes from this node up 3. Identif the lowest unbalanced node, perform rotations/restructuring around that node
AVL Trees - Eercise Restore the balance of the given AVL tree 7 4 13 2 6 11 15 1 3 5 10 14 16 8 16
Spla Trees
Spla Trees Spla trees are binar search trees Entries stored at internal nodes onl Kes stores at nodes in the left subtree of v are less than or equal to the ke stored at v Kes stored at nodes in the right subtree of v are greater than or equal to the ke stored at v Inorder traversal will return the kes in order Similar operations to BSTs
Rotations after Ever Operation new operation: spla splaing moves a node to the root using rotations right rotation makes the left child of a node into s parent; becomes the right child of left rotation makes the right child of a node into s parent; becomes the left child of a right rotation about a left rotation about T 3 T 1 T 1 T 2 T 1 T 2 T 3 T 3 (structure of tree above is not modified) T 2 T 3 (structure of tree above is not modified) T 1 T 2
Spla Tree - Definition a spla tree is a binar search tree where a node is splaed after it is accessed (for a search or update) deepest internal node accessed is splaed splaing costs O(h), where h is height of the tree which is still O(n) worst-case O(h) rotations, each of which is O(1) Amortied cost of spla O(log(n))
Splaing Eample search for =8 p (20,Z) (10,A) g (14,J) (7,T) (1,Q) (8,N) (21,O) (35,R) (36,L) (37,P) (40,X) 1. (before rotating) (1,C) (5,H) (7,P) (10,U) (20,Z) (2,R) (5,I) (5,G) (6,Y) g (10,A) (35,R) (20,Z) p (7,T) (8,N) (10,U) (14,J) (21,O) (36,L) (37,P) (40,X) p (7,T) (8,N) (10,A) g (21,O) (35,R) (37,P) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) 2. (after first rotation) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) (10,U) (14,J) (36,L) 3. (after second rotation) (40,X) is not et the root, so we spla again
Splaing Eample search for =8 (20,Z) now is the left child of the root right-rotate around root (8,N) (35,R) (7,T) (10,A) (21,O) (37,P) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) (10,U) (14,J) (36,L) 1. (before appling rotation) (40,X) (1,C) (1,Q) (7,T) (5,H) (7,P) (8,N) (10,A) (14,J) (20,Z) 2. (after rotation) (21,O) (35,R) (37,P) (2,R) (5,G) (10,U) (36,L) (40,X) (5,I) (6,Y) is the root, so stop
Spla Trees - Conclusions Tree ma not be balanced Amortied cost of splaing is log(n) Adapts to perform searches on frequentlrequested items much faster than O(log(n))