Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008

Similar presentations


Presentation on theme: "Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008"— Presentation transcript:

1 Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008
IPR Tree Example Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008

2 Next step: insert 1 -->
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 -->

3 Insert 1 and see if tree can be rebalanced
3 25 26 57 72 94 78 63 37 47 38 1 Start with parent of inserted node. Internal path length: 1x5 + 4x4 + 4x3 + 2x2 + 1 = 38 Next step: Find rebalancing case -->

4 Next step: apply single right rotation -->
Find rebalancing case 3 25 26 57 72 94 78 63 37 47 38 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 -->

5 Apply single right rotation
57 26 72 3 38 63 94 1 25 37 47 78 Internal path length: 5x4 + 4x3 + 2x2 + 1 = 37 Next step: insert 30 -->

6 Insert 30 and see if tree can be rebalanced
57 26 72 3 38 63 94 1 25 37 47 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). 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 -->

7 Insert 32 and see if tree can be rebalanced
57 26 72 3 38 63 94 1 25 37 47 78 30 32 Internal path length: 1x6 + 1x5 + 5x4 + 4x3 + 2x2 + 1 = 48 Next step: Find rebalancing case -->

8 Next step: Perform double rotation -->
Find rebalancing case 57 26 72 3 38 63 94 1 25 37 47 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 -->

9 Perform double rotation
57 26 72 3 38 63 94 1 25 32 47 78 30 37 Internal path length: 2x5 + 5x4 + 4x3 + 2x2 + 1 = 47 Next step: Insert 35 -->

10 Insert 35 and see if tree can be rebalanced
57 26 72 3 38 63 94 1 25 32 47 78 30 37 35 Internal path length: 1x6 + 2x5 + 5x4 + 4x3 + 2x2 + 1 = 53 Next step: Find rebalancing case -->

11 Next step: Apply double rotation -->
Find rebalancing case 57 26 72 3 38 63 94 1 25 32 47 78 30 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 -->

12 Next step: Notes -->
Apply double rotation 57 26 72 3 37 63 94 1 25 32 38 78 30 35 47 Internal path length: 3x5 + 5x4 + 4x3 + 2x2 + 1 = 52 Next step: Notes -->

13 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.


Download ppt "Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008"

Similar presentations


Ads by Google