Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 342, Winter 2006 Lecture Notes

Similar presentations


Presentation on theme: "TCSS 342, Winter 2006 Lecture Notes"— Presentation transcript:

1 TCSS 342, Winter 2006 Lecture Notes
Multiway Search Trees version 1.0

2 Objectives Define Multiway Search trees
Show through how trees are kept balanced Show the connection between trees and red-black trees Explain the motivation behind B-Trees.

3 Multi-Way search Trees
Like Binary Search trees except: nodes can have more than one element perfectly balanced (all leaf nodes at same level) Still obeys ordering property (2,3) tree contains only 2-nodes and 3-nodes 2-node: one item node with 0 or 2 children same ordering property as binary search tree nodes 3-node: two item node with 0 or 3 children With two items k1 and k2 and 3 children c0, c1, c2, ordering property is: c0 < k1 < c1 < k2 < c2. all leaves at same level.

4 Example A (2,3) tree storing 18 items. 20 80 5 30 70 90 100 25 40 50
20 80 5 30 70 25 40 50 75 85 2 4 10 95

5 Multiway search trees (2,4) tree Alternative 2-4 tree definition:
contains only 2-nodes, 3-nodes, and 4-nodes 4-node: three item node with 0 or 4 children; satisfies similar ordering property all leaves at same level Alternative 2-4 tree definition: All nodes must contain between 1 and 3 items All leaf nodes must be at the same level Every internal (non-leaf) node satisfies: If it contains i items k1, k2, .. ki, then It must have i+1 children c0, c1, .. ci Items are sorted so that for each item kb, All items in subtree cb-1 are less than kb. All items in subtree cb are greater than kb.

6 (2,4) Tree Example 3 8 50 55 25

