86
LECTURE 39: GREEK TRAGEDY & BALANCED TREES CSC 213 – Large Scale Programming

Lecture 39: Greek Tragedy & Balanced Trees

  • Upload
    tanaya

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 39: Greek Tragedy & Balanced Trees. 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? - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 39: Greek Tragedy &  Balanced Trees

LECTURE 39: GREEK TRAGEDY & BALANCED TREES

CSC 213 – Large Scale Programming

Page 2: Lecture 39: Greek Tragedy &  Balanced Trees

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?

Page 3: Lecture 39: Greek Tragedy &  Balanced Trees

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?

Page 4: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 5: Lecture 39: Greek Tragedy &  Balanced Trees

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)

Page 6: Lecture 39: Greek Tragedy &  Balanced Trees

Optimal Tree Partition

Page 7: Lecture 39: Greek Tragedy &  Balanced Trees

Optimal Tree Partition

But no GC algorithm produces this!

Page 8: Lecture 39: Greek Tragedy &  Balanced Trees

Real-World Big Search Trees

Excellent way to test roommates system

Page 9: Lecture 39: Greek Tragedy &  Balanced Trees

Real-World Big Search Trees

Excellent way to test roommates system

Page 10: Lecture 39: Greek Tragedy &  Balanced Trees

Real-World Big Search Trees

Excellent way to test roommates system

Page 11: Lecture 39: Greek Tragedy &  Balanced Trees

(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

Page 12: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 13: Lecture 39: Greek Tragedy &  Balanced Trees

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?

Page 14: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 15: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 16: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 17: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 18: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 19: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 20: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 21: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 22: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 23: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 24: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 25: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 26: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 27: Lecture 39: Greek Tragedy &  Balanced Trees

(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

Page 28: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 29: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 30: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 31: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 32: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 33: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 34: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 35: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 36: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 37: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 38: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 39: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 40: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 41: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 42: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 43: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 44: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 45: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 46: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 47: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 48: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 49: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 50: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 51: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 52: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 53: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 54: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 55: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 56: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 57: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 58: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 59: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 60: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 61: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 62: Lecture 39: Greek Tragedy &  Balanced Trees

Deletion

If Entry's child internal, replace with successor Go 1 to right and then go left just like with

a BST

Page 63: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 64: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 65: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 66: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 67: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 68: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 69: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 70: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 71: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 72: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 73: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 74: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 75: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 76: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 77: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 78: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 79: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 80: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 81: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 82: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 83: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 84: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 85: Lecture 39: Greek Tragedy &  Balanced Trees

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

Page 86: Lecture 39: Greek Tragedy &  Balanced Trees

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