Post on 25-Dec-2015
LECTURE 39: GREEK TRAGEDY & BALANCED TREES
CSC 213 – Large Scale Programming
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?
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?
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
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(Numbers gathered from Feb. 2007 article)
Optimal Tree Partition
Optimal Tree Partition
But no GC algorithm produces this!
Real-World Big Search Trees
Excellent way to test roommates system
Real-World Big Search Trees
Excellent way to test roommates system
Real-World Big Search Trees
Excellent way to test roommates system
(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
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
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?
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 Entrys than children Entrys sorted from smallest to largest
11 24
2 6 8 15 27 30
Multi-Way Search Tree
Children v1 v2 v3 … vd & keys k1 k2 … kd-1
Keys in subtree v1 smaller than k1
Keys in subtree vi between ki-1 and k2
Keys in subtree vd greater than kd-1
11 24
27 302 6 8 15
Inorder Traversal
Visit each child, vi , before visiting Entry ei
As with BST, visits keys in increasing order
11 24
2 6 8 15 27 301 2 3 7
4 6
5 8
Multi-Way Searching
Similar to BST treeSearch finding a keyfor i = 0 to numChildren – 1 doif 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 8 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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 15 27 30
Multi-Way Searching
for i = 0 to numChildren – 1 doif 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
15 27 302 6 8
(2,4) Trees
Multi-way search tree with 2 properties: Node-Size Property
Internal nodes have at most 4 children
Depth PropertyAll 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 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 3218
Insertion
Start by searching for key k Entry added to last internal node
searched Depth property preserved by enforcing this
Example: insert(30)
10 15 24
2 8 12 27 30 3218
Insertion
Insertion may cause overflow! 5-node created by the insertion This would make it violate Node-Size
property
27 32 35
15 24
12 18
Insertion
Insertion may cause overflow! 5-node created by the insertion This would make it violate Node-Size
property
27 30 32 35
15 24
12 18
In Case Of Overflow Split Node Split 5-node into 2 new nodes
Entrys e1 e2 & children v1 v2 v3 become a 3-node 2-node created with Entry e4 & children v4 v5
15 24
12 18 27 30 32 35
In Case Of Overflow Split Node Split 5-node into 2 new nodes
Entrys e1 e2 & children v1 v2 v3 become a 3-node 2-node created with Entry e4 & children v4 v5
Promote e3 to parent node If overflow occurs in root node, create new root Overflow can cascade when parent already was
4-node15 24
12 18 27 30 32 35 12 27 3018 35
15 24 32
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 29 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 29 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
27 29 32 3512 18 25
15 24 26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
12 18 25
15 24 26 32
27 29 35
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
12 18 25
15 24 26 32
27 29 35
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
12 18 25
15 24
27 29 35
32
26
Parent Overflow
In case of cascade, repeat overflow process Works identically to when children are
external Example: insert(29)
12 18 25
15 24
27 29 35
32
26
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
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 8 12 18 27 32 35
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 8 12 18 27 32 35
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 8 12 18 27 32 35
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 8 12 18 27 32 35
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 8 12 18 27 32 35
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 8 12 18 27 32 35
10 15 24
2 8 12 18 32 35
Deletion
If Entry's child internal, replace with successor Go 1 to right and then go left just like with
a BST
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 8 12 18 27 32 35
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 8 12 18 27 32 35
10 15 27
2 8 12 18 32 35
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
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
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
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
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
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 10
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 10
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 10
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 10
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
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
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 82 9
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 82 9
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
62 9
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
62 9
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
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
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
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
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
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
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