Data Structure and Algorithms [CO2003] Chapter 7 - AVL Tree Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents 1. AVL Tree Concepts 2. AVL Tree Operations 4.
B-Trees Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 1 / 76 Outcomes • L.1 - Depict the following concepts: binary tree, complete binary tree, balanced binary tree, AVL tree, multi-way tree, etc.2 - Describe the strorage structure for tree structures using pseudocode.3 - List necessary methods supplied for tree structures, and describe them using pseudocode.4 - Identify the importance of “blanced” feature in tree structures and give examples to demonstate it.5 - Identiy cases in which AVL tree and B-tree are unblanced, and demonstrate methods to resolve all the cases step-by-step using figures. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 2 / 76 Outcomes • L.6 - Implement binary tree and AVL tree using C/C++.7 - Use binary tree and AVL tree to solve problems in real-life, especially related to searching techniques.8 - Analyze the complexity and develop experiment (program) to evaluate methods supplied for tree structures.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs.2 - Analyze algorithms and use Big-O notation to characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion).
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 3 / 76 AVL Tree Concepts AVL Tree Definition AVL Tree is: • A Binary Search Tree, • in which the heights of the left and right subtrees of the root differ by at most 1, and • the left and right subtrees are again AVL trees.Adel’son-Vel’skii and E. AVL Tree is a Binary Search Tree that is balanced tree. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 4 / 76 AVL Tree A binary tree is an AVL Tree if • Each node satisfies BST property: key of the node is greater than the key of each node in its left subtree and is smaller than or equals to the key of each node in its right subtree. • Each node satisfies balanced tree property: the difference between the heights of the left subtree and right subtree of the node does not exceed one. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 5 / 76 AVL Tree Balance factor • left_higher (LH): HL = HR + 1 • equal_height (EH): HL = HR • right_higher (RH): HR = HL + 1 (HL , HR : the heights of left and right subtrees) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 6 / 76 AVL Trees 8 8 8 8 5 5 10 5 10 9 3 6 12 8 5 10 3 6 9 1 4 5 7 Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 7 / 76 Non-AVL Trees 8 8 5 10 3 9 12 8 8 5 10 5 10 12 3 12 15 1 15 Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 8 / 76 Why AVL Trees? • When data elements are inserted in a BST in sorted order: 1, 2, 3,. BST becomes a degenerate tree.
Search operation takes O(n), which is inefficient. • It is possible that after a number of insert and delete operations, a binary tree may become unbalanced and inscrease in height. • AVL trees ensure that the complexity of search is O(log2 n). Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 9 / 76 AVL Balance Balancing Trees • When we insert a node into a tree or delete a node from a tree, the resulting tree may be unbalanced. → rebalance the tree. • Four unbalanced tree cases: • left of left: a subtree of a tree that is left high has also become left high; • right of right: a subtree of a tree that is right high has also become right high; • right of left: a subtree of a tree that is left high has become right high; • left of right: a subtree of a tree that is right high has become left high; Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 10 / 76 Unbalanced tree cases (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 11 / 76 Unbalanced tree cases (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 12 / 76 Rotate Right Algorithm rotateRight(ref root <pointer>) Exchanges pointers to rotate the tree right. Pre: root is pointer to tree to be rotated Post: node rotated and root updated tempPtr = root->left root->left = tempPtr->right tempPtr->right = root Return tempPtr End rotateRight Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 13 / 76 Rotate Right (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 14 / 76 Rotate Left Algorithm rotateLeft(ref root <pointer>) Exchanges pointers to rotate the tree left. Pre: root is pointer to tree to be rotated Post: node rotated and root updated tempPtr = root->right root->right = tempPtr->left tempPtr->left = root Return tempPtr End rotateLeft Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 15 / 76 Balancing Trees - Case 1: Left of Left Out of balance condition created by a left high subtree of a left high tree → balance the tree by rotating the out of balance node to the right. (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 16 / 76 Balancing Trees - Case 1: Left of Left (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 17 / 76 Balancing Trees - Case 2: Right of Right Out of balance condition created by a right high subtree of a right high tree → balance the tree by rotating the out of balance node to the left. (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 18 / 76 Balancing Trees - Case 2: Right of Right (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 19 / 76 Balancing Trees - Case 3: Right of Left Out of balance condition created by a right high subtree of a left high tree → balance the tree by two steps: 1. rotating the left subtree to the left; 2. rotating the root to the right. (Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 20 / 76 Balancing Trees - Case 3: Right of Left Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 21 / 76 Balancing Trees - Case 4: Left of Right Out of balance condition created by a left high subtree of a right high tree → balance the tree by two steps: 1. rotating the right subtree to the right; 2. rotating the root to the left.
(Source: Data Structures - A Pseudocode Approach with C++) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 22 / 76 Balancing Trees - Case 4: Left of Right Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 23 / 76 AVL Tree Operations AVL Tree Structure node // G e n e r a l dataTye : d a t a <dataType> dataType l e f t <p o i n t e r > k e y <keyType> r i g h t <p o i n t e r > f i e l d 1 <. > avlTree end dataType r o o t <p o i n t e r > end a v l T r e e Note: Array is not suitable for AVL Tree.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 24 / 76 AVL Tree Operations • Search and retrieval are the same for any binary tree. • AVL Insert • AVL Delete Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 25 / 76 AVL Insert • Insert can make an out of balance condition.
• Otherwise, some inserts can make an automatic balancing. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 26 / 76 AVL Insert Algorithm Algorithm AVLInsert(ref root <pointer>, val newPtr <pointer>, ref taller <boolean>) Using recursion, insert a node into an AVL tree. Pre: root is a pointer to first node in AVL tree/subtree newPtr is a pointer to new node to be inserted Post: taller is a Boolean: true indicating the subtree height has increased, false indicating same height Return root returned recursively up the tree Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 27 / 76 AVL Insert Algorithm // Insert at root if root null then root = newPtr taller = true return root end Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 28 / 76 AVL Insert Algorithm if newPtr->data.key < root->data.key then root->left = AVLInsert(root->left, newPtr, taller) // Left subtree is taller if taller then if root is LH then root = leftBalance(root, taller) else if root is EH then root->balance = LH else root->balance = EH taller = false end end Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 29 / 76 AVL Insert Algorithm else root->right = AVLInsert(root->right, newPtr, taller) // Right subtree is taller if taller then if root is LH then root->balance = EH taller = false else if root is EH then root->balance = RH else root = rightBalance(root, taller) end end end return root End Lecturer: Duc AVLInsert Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 30 / 76 AVL Left Balance Algorithm Algorithm leftBalance(ref root <pointer>, ref taller <boolean>) This algorithm is entered when the left subtree is higher than the right subtree.
Pre: root is a pointer to the root of the [sub]tree taller is true Post: root has been updated (if necessary) taller has been updated Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 31 / 76 AVL Left Balance Algorithm leftTree = root->left // Case 1: Left of left. Single rotation right. if leftTree is LH then root = rotateRight(root) root->balance = EH leftTree->balance = EH taller = false Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 32 / 76 AVL Left Balance Algorithm else rightTree = leftTree->right if rightTree->balance = LH then root->balance = RH leftTree->balance = EH else if rightTree->balance = EH then leftTree->balance = EH else root->balance = EH leftTree->balance = LH end rightTree->balance = EH root->left = rotateLeft(leftTree) root = rotateRight(root), taller = false end return root Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 33 / 76 AVL Right Balance Algorithm Algorithm rightBalance(ref root <pointer>, ref taller <boolean>) This algorithm is entered when the right subtree is higher than the left subtree. Pre: root is a pointer to the root of the [sub]tree taller is true Post: root has been updated (if necessary) taller has been updated Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Data Structure and Algorithms [CO2003] 34 / 76 AVL Right Balance Algorithm rightTree = root->right // Case 1: Right of right.
Single rotation left.