Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.

Similar presentations


Presentation on theme: "More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store."— Presentation transcript:

1 More Trees Multiway Trees and 2-4 Trees

2 Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store an entire data structure in the main memory of a computer. ◦ What if we have more data than can fit in main memory? ◦ Meaning that we must have the data structure reside on disk. ◦ The rules of the game change, because the Big-Oh model doesn’t apply if all operations are not equal.

3 Motivation of Multi-way Trees Main memory vs. disk ◦ Disk Accesses are incredibly expensive. ◦ 1 disk access is worth about 4,000,000 instructions. ◦ (See the book for derivation) ◦ So we’re willing to do lots of calculations just to save disk accesses.

4 Motivation of Multi-way Trees For example: ◦ Suppose we want to access the driving records of the citizens of Florida.  10 million items.  Assume doesn’t fit in main memory.  Assume in 1 sec, can execute 25 million instructions or perform 6 disk accesses. The Unbalanced Binary tree would be a disaster. In the worst case, it has linear depth and could require 10 mil disk accesses. An AVL Tree In the typical case, it has a depth close to log N, log 10 mil ≈ 24 disk accesses, requiring 4 sec.

5 The point is… Reduce the # of disk accesses to a very small constant,  Such as 3 or 4  And we are willing to write complicated code to do this, because in comparison machine instructions are essentially free.  As long as we’re not ridiculous. We cannot go below log N using a BST ◦ Even an AVL Solution?? ◦ More branching, less height. ◦ Multiway Tree

6 Multiway Tree (or B-tree in book) Multiway tree is similar to BST ◦ In a BST  We need 1 key to decide which of 2 branches to take. ◦ In an Multiway tree  We need M-1 keys to decide which branch to take, where M is the order of the Multiway tree.  Also need to balance,  Otherwise like a BST it could degenerate into a linked list. ◦ Here is a Multiway tree of Order 5:

7 Specifications of a MultiwayTree If a node contains k items (I 1, I 2.. I k ), it will contain k+1 subtrees. Specifically subtrees S 1 thru S k+1 10 20 30 3 7 _15 _ _22 28 _40 50 60 1___1___ 345_345_ 78__78__ 10 12 _ _ 15 17 _ _ 20 21 __ 22 26 _ _ 28 29 _ _ 30 __ _ 40 45 _ _ 55 _ _ _ 65 70 _ _ I1I1 I2I2 I3I3 S1S1 S2S2 S3S3 S k+1 All values in S 1 < I 1. All values in S k+1 >I k. All values in S 3 < I 3, but ≥ I 2. All values in S 2 < I 2, but ≥ to I 1.

8 2-4 Trees ◦ Specific type of multitree. 1)Every node must have in between 2 and 4 children. (Thus each internal node must store in between 1 and 3 keys) 2)All external nodes (the null children of leaf nodes) and leaf nodes have the same depth.

9 Example of a 2-4 tree 10 20 30 3 7 _15 _ _22 28 _40 50 60 1___1___ 345_345_ 78__78__ 10 12 _ _ 15 17 _ _ 20 21 __ 22 26 _ _ 28 29 _ _ 30 __ _ 40 45 _ _ 55 _ _ _ 65 70 _ _

10 Insert Insert 4 into the 2-4 tree below. Compare 4 to vals in root node. 10 20 30 3 7 _15 _ _22 28 _40 50 60 1__1__ 5__5__ 8__8__ 12 _ _ 17 _ _ 21 __ __ 26 _ _ 28 29 __ ______ 45 _ _ 55 _ _ 65 70 _ 4 < 10, 4 goes in the subtree to left of 10. 4 > 3 and 4 < 7, 4 goes in the subtree to right of 3 and left of 7 45_45_

11 Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: 10 20 _ 37_37_ 13 15 17 22 _ _

12 Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: 10 20 _ 13 15 17 18 The node has too many values. You can send one of the values to the parent (the book’s convention is to sent the 3 rd value. 37_37_ 22 _ _

13 Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: 10 17 20 13 15 _ 22 _ _ Moving 17, forces you to “split” the other three values. 18 __ __ 37_37_

14 Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: 10 20 30 5_5_ 11 14 17 _ 32 37 _ _ 25 __ __

15 Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: 10 20 30 5_5_ 11 12 14 17 32 37 _ _ 25 __ __ Using the rule from before, let’s move 12 up.

16 Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: 10 14 20 30 5_5_ 11 12 32 37 _ _ 17 __ __ Using the rule from before, let’s move 14 up. 25 __ __

17 Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: 10 14 20 30 5_5_ 11 12 32 37 _ _ 25 __ __ Now this has too many parent nodes AND subtrees! We can just repeat the process and make a new root. 17 __ __

18 Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: 10 14 5_5_ 11 12 32 37 _ _ 17 __ __ Now this has too many parent nodes AND subtrees! We can just repeat the process and make a new root. 25 __ __ 20 30

19 Deletion from a 2-4 Tree Delete a non-leaf value ◦ Replace that value with the largest value in its left subtree  Or smallest value in its right subtree.

20 Deletion from a 2-4 Tree Delete a leaf node ◦ In the standard case:  a value can simply be removed from a leaf node that stores more than one value.  Requires no structural change. 10 20 30 57_57_ 12 17 __ 35 _ _ 23 27 35 10 20 30 7_7_ 12 17 __ 35 _ _ 23 27 35 Delete 5

21 Deletion from a 2-4 Tree BUT what if the leaf node has ONLY one value? ◦ If you get rid of it,  then it would violate the 2-4 property that all leaf nodes MUST be on the same height of the tree. Break up into 2 cases: 1)An adjacent sibling has more than one value stored in its node. 2)An adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed.

22 Deletion from a 2-4 Tree Case 1: ◦ Consider deleting 5 from the following tree:  An adjacent sibling has more than one value stored in its node. Take the 10 to replace the 5, And then simply replace the 10 with the smallest value in its right subtree. ◦ This is okay, since there is more than one value at this subtree. 10 20 30 5__5__ 12 17 __ 35 _ _ 23 27 _ 12 20 30 10 _ __ 17 _ __ 35 _ _ 23 27 _ Delete 5

23 Deletion from a 2-4 Tree Case 2: ◦ If an adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed. ◦ The fusion operation is a little more difficult since it may result in needing another fusion at a parent node. We have 3 child nodes when we should have 4. Thus we can drop a value, we will drop 10. 10 20 30 5__5__ 15 _ __ 35 _ _ 25 _ _ 10 20 30 _ 15 _ __ 35 _ _ 25 _ _ 10 20 30 15 _ __ 35 _ _ 25 _ _ Delete 5 Fuse empty node with 15

24 Deletion from a 2-4 Tree Case 2: ◦ If an adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed. ◦ The fusion operation is a little more difficult since it may result in needing another fusion at a parent node. We have 3 child nodes when we should have 4. Thus we can drop a value, we will drop 10. 10 20 30 15 _ __ 35 _ _ 25 _ _ 20 30 10 15 __ 35 _ _ 25 _ _ Drop one parent into fused node

25 Examples on the Board


Download ppt "More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store."

Similar presentations


Ads by Google