7 B-Trees (2,3) Tree = B-tree of order 3 (2,4) Tree = B-tree of order 4
In general, B-tree of order m allows k items per node, where m/2 -1  k  m-1 Internal nodes with k items have k+1 children Satisfies similar ordering property All leaves at same level Design ideas behind B-Trees: In array implementation, each node at most ½ empty (# items per node allowed not strictly followed when there are not enough items) Always balanced

8 B-Trees motivation Suppose data stored on disk
To read data, speed depends on Seek time (moving head) Transfer time (after location is found on disk) Accessing a contiguous chunk of data relatively fast Accessing data scattered in different parts of disk slow Storing sorted data on disk: Use B-tree to store long contiguous chunks Design order of B-tree so items in one node fits in exactly one fast-loading chunk of data.

9 Implementing multiway search trees
Always maintain the search tree properties after every insertion and deletion Maintain ordering property Maintain all leaves at same level Make sure #items in each node is acceptable Do some combination of the following to maintain tree properties: Rotations (Redistributions) Splitting full nodes Fusing neighboring nodes together Moving item from child to parent, or vice versa Replacing an item with its in-order successor. Do case-by-case analysis to figure out exact steps

10 (2,4) Trees, some details While adjusting trees Insertion:
Temporarily allow 4 items in a node. Temporarily allow leaves to not be at same level. Insertion: Find and insert item into proper leaf node (by sorted order) If node is too full (has 4 items), then split the node Split full node into one node with two subtrees Leaves of new subtrees are now one level off Merge root of split node upward with parent Fixes level problem Repeatedly apply split to parent node, if necessary. If process goes all the way top, may get new root. Alternative insertion strategy: split full nodes on the way down when looking for proper leaf node.

11 (2,4) Tree Next: Insert 38 3 8 50 55 25

12 After insert(38); next: insert(105)
(2,4) Tree After insert(38); next: insert(105) 45 10 60 3 8 25 38 50 55

13 (2,4) Tree After Insert(105); 45 10 60 90 3 8 50 55 70 25 38

14 Removal Swap node to removed with inorder successor
Inorder successor will always be in a leaf. Remove item from the leaf node. If node is now empty, try to fill it with an extra item from neighbor (redistribution/rotation). If all neighbors have only one item, we have an UNDERFLOW. Fix this by move parent item down into child node so it contains two items move a grandparent item down into parent node merge parent node with sibling (possible node split) If necessary, fix UNDERFLOW at grandparent level Underflows propagating to root shrink tree.

15 Delete(15) 35 20 60 6 15 40 50

16 Delete(15) 35 20 60 6 40 50

17 Continued Drop item from parent 35 60 6 20 40 50

18 Continued Drop item from grandparent 35 60 6 20 40 50

19 Fuse 35 60 6 20 40 50

20 Drop item from root Remove root, return the child. 35 60 6 20 40 50
35 60 6 20 40 50

21 Summary (2,4) trees make it very easy to maintain balance.
Insertion and deletion easier for (2,4) tree. Cost is waste of space in each node. Also extra comparison inside each node. Does not “extend” binary trees.

22 Red-Black and (2,4) trees. Every Red-Black tree has a corresponding (2,4) tree. Move every red node upwards into parent black node. Does this always work? Why? max # items in a node? all leaves at same depth? Is there only one unique way to convert red-black tree into (2,4) tree? (2,4) tree into red-black tree? Strategy for red-black deletion: Convert Red-Black to (2,4) tree. Delete from (2,4) tree. Convert back to red-black tree.

23

24 The corresponding (2,4) tree
10 30 20 5 Deleting 20 is a simple transfer! Remove the item 20. Drop an item (30) from parent. Move an item 40  parent.

25 Updated tree 10 40 5 30 50 55

26 Now redraw the RBTree! 60 40 70 10 85 50 65 5 30 80 90 55 Drill: delete(65), delete(80), delete(90), delete(85)

27 References Lewis & Chase book, chapter 16.

28 Example A (2,3) tree storing 18 items. 20 80 5 30 70 90 100 25 40 50
20 80 5 30 70 25 40 50 75 85 2 4 10 95

29 Updating Insertion: Find the appropriate leaf. If there is only one item, just add to leaf. Insert(23); Insert(15) If no room, move middle item to parent and split remaining two items among two children. Insert(3);

30 Insertion Insert(3); 20 80 5 30 70 40 50 75 85 10 15 23 25 95

31 Insert(3); In mid air… 20 80 5 30 70 3 23 25 40 50 75 85 2 10 15 95 4

32 Done…. 20 80 3 5 30 70 4 23 25 40 50 75 85 2 10 15 95

33 Tree grows at the root… Insert(45); 20 80 3 5 30 70 90 100 4 25
20 80 3 5 30 70 4 25 75 85 2 10 95

34 New root: 45 20 80 3 5 30 70 4 25 40 50 75 85 2 10 95

35 Delete If item is not in a leaf exchange with in-order successor.
If leaf has another item, remove item. Examples: Remove(110); (Insert(110); Remove(100); ) If leaf has only one item but sibling has two items: redistribute items. Remove(80);

36 Remove(80); Step 1: Exchange 80 with in-order successor. 45 20 85 3 5
3 5 30 70 4 25 40 50 75 80 2 10 95

37 Redistribute Remove(80); 45 20 85 3 5 30 70 95 110 120 4 25 40 50 75
3 5 30 70 120 4 25 40 50 75 90 2 10 100

38 Some more removals… Remove(70); Swap(70, 75);
“Merge” Empty node with sibling; Join parent with node; Now every node has k+1 children except that one node has 0 items and one child. Sibling can spare an item: redistribute. 110

39 Delete(70) 45 20 85 3 5 30 75 120 4 25 40 50 90 2 10 100

40 New tree: Delete(85) will “shrink” the tree. 45 20 95 3 5 30 85 110
3 5 30 85 110 120 4 25 40 50 90 100 2 10

41 Details 1. Swap(85, 90) //inorder successor
2. Remove(85) //empty node created 3. Merge with sibling 4. Drop item from parent// (50,90) empty Parent 5. Merge empty node with sibling, drop item from parent (95) 6. Parent empty, merge with sibling drop item. Parent (root) empty, remove root.

42 “Shorter” 2-3 Tree 20 45 3 5 30 95 110 50 90 120 4 25 40 100 2 10

43 Deletion Summary If item k is present but not in a leaf, swap with inorder successor; Delete item k from leaf L. If L has no items: Fix(L); Fix(Node N); //All nodes have k items and k+1 children // A node with 0 items and 1 child is possible, it will have to be fixed.

44 Deletion (continued) If N is the root, delete it and return its child as the new root. Example: Delete(8); 5 5 1 2 3 3 3 5 3 8 Return 3 5

45 Deletion (Continued) If a sibling S of N has 2 items distribute items among N, S and the parent P; if N is internal, move the appropriate child from S to N. Else bring an item from P into S; If N is internal, make its (single) child the child of S; remove N. If P has no items Fix(P) (recursive call)


Download ppt "TCSS 342, Winter 2006 Lecture Notes"

Similar presentations


Ads by Google