Balancing Trees & AVL Balancing Trees One of the disadvantages of the binary search tree is that it can be skewed and its height can reach n-1 Which means that the time needed to perform insertion and deletion and many operations may reach O(n) in the worst case Now, we need a tree with a smaller height The least height of a binary tree with n nodes is O(log n) Why do we use big O notation to represent the height of a BST? In a Binary Search Tree (BST): Search, Insert, and Delete all take time proportional to the tree's height So, if the height is O(log n) → all operations are efficient If the height is O(n) (like in an unbalanced tree) → operations become slow So analyzing height in Big O gives us a direct way to talk about the performance of these operations. Now, our goal is to keep the height of a BST O(log n) These trees are called balanced BSTs AVL → named after Adel’son-Vel’skii and Landis red-black tree Balanced Tree Is a tree who has the difference between heights of both subtrees in any node in the tree is either zero or one The difference in height here between the two subtrees of the root node is 1 The difference in height between the two subtrees of node 10 is zero Balancing Trees & AVL 1 Perfectly Balanced Tree Is a balanced tree with its leaves on the same level → Difference in height between the two subtrees is zero The difference between the heights of the two subtrees of the root node is zero Also, the difference between the heights of the subtrees of any other node is zero Balancing Techniques 1. Continuously restructure the tree when new elements arrive that cause imbalance in the tree → So, the tree is always kept balanced as done in AVL and DSW 2. Reorder the data and then build the tree again so that it is balanced (not preferred, don’t use) AVL Trees It is a tree in which the difference in the height between the two subtrees if any node is not more than 1 After every insertion or deletion, we rebalance the tree No guarantee that the tree is perfectly balanced → it may be balanced or perfectly balanced In AVL, The height of a leaf is one The height of a node is the maximum height of its subtrees + 1 → The height of the longest subtree + the the height of the node itself which is one The height of a null pointer is zero → Every node has two children as we know, so if it has no children, the pointers pointing to the left and right child their height is zero as they are null AVL is a binary search tree, so same rules of insertion and deletion applies to it AVL Tree Balancing After each insertion or deletion, we check the balance factor of the tree The balance factor is the difference between the two subtrees of a node If the balance factor is more than one, then the tree needs rebalance In AVL, we have four types of rotations to rebalance the tree Right Rotation Left Rotation Right Left Rotation Left Right Rotation Balancing Trees & AVL 2 → We will see examples of when to use each of them now Steps of Balancing the Tree First, we insert the node as we do in BST Then, we go up to check the balance factor from the node we inserted till the root of the tree If we found the balance factor more than 1, then we have to rebalance doing one of the rotations we previously mentioned Left Rotation Insert the following values into an AVL Tree: 1, 2, 3, 4, 5 1. insert 1, check the balance 2. insert 2, check the balance 1. insert 3, check the balance The tree is balanced as the The balance factor of the The balance factor of the difference between the node we inserted is 0 node we inserted is 0 heights of the two subtrees of The balance factor of its The balance factor of its parent is 1 parent is 1 So. the tree is balanced The balance factor of the root the root is 0 is 2 The tree is unbalanced! → we have to rebalance In this case, the tree is skewed to the right, so we have to make a left rotation to balance the tree We make the rotation around the unbalanced node, in this case → node 1 Now, the tree is balanced → In AVL, once you balance a node you don’t have to continue checking the rest of the nodes as the tree is always rebalancing Balancing Trees & AVL 3 4. insert 4, check the balance 5. insert 5, check the balance The parent of the node we inserted (3) has The balance factor of the parent of the node we balance factor = 1 inserted (4) = 1 The root node has balance factor = 1 The balance factor of its parent (3) is 2 → We The tree is balanced have a problem here, rebalance needed, don’t proceed to the root until you rebalance this part Since the problem is in node 3 and it is in the right subtree in the right child (right, right) so, we are doing left rotation around 3 Now the tree is balanced and we finished Insert the following values into an AVL tree: 3, 5, 12, 10, 15, 16 1. insert 3, 5 and the tree would still be balanced as the previous example 2. Then insert 12 and then the problem will arise going up the way from 12, 5 is balanced but 3 have a problem Balancing Trees & AVL 4 The problem comes from a right right node, so a left rotation is needed Now, the tree is balanced, we can continue 3. insert 10, check the balance 4. insert 15, check the balance, same case as 10, the tree is still balanced going all the way up from 10 (the node we inserted), all the nodes are balanced then the tree is still balanced 5. insert 16. check the balance Balancing Trees & AVL 5 Going all the way up from 16: 15 is balanced, 12 is balanced but we have a problem at 5 it is unbalanced as the balance factor is 3-1 = 2 The problem comes from the right subtree containing (15, 16) of the right subtree with root (12) of the main root (5) So, we are doing a left rotation around the node that has the problem → node 5 But we have a little problem, when we rotate 5, 12 will become in its place and 5 will be its left child, what about 10? First, we will disconnect 10, do the rotation, then place 10 in its suitable place in the tree Now, connect 10 to its suitable place in the tree → right child of 5 Now, the tree is balanced and we finished! Note that: Disconnecting 10 and then reconnecting it will then be in one step, I just separated them to be more clear here ❤ Balancing Trees & AVL 6 Left Right Rotation Insert the following values into an AVL tree: 30, 15, 18, 10, 16, 11 1. insert 30, 15, check balance, tree is balanced 2. insert 18, check the balance Going the way up from 18, 15 is balanced but 30 is not The problem comes from a right child to a left subtree of the node having the problem, so it is left right problem So, we are doing a left rotation then a right rotation The left rotation will be done on the 15 to change the problem to be left left → left left problems requires right rotation as the left rotation we previously did The left rotation is done, now with the right rotation Here the problem is at node 30, arising from a left left node, so we are doing a right rotation around 30 Balancing Trees & AVL 7 Now, the tree is balanced, we can continue 3. insert 10, check the balance Going all the way up from 10, 15 is balanced, 18 is balanced, so the tree is balanced 4. insert 16, check the balance Going all the way up from 10, 15 is balanced, 18 is balanced, so the tree is still balanced 5. insert 11, check the balance Going the way up from 11, we find that 10 is balanced, 15 is balanced but 18 is not The cause of the problem is the left subtree of the left subtree of the root, so it is a left left problem that requires a right rotation Balancing Trees & AVL 8 The right rotation will be around 18, notice the change in the place of the 16 node Now, the tree is balanced and we finished! To sum up, the rotation happens around the node having a balance factor more than 1 Left Left problem → Right rotation Right Right problem → Left rotation Left Right problem → Left Right rotation left rotation first to make it look like left left problems then right rotation we do the first (left) rotation on the child of the node having the problem and the second (right) on the node itself Right Left problem → Right Left rotation right rotation first to make it look like right right problems then left rotation we do the first (right) rotation on the child of the node having the problem and the second (left) on the node itself → The slides of the doctor has 2 detailed problems with all possible rotations and cases, In the upcoming pages I will solve the first problem with detailed explanation and the second problem I will solve it as the doctor said we should solve in the exam paper, TRY BY YOURSELF DON’T JUST READ!! Balancing Trees & AVL 9 Example 1 Insert 14, 17, 11, 7, 53, 4, 13, 12, 8 Then remove 53, 11, 8 1. insert 14, 17, 11, no problem will happen 2. insert 7, 53 check balance Going all the way up from 7, 11 is balanced, 14 is balanced, no problem tree is balanced Then after inserting 53, going all the way up from 53, 17 is balanced, 14 is balanced, tree is still balanced 3. insert 4, check balance Going all the way up from 4, 7 is balanced, but 11 has a problem Solve the problem at 11 before proceeding to the root (That’s why I didn’t change heights at the root) The problem at 11 is a left left problem, so we are doing a right rotation around 11 Balancing Trees & AVL 10 4. insert 13, check balance 5. insert 12, check balance Going all the way up from 13, 11 is balanced, 7 is balanced, 14 is balanced, so the tree is balanced we have no problem Going all the way up from 12, 13 is balanced but 11 is not, stop here, solve the problem The problem is a right left problem, so we are doing right left rotation First, right rotation around 13 to make it right right problem Balancing Trees & AVL 11 Then, left rotation around 11 Now, the tree is balanced 6. insert 8, check the balance Going all the way up from 8, 11 is balanced, 12 is balanced, but 7 is not, stop here, solve the problem The problem comes from a child in the left subtree of the right subtree of 7 → 8 is child in the left subtree of 12, 12 is the root of the right subtree of 7 So, it is a right left problem, we are doing right left rotation First, right rotation around 12 Balancing Trees & AVL 12 Then, left rotation around 7, notice the change in the place of 8 after rotation Now, the tree is balanced, we finished insertion, let’s go to deletion 7. remove 53, check the balance So, here we have a problem at node 14, the balance factor is 2 The problem is arising from the left subtree of 11 because it gives a longer left path to the root, so it is a left left problem, needs a right rotation around 14 Now, 11 is going to be in 14’s place and then 14 will be its right child But what about the right subtree of 11, we will connect it as a left child to 14 after rotation Now, the tree is balanced Balancing Trees & AVL 13 8. remove 11, check balance 11 has two children, so we will find its predecessor which is the greatest element in the left subtree which is 8 Copy the value of 8 to the node containing 11, then remove the node containing 8 Then the tree will look like this The tree is still balanced, we have no problem 9. remove 8, check the balance As we did with 11, 8 has two children, so we will find its predecessor which is the greatest element in the left subtree which is 7 Copy the value of 7 to the node containing 8, then remove the node containing 7 the old 7 has one child, so we will connect it to the root → If you have problems with deletion, check the deleting nodes section here: Binary Search Tree Then the tree will look like this Now, the root node (7) is imbalanced, the problem is coming from the left subtree of the right subtree of the root Balancing Trees & AVL 14 Node 13 is in the left subtree of node 14 which is the root node of the right subtree of 7 So, the problem is right left so we need a right left rotation First, right rotation around 14 → The child of the node having the problem Then left rotation around 7 → The node having the problem Now, our tree is balanced and we finished the problem finallyyyyyyyyy! Example 2 → In this example, I will not be explaining as I want you to see how the doctor want us to solve a problem like this in the exam paper, he said, in each step draw the current tree and the insert or delete and the do the rotation all beside each other and write what type of rotation you are doing, then draw a line and do the next step and so on (some steps will look small so kindly zoom in, also note that there is a misspelling, it is unbalanced not imbalanced) Insert 15, 20, 24, 10, 13, 7, 30, 36, 25 to an empty AVL Then remove 24 and 20 Balanced Balancing Trees & AVL 15 Balancing Trees & AVL 16 Complexity of AVL Tree Average Worst Space Complexity O(n) O(n) Search O(log n) O(log n) Insert O(log n) O(log n) Delete O(log n) O(log n) By: Malak Amr Balancing Trees & AVL 17
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )