Download presentation
Presentation is loading. Please wait.
Published byTimothy Short Modified over 9 years ago
1
CSC 213 – Large Scale Programming
2
Today’s Goals Review a new search tree algorithm is needed What real-world problems occur with old tree? Why does garbage collection make problem worse? What was ideal approach? How could we force this? Consider how to create other search tree types Not limit nodes to 1 element & what could happen? How to perform insertions on multi-nodes? What about withdrawal? How can we remove data? Can this sound dirtier? And do I hear banjos playing?
3
Dictionary ADT Dictionary and Map maps keys to values O(1) time with hash, but only if hash is good Can guarantee better -- O(log n) with balanced BST Assumes data fits in memory since locality will suck But, honestly, how big can a tree be?
4
Dictionary ADT Dictionary and Map maps keys to values O(1) time with hash, but only if hash is good Can guarantee better -- O(log n) with balanced BST Assumes data fits in memory since locality will suck But, honestly, how big can a tree be? Library of Congress – 20 TB in text database Amazon.com – 42 TB of combined data ChoicePoint – 250 TB of data on everyday Americans World Data Center for Climate – 4 PB of climate data
5
Dictionary ADT
6
Optimal Tree Partition
7
But no GC algorithm produces this!
8
Real-World Big Search Trees Excellent way to test roommates system
9
Real-World Big Search Trees roommates Excellent way to test roommates system
10
Real-World Big Search Trees roommates Excellent way to test roommates system
11
(a,b) Trees to the Rescue! General solution to frequent hikes to Germany Linux & MacOS to track files & directories MySQL & other databases use this to hold all the data Found in many other places where paging occurs Simple rules define working of any ( a, b ) tree Grows upward so that all leaves found at same level At least a children for each internal node Every internal node has at most b children
12
What is “the BTree?” Common multi-way tree implementation Describe B-Tree using order (“BTree of order m ”) m / 2 to m children per internal node Root node can have m or fewer elements Many variants exist to improve some failing Each variant is specialized for some niche use Minor differences only between each variant Will just describe most basic B-Tree during lecture
13
BTree Order Select order minimizing paging when created Elements & references to kids in full node fills page Nodes have at least m / 2 elements, even at their smallest In memory guarantees each page is at least 50% full How many pages touched by each operation?
14
Multi-Way Search Tree Nodes contain multiple elements Tree grows up with leaves always at same level Each internal node: At least 2 children 1 fewer Entry s than children Entry s sorted from smallest to largest 11 24 2 6 81527 30
15
Multi-Way Search Tree Children v 1 v 2 v 3 … v d & keys k 1 k 2 … k d 1 Keys in subtree v 1 smaller than k 1 Keys in subtree v i between k i 1 and k 2 Keys in subtree v d greater than k d-1 11 24 27 302 6 815
16
Inorder Traversal Visit each child, v i, before visiting Entry e i As with BST, visits keys in increasing order 11 24 2 6 81527 30 1237 4 6 5 8
17
Multi-Way Searching Similar to BST treeSearch finding a key for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) 11 24 2 6 81527 30
18
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
19
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
20
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
21
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
22
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
23
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
24
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 81527 30
25
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 2 6 8 1527 30
26
Multi-Way Searching for i = 0 to numChildren – 1 do if k < e[i].getKey() then return search(child[i]) if k == e[i].getKey() then return e[i] endfor if k > e[e.length-1].getKey() then return search(child[child.length-1]) Example: find(8) 11 24 1527 30 2 6 8
27
(2,4) Trees Multi-way search tree with 2 properties: Node-Size Property Internal nodes have at most 4 children Depth Property All external nodes at same depth Nodes are either 2-node, 3-node or 4-node Node’s number of children used as basis for name 10 15 24 2 81227 3218
28
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
29
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
30
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
31
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
32
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
33
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
34
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
35
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
36
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
37
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 81227 3218
38
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 812 27 32 18
39
Insertion Start by searching for key k last Entry added to last internal node searched Depth property preserved by enforcing this Example: insert(30) 10 15 24 2 812 27 30 32 18
40
Insertion Insertion may cause overflow! 5-node created by the insertion Node-Size This would make it violate Node-Size property 27 32 35 15 24 1218
41
Insertion Insertion may cause overflow! 5-node created by the insertion Node-Size This would make it violate Node-Size property 27 30 32 35 15 24 1218
42
In Case Of Overflow Split Node Split 5-node into 2 new nodes Entry s e 1 e 2 & children v 1 v 2 v 3 become a 3-node 2-node created with Entry e 4 & children v 4 v 5 15 24 1218 27 30 32 35
43
In Case Of Overflow Split Node Split 5-node into 2 new nodes Entry s e 1 e 2 & children v 1 v 2 v 3 become a 3-node 2-node created with Entry e 4 & children v 4 v 5 Promote e 3 to parent node If overflow occurs in root node, create new root Overflow can cascade when parent already was 4-node 15 24 1218 27 30 32 35 12 27 30 18 35 15 24 32
44
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 32 35121825 15 24 26
45
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 32 35121825 15 24 26
46
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 32 35121825 15 24 26
47
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 32 35121825 15 24 26
48
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 29 32 35121825 15 24 26
49
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 29 32 35121825 15 24 26
50
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 27 29 32 35121825 15 24 26
51
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 121825 15 24 26 32 27 2935
52
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 121825 15 24 26 32 27 2935
53
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 121825 15 24 27 2935 32 26
54
Parent Overflow In case of cascade, repeat overflow process Works identically to when children are external Example: insert(29) 121825 15 24 27 2935 32 26
55
Adding to MultiWay Tree Leaves all at same level, so trees grow upwards Add to last internal node from initial tree search Addition not done – check if node too large Push 1 item up into parent and split into 2 nodes May pass problem along, so check if parent too large Traverse until overflow stops or made new root node
56
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35
57
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35
58
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35
59
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35
60
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35
61
Deletion Must first find Entry to be deleted Remove Entry & an external child if it is on leaf Example: delete(27) 10 15 24 2 8121827 32 35 10 15 24 2 8121832 35
62
Deletion If Entry 's child internal, replace with successor Go 1 to right and then go left just like with a BST
63
Deletion If Entry 's child internal, replace with successor Go 1 to right and then as far left as possible; like BST Example: delete(24) 10 15 24 2 8121827 32 35
64
Deletion If Entry 's child internal, replace with successor Go 1 to right and then go left just like with a BST Example: delete(24) 10 15 24 2 8121827 32 35 10 15 27 2 8121832 35
65
15 9 14 Underflow and Fusion Entry deletion may cause underflow Node becomes 1-node Choice of solution depends on situation Example: remove(15) 102 5 7
66
15 9 14 Underflow and Fusion Entry deletion may cause underflow Node becomes 1-node Choice of solution depends on situation Example: remove(15) 102 5 7
67
15 9 14 Underflow and Fusion Entry deletion may cause underflow Node becomes 1-node Choice of solution depends on situation Example: remove(15) 102 5 7
68
Entry deletion may cause underflow Node becomes 1-node Choice of solution depends on situation Example: remove(15) 9 14 Underflow and Fusion 102 5 7
69
Entry deletion may cause underflow Node becomes 1-node Choice of solution depends on situation Example: remove(15) 9 14 Underflow and Fusion 102 5 7
70
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 8210
71
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 8210
72
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 8210
73
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 8210
74
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 82
75
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 9 6 82
76
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 6 829
77
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 6 829
78
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 8 629
79
Case 1: Transfer Has adjacent 3- or 4-node sibling Steal parent’s Entry closest to the 1-node Prevent loneliness & promote sibling’s Entry No further processing needed in this case Example: remove(10) 4 8 629
80
Emptied node has adjacent 2-node sibling Merge node & sibling into one Look to parent and steal Entry between siblings May propagate underflow to parent! Example: remove(15) Case 2: Fusion Mom 9 14 102 5 7
81
Emptied node has adjacent 2-node sibling Merge node & sibling into one Look to parent and steal Entry between siblings May propagate underflow to parent! Example: remove(15) 9 14 Case 2: Fusion 102 5 7 Mom
82
Emptied node has adjacent 2-node sibling Merge node & sibling into one Look to parent and steal Entry between siblings May propagate underflow to parent! Example: remove(15) 9 Case 2: Fusion 10 142 5 7 Mom
83
Emptied node has adjacent 2-node sibling Merge node & sibling into one Look to parent and steal Entry between siblings May propagate underflow to parent! Example: remove(15) 9 Case 2: Fusion 10 142 5 7 Mom
84
Emptied node has adjacent 2-node sibling Merge node & sibling into one Look to parent and steal Entry between siblings May propagate underflow to parent! Example: remove(15) 9 Case 2: Fusion 10 142 5 7 Mom
85
Deletion from MultiWay Tree Removal like BST: swap element to legal node If removal causes underflow, check its nearest siblings If 3-node or 4-node as sibling, then solution is easy… … move sibling up and bring parent down into node Merge with sibling & parent data if no big neighbor… … but must then check if parent has an underflow
86
For Next Lecture Wednesday will be quiz on real-world stuff Garbage collection, cache behavior & trees (oh, my) End of day Wednesday lab project is due Will have regular hour during lab, too At end of day on Friday will have Project #3 due
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.