Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000."— Presentation transcript:

1 CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000

2 Today’s Outline Hand in the homework (GO! GO! GO!) B-Trees

3 M-ary Search Tree Maximum branching factor of M Complete tree has depth = log M N Each internal node in a complete tree has M - 1 keys runtime:

4 B-Trees B-Trees are specialized M-ary search trees Each node has many keys –subtree between two keys x and y contains values v such that x  v < y –binary search within a node to find correct subtree Each node takes one full {page, block, line} of memory 371221 x<3 3  x<77  x<1212  x<2121  x

5 B-Tree Properties ‡ Properties –maximum branching factor of M –the root has between 2 and M children or at most L keys –other internal nodes have between  M/2  and M children –internal nodes contain only search keys (no data) –smallest datum between search keys x and y equals x –each (non-root) leaf contains between  L/2  and L keys –all leaves are at the same depth Result –tree is  (log n) deep –all operations run in  (log n) time –operations pull in about M or L items at a time ‡ These are technically B + -Trees

6 B-Tree Properties Properties –maximum branching factor of M –the root has between 2 and M children or at most L keys –other internal nodes have between  M/2  and M children –internal nodes contain only search keys (no data) –smallest datum between search keys x and y equals x –each (non-root) leaf contains between  L/2  and L keys –all leaves are at the same depth Result –tree is  (log n) deep –all operations run in  (log n) time –operations pull in about M or L items at a time

7 B-Tree Properties Properties –maximum branching factor of M –the root has between 2 and M children or at most L keys –other internal nodes have between  M/2  and M children –internal nodes contain only search keys (no data) –smallest datum between search keys x and y equals x –each (non-root) leaf contains between  L/2  and L keys –all leaves are at the same depth Result –tree is  (log n) deep –all operations run in  (log n) time –operations pull in about M or L items at a time

8 B-Tree Properties Properties –maximum branching factor of M –the root has between 2 and M children or at most L keys –other internal nodes have between  M/2  and M children –internal nodes contain only search keys (no data) –smallest datum between search keys x and y equals x –each (non-root) leaf contains between  L/2  and L keys –all leaves are at the same depth Result –tree is  (log M n) deep –all operations run in  (log M n) time –operations pull in about M/2 or L/2 items at a time

9 … __ k1k1 k2k2 … kiki B-Tree Nodes Internal node –i search keys; i+1 subtrees; M - i - 1 inactive entries Leaf –j data keys; L - j inactive entries k1k1 k2k2 … kjkj … __ 12M - 1 12L i j

10 Example B-Tree with M = 4 and L = 4 12 3569101112 1517 202526 303233364042 506070 1040 3 15203050

11 Making a B-Tree The empty B-Tree M = 3 L = 2 3 Insert(3) 314 Insert(14) Now, Insert(1)?

12 Splitting the Root And create a new root 13 14 13 13 3 Insert(1) Too many keys in a leaf! So, split the leaf.

13 Insertions and Split Ends Insert(59) 14 13 59 14 13 Insert(26) 14 13 26 59 142659 1459 13142659 And add a new child Too many keys in a leaf! So, split the leaf.

14 Propagating Splits 1459 13142659 1459 13142659 5 135 Insert(5) 514 2659 135 5 5 135 142659 14 Add new child Create a new root Too many keys in an internal node! So, split the node.

15 Insertion in Boring Text Insert the key in its leaf If the leaf ends up with L+1 items, overflow! –Split the leaf into two nodes: original with  (L+1)/2  items new one with  (L+1)/2  items –Add the new child to the parent –If the parent ends up with M+1 items, overflow! If an internal node ends up with M+1 items, overflow! –Split the node into two nodes: original with  (M+1)/2  items new one with  (M+1)/2  items –Add the new child to the parent –If the parent ends up with M+1 items, overflow! Split an overflowed root in two and hang the new nodes under a new root This makes the tree deeper!

16 After More Routine Inserts 5 135 142659 14 5 135 265979 5989 14 89 Insert(89) Insert(79)

17 Deletion 5 135 14265979 5989 14 89 5 135 142679 89 14 89 Delete(59)

18 Deletion and Adoption 5 135 142679 8914 89 Delete(5) ? 13 142679 8914 89 3 133 142679 89 14 89 A leaf has too few keys! So, borrow from a neighbor

19 Deletion with Propagation 3 1 3 142679 8914 89 Delete(3) ? 1 142679 8914 89 1 142679 8914 89 A leaf has too few keys! And no neighbor with surplus! So, delete the leaf But now a node has too few subtrees!

20 Adopt a neighbor 1 142679 89 14 89 14 1 2679 89 79 89 Finishing the Propagation (More Adoption)

21 Delete(1) (adopt a neighbor) 14 1 2679 89 79 89 A Bit More Adoption 26 14 26 79 89 79 89

22 Delete(26) 26 14 26 79 89 79 89 Pulling out the Root 14 79 89 79 89 A leaf has too few keys! And no neighbor with surplus! 14 79 89 79 89 So, delete the leaf A node has too few subtrees and no neighbor with surplus! 14 79 89 Delete the leaf But now the root has just one subtree!

23 Pulling out the Root (continued) 14 79 89 The root has just one subtree! But that’s silly! 14 79 89 Just make the one child the new root!

24 Deletion in Two Boring Slides of Text Remove the key from its leaf If the leaf ends up with fewer than  L/2  items, underflow! –Adopt data from a neighbor; update the parent –If borrowing won’t work, delete node and divide keys between neighbors –If the parent ends up with fewer than  M/2  items, underflow! Why will dumping keys always work if borrowing doesn’t?

25 Deletion Slide Two If a node ends up with fewer than  M/2  items, underflow! –Adopt subtrees from a neighbor; update the parent –If borrowing won’t work, delete node and divide subtrees between neighbors –If the parent ends up with fewer than  M/2  items, underflow! If the root ends up with only one child, make the child the new root of the tree This reduces the height of the tree!

26 Thinking about B-Trees B-Tree insertion can cause (expensive) splitting and propagation B-Tree deletion can cause (cheap) borrowing or (expensive) deletion and propagation Propagation is rare if M and L are large (Why?) Repeated insertions and deletion can cause thrashing If M = L = 128, then a B-Tree of height 4 will store at least 30,000,000 items

27 A Tree with Any Other Name FYI: –B-Trees with M = 3, L = x are called 2-3 trees –B-Trees with M = 4, L = x are called 2-3-4 trees Why would we ever use these?

28 Another Way B-Trees are hairy in practice hairy - Slang. Fraught with difficulties; hazardous: a hairy escape; hairy problems.

29 To Do Continue Project II Read chapter 4 in the book Look forward to no quiz/homework for two weeks! (And prepare for the midterm)

30 Coming Up Self-balancing Trees –AVL trees –Splay trees Second project due (January 31 st ) Midterm (February 4 th )!


Download ppt "CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000."

Similar presentations


Ads by Google