TCSS 342 Winter 2006 Lecture Notes Multiway Search
- Slides: 45
TCSS 342, Winter 2006 Lecture Notes Multiway Search Trees version 1. 0 1
Objectives n n Define Multiway Search trees Show through how 2 -3 -4 trees are kept balanced Show the connection between 2 -3 -4 trees and red-black trees Explain the motivation behind B-Trees. 2
Multi-Way search Trees Like Binary Search trees except: n nodes can have more than one element perfectly balanced (all leaf nodes at same level) Still obeys ordering property (2, 3) tree n n contains only 2 -nodes and 3 -nodes 2 -node: one item node with 0 or 2 children n n 3 -node: two item node with 0 or 3 children n n same ordering property as binary search tree nodes With two items k 1 and k 2 and 3 children c 0, c 1, c 2, ordering property is: c 0 < k 1 < c 1 < k 2 < c 2. all leaves at same level. 3
Example n A (2, 3) tree storing 18 items. 20 80 30 70 5 2 4 10 25 40 50 75 90 100 85 95 110 120 4
Multiway search trees n (2, 4) tree n n 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: n n n All nodes must contain between 1 and 3 items All leaf nodes must be at the same level Every internal (non-leaf) node satisfies: n If it contains i items k 1, k 2, . . ki, then n n It must have i+1 children c 0, c 1, . . ci Items are sorted so that for each item kb, n All items in subtree cb-1 are less than kb. n All items in subtree cb are greater than kb. 5
(2, 4) Tree Example 10 45 3 8 25 60 50 55 70 90 100 6
B-Trees n n n (2, 3) Tree = B-tree of order 3 (2, 4) Tree = B-tree of order 4 In general, B-tree of order m n allows k items per node, where n n n 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: n In array implementation, each node at most ½ empty n n (# items per node allowed not strictly followed when there are not enough items) Always balanced 7
B-Trees motivation n Suppose data stored on disk n To read data, speed depends on n n 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: n n 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. 8
Implementing multiway search trees n Always maintain the search tree properties after every insertion and deletion n n Do some combination of the following to maintain tree properties: n n n Maintain ordering property Maintain all leaves at same level Make sure #items in each node is acceptable 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 9
(2, 4) Trees, some details n While adjusting trees n n n Temporarily allow 4 items in a node. Temporarily allow leaves to not be at same level. Insertion: n n Find and insert item into proper leaf node (by sorted order) If node is too full (has 4 items), then split the node n Split full node into one node with two subtrees n n Merge root of split node upward with parent n n Leaves of new subtrees are now one level off 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. 10
(2, 4) Tree Next: Insert 38 10 45 3 8 25 60 50 55 70 90 100 11
(2, 4) Tree After insert(38); next: insert(105) 45 10 60 3 8 25 38 50 55 70 90 100 12
(2, 4) Tree n After Insert(105); 45 10 3 8 25 38 60 90 50 55 70 105 13
Removal n Swap node to removed with inorder successor n n 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 n n n 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. 14
Delete(15) 35 20 6 60 15 40 50 70 80 90 15
Delete(15) 35 20 6 60 40 50 70 80 90 16
Continued n Drop item from parent 35 60 6 20 40 50 70 80 90 17
Continued n Drop item from grandparent 35 6 20 60 40 50 70 80 90 18
Fuse 35 60 6 20 40 50 70 80 90 19
Drop item from root n Remove root, return the child. 35 60 6 20 40 50 70 80 90 20
Summary n n (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. 21
Red-Black and (2, 4) trees. n Every Red-Black tree has a corresponding (2, 4) tree. n n Does this always work? Why? n n n max # items in a node? all leaves at same depth? Is there only one unique way to convert n n n Move every red node upwards into parent black node. red-black tree into (2, 4) tree? (2, 4) tree into red-black tree? Strategy for red-black deletion: n n n Convert Red-Black to (2, 4) tree. Delete from (2, 4) tree. Convert back to red-black tree. 22
23
The corresponding (2, 4) tree 10 30 5 20 40 50 55 Deleting 20 is a simple transfer! Remove the item 20. Drop an item (30) from parent. Move an item 40 parent. 24
Updated 2 -3 -4 tree 10 40 5 30 40 55 50 50 55 25
Now redraw the RBTree! 60 40 10 5 70 50 30 85 65 55 80 90 Drill: delete(65), delete(80), delete(90), delete(85) 26
References n Lewis & Chase book, chapter 16. 27
Example n A (2, 3) tree storing 18 items. 20 80 30 70 5 2 4 10 25 40 50 75 90 100 85 95 110 120 28
Updating n n n 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); 29
Insertion n Insert(3); 20 80 5 2 3 4 10 15 30 70 23 25 40 50 75 90 100 85 95 110 120 30
Insert(3); n In mid air… 20 80 5 30 70 90 100 3 2 4 10 15 23 25 40 50 75 85 95 110 120 31
Done…. 20 80 3 5 2 30 70 4 10 15 23 25 40 50 75 90 100 85 95 110 120 32
Tree grows at the root… n Insert(45); 20 80 3 5 2 4 30 70 10 25 40 45 50 75 90 100 85 95 110 120 33
n New root: 45 20 3 5 2 4 80 30 10 25 40 70 50 90 100 75 85 95 110 120 34
Delete n n n 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); 35
Remove(80); n Step 1: Exchange 80 with in-order successor. 45 20 3 5 2 4 85 30 10 25 40 70 50 90 100 75 80 95 110 120 36
n n Redistribute Remove(80); 45 20 3 5 2 4 85 30 10 25 40 70 50 95 110 75 90 100 120 37
Some more removals… Remove(70); Swap(70, 75); Remove(70); “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. 95 110 n 38
Delete(70) 45 20 3 5 2 4 85 30 10 25 40 75 50 95 110 90 100 120 39
New tree: n Delete(85) will “shrink” the tree. 45 20 3 5 2 4 95 30 10 85 25 40 50 110 90 100 120 40
Details n n n 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. 41
“Shorter” 2 -3 Tree 20 45 3 5 2 4 30 10 25 40 95 110 50 90 100 120 42
Deletion Summary n n n 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. 43
Deletion (continued) n n If N is the root, delete it and return its child as the new root. Example: Delete(8); 5 5 1 3 2 8 3 3 Return 35 35 44
Deletion (Continued) n n 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) 45
- M way tree
- Multiway trees
- 105 ×106
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Www.tcss.net
- Mypay tcss
- Mypay tcss
- Tcss
- Tcss net
- Winter kommt winter kommt flocken fallen nieder
- Winter kommt winter kommt flocken fallen nieder
- Es war eine mutter
- Two way selection and multiway selection
- Two way selection and multiway selection in c
- Multiway selection flowchart
- Multiway trie
- Multiway join
- Two phase multiway merge sort
- Pentaho user guide
- Project procurement management lecture notes
- Theology proper lecture notes
- Define public sector accounting
- 4 ps of project management
- Electricity and magnetism lecture notes
- Physics 111 lecture notes
- Physical science lecture notes
- Power system dynamics and stability lecture notes
- Microbial physiology notes
- Mechatronics lecture notes / ppt
- Limits fits and tolerances lecture notes
- Money-time relationship and equivalence
- Bjt
- Requirement analysis in software engineering notes
- Introduction to ofdm
- Land use planning '' lecture notes
- Project quality management lecture notes
- Lecture notes on homiletics
- Foundation engineering lecture notes
- Image processing lecture notes
- Intermediate microeconomics lecture notes
- Cloud computing lecture
- Decision theory lecture notes
- Nonlinear regression lecture notes
- Advanced inorganic chemistry lecture notes
- Bilateral vs unilateral tolerance
- Global stiffness matrix