IPR Tree Example Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008 Initial IPR tree: 57 26 72 25 3 38 37 63 47 94 78 Internal path length: 4x4 + 4x3 + 2x2 + 1 = 33 Next step: insert 1 --> Insert 1 and see if tree can be rebalanced 57 26 72 25 Start with parent of inserted node. 3 38 37 63 47 94 78 1 Internal path length: 1x5 + 4x4 + 4x3 + 2x2 + 1 = 38 Next step: Find rebalancing case --> Find rebalancing case 57 26 72 25 3 38 37 63 47 94 78 1 We can apply a single right rotation at node 25 since it is ML (more nodes to the left), and the number of nodes in the left subtree of node 3 is 1 and the number of nodes in the right subtree of node 25 is 0, and 1 > 0. Next step: apply single right rotation --> Apply single right rotation 57 26 72 3 1 38 25 37 63 47 94 78 Internal path length: 5x4 + 4x3 + 2x2 + 1 = 37 Next step: insert 30 --> Insert 30 and see if tree can be rebalanced 57 26 72 3 1 38 25 37 63 47 94 78 30 1. Single right rotation at node 38: cannot be applied since the left subtree of 37 and the right subtree of 38 contain the same number of nodes (1). 2. Double rotation at node 26: cannot be applied since the subtrees of 37 have fewer nodes (1) than the left subtree of 26 (3). Internal path length: 1x5 + 5x4 + 4x3 + 2x2 + 1 = 42 3. Double rotation at node 57: cannot be applied since the subtrees of 38 contain fewer nodes (3) than the right subtree of 57 (4). So: tree cannot be rebalanced. Next step: Insert 32 --> Insert 32 and see if tree can be rebalanced 57 26 72 3 1 38 25 37 63 47 94 78 30 32 Internal path length: 1x6 + 1x5 + 5x4 + 4x3 + 2x2 + 1 = 48 Next step: Find rebalancing case --> Find rebalancing case 57 26 72 3 1 38 25 37 63 47 94 78 30 32 We can apply a double rotation since the number of nodes in the right subtree of node 30 is 1 and the number of nodes in the right subtree of node 37 is 0, and 1 > 0. Next step: Perform double rotation --> Perform double rotation 57 26 72 3 1 38 32 25 30 63 47 94 78 37 Internal path length: 2x5 + 5x4 + 4x3 + 2x2 + 1 = 47 Next step: Insert 35 --> Insert 35 and see if tree can be rebalanced 57 26 72 3 1 38 32 25 30 63 47 94 78 37 35 Internal path length: 1x6 + 2x5 + 5x4 + 4x3 + 2x2 + 1 = 53 Next step: Find rebalancing case --> Find rebalancing case 57 26 72 3 1 38 32 25 30 63 47 94 78 37 35 We can apply a double rotation since the number of nodes in the right subtree of node 32 is 2 and the number of nodes in the right subtree of node 38 is 1, and 2 > 1. Next step: Apply double rotation --> Apply double rotation 57 26 72 3 1 37 25 32 30 63 38 35 94 78 47 Internal path length: 3x5 + 5x4 + 4x3 + 2x2 + 1 = 52 Next step: Notes --> Notes • When looking for possible rotations, always start at the parent of the just-inserted node, see if it meets the criteria for any of the 4 cases of rotations, and if not, try its parent. • If you have a choice between a single rotation and a double rotation, do a single rotation since it is simpler. • Note that after every rotation done in this example, the internal path length was reduced. • Note that the results of this example do not differ from those of the AVL example. • As an exercise, try inserting some more nodes into this tree.