140 likes | 443 Views
IPR Tree Example. Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008. Initial IPR tree:. 57. 26. 72. 25. 38. 63. 94. 3. 37. 47. 78. Internal path length: 4x4 + 4x3 + 2x2 + 1 = 33. Next step: insert 1 -->. 57. 26. 72. 25. 94. 3. 78. 63. 37. 47. 38. 1.
E N D
IPR Tree Example Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008
Initial IPR tree: 57 26 72 25 38 63 94 3 37 47 78 Internal path length: 4x4 + 4x3 + 2x2 + 1 = 33 Next step: insert 1 -->
57 26 72 25 94 3 78 63 37 47 38 1 Insert 1 and see if tree can be rebalanced Start with parent of inserted node. Internal path length: 1x5 + 4x4 + 4x3 + 2x2 + 1 = 38 Next step: Find rebalancing case -->
57 26 72 25 94 3 78 63 37 47 38 1 Find rebalancing case 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 -->
57 26 72 25 3 78 63 37 47 38 1 Apply single right rotation 94 Internal path length: 5x4 + 4x3 + 2x2 + 1 = 37 Next step: insert 30 -->
57 26 72 25 38 78 3 63 1 37 47 30 Insert 30 and see if tree can be rebalanced 94 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). 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. Internal path length: 1x5 + 5x4 + 4x3 + 2x2 + 1 = 42 Next step: Insert 32 -->
57 26 72 25 38 3 78 63 1 37 47 32 Insert 32 and see if tree can be rebalanced 94 30 Internal path length: 1x6 + 1x5 + 5x4 + 4x3 + 2x2 + 1 = 48 Next step: Find rebalancing case -->
57 26 72 25 38 3 78 63 1 37 47 32 Find rebalancing case 94 30 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 -->
57 26 72 25 38 3 78 63 1 37 47 Perform double rotation 94 32 30 Internal path length: 2x5 + 5x4 + 4x3 + 2x2 + 1 = 47 Next step: Insert 35 -->
57 26 72 25 38 3 78 63 1 37 47 Insert 35 and see if tree can be rebalanced 94 32 30 35 Internal path length: 1x6 + 2x5 + 5x4 + 4x3 + 2x2 + 1 = 53 Next step: Find rebalancing case -->
57 26 72 25 38 3 78 63 1 37 47 Find rebalancing case 94 32 30 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 -->
57 26 72 25 38 94 3 63 78 1 32 47 30 35 Apply double rotation 37 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.