Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.

Similar presentations


Presentation on theme: "CSE 326: Data Structures B-Trees Ben Lerner Summer 2007."— Presentation transcript:

1 CSE 326: Data Structures B-Trees Ben Lerner Summer 2007

2 2 Splay Tree Summary All operations are in amortized O(log n) time Splaying can be done top-down; this may be better because: –only one pass –no recursion or parent pointers necessary –we didn’t cover top-down in class Splay trees are very effective search trees –Relatively simple –No extra fields required –Excellent locality properties: frequently accessed keys are cheap to find

3 3 Trees so far BST AVL Splay

4 4 Something We Forgot: Disk Acesses Real world: constants matter! Distance

5 5 Trees on Disk Each pointer lookup means seeking the disk Want as shallow a tree as possible Balanced binary tree with N nodes has height ________? Balanced M-ary tree with N nodes has height ________?

6 6 M-ary Search Tree Maximum branching factor of M Complete tree has height = # disk accesses for find: Runtime of find:

7 7 Problems with M-ary trees? Problems come from each of our design goals: Structure? Order? Balance?

8 8 Solution: B-Trees specialized M-ary search trees Each node has (up to) M-1 keys: –subtree between two keys x and y contains leaves with values v such that x  v < y Pick branching factor M such that each node takes one full {page, block} of memory 371221 x<3 3  x<77  x<1212  x<2121  x

9 9 B-Trees What makes them disk-friendly? 1.Many keys stored in a node All brought to memory/cache in one access! 2.Internal nodes contain only keys; Only leaf nodes contain keys and actual data The tree structure can be loaded into memory irrespective of data object size Data actually resides in disk

10 10 Example 1KB pages Key is 8bytes, pointer is 4bytes 8*(M-1) + 4*(M) = 1024 12M = 1032 M = floor(1032/12) = 86

11 11 B-Trees are multi-way search trees commonly used in database systems or other applications where data is stored externally on disks and keeping the tree shallow is important. A B-Tree of order M has the following properties: 1. The root is either a leaf or has between 2 and M children. 2. All nonleaf nodes (except the root) have between  M/2  and M children. 3. All leaves are at the same depth. All data records are stored at the leaves. Leaves store between  M/2  and M data records. Internal nodes only used for searching. B-Trees

12 12 B-Tree: Example B-Tree with M = 4 (# pointers in internal node) and L = 4 (# data items in leaf) 1 AB 2 xG 3569101112 1517 202526 303233364042 506070 1040 3 15203050 Note: All leaves at the same depth! Data objects, that I’ll ignore in slides

13 13 B-Tree Details Each (non-leaf) internal node of a B-tree has: –Between  M/2  and M children. –up to M-1 keys k 1  k 2  k M-1 Keys are ordered so that: k 1 < k 2 <... < k M-1 k M-1... k i-1 kiki k1k1

14 14 B-Tree Details Each leaf node of a B-tree has: –Between  M/2  and M keys and pointers. Keys are ordered so that: k 1 < k 2 <... < k M-1 kMkM... k i-1 kiki k1k1 Keys point to data on other pages.

15 15 Properties of B-Trees Children of each internal node are "between" the items in that node. Suppose subtree T i is the i-th child of the node: all keys in T i must be between keys k i-1 and k i i.e. k i-1  T i  k i k i-1 is the smallest key in T i All keys in first subtree T 1  k 1 All keys in last subtree T M  k M-1 k 1 TT ii... kk i-1 kk ii TT M TT 11 kk M-1...

16 16 Inserting into B-Trees Insert X: Do a Find on X and find appropriate leaf node –If leaf node is not full, fill in empty slot with X E.g. Insert 5 –If leaf node is full, split leaf node and adjust parents up to root node E.g. Insert 9 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:-

17 17 Insert Example 13:- 6:11 3 4 6 7 11 12 13 14 17 18 17:- 8 9

18 18 Insert Example 13:- 6:- 3 4 6 7 11 12 13 14 17 18 17:- 8 9 11:-

19 19 Insert Example 8:13 6:- 3 4 6 7 11 12 13 14 17 18 17:- 8 9 11:-

20 20 Deleting From B-Trees Delete X : Do a find and remove from leaf –Leaf underflows – borrow from a neighbor E.g. 11 –Leaf underflows and can’t borrow – merge nodes, delete parent E.g. 17 13:- 6:11 3 4 6 7 8 11 12 13 14 17 18 17:-

21 21 Delete Example 13:- 6:11 3 4 6 7 8 11 12 13 14 18 17:-

22 22 Delete Example 13:- 6:11 3 4 6 7 8 11 12 13 14 18 -:-

23 23 Delete Example 11:- 6:- 3 4 6 7 8 11 12 13 14 18 13:-

24 24 Run Time Analysis of B-Tree Operations For a B-Tree of order M –Each internal node has up to M-1 keys to search –Each internal node has between  M/2  and M children –Depth of B-Tree storing N items is O(log  M/2  N) Example: M = 86 –log 43 N = log 2 N / log 2 43 =.184 log 2 N –log 43 1,000,000,000 = 5.51

25 25 Summary of Search Trees Problem with Search Trees: Must keep tree balanced to allow –fast access to stored items AVL trees: Insert/Delete operations keep tree balanced Splay trees: Repeated Find operations produce balanced trees on average Multi-way search trees (e.g. B-Trees): More than two children –per node allows shallow trees; all leaves are at the same depth –keeping tree balanced at all times

26 26 B-Tree Properties ‡ –Data is only stored at the leaves –All leaves are at the same depth and contains between  L/2  and L data items –Internal nodes store up to M-1 keys –Internal nodes have between  M/2  and M children –Root (special case) has between 2 and M children (or root could be a leaf) ‡ These are technically B + -Trees

27 27 Example, Again B-Tree with M = 4 and L = 4 12 3569101112 1517 202526 303233364042 506070 1040 3 15203050 (Only showing keys, but leaves also have data!)

28 28 B-trees vs. AVL trees Suppose we have 100 million items (100,000,000): Depth of AVL Tree Depth of B+ Tree with M = 128, L = 64

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

30 30 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. M = 3 L = 2

31 31 Overflowing leaves Insert(59) 14 13 59 14 13 Insert(26) 14 13 2659 1459 13142659 And add a new child Too many keys in a leaf! So, split the leaf. M = 3 L = 2

32 32 Propagating Splits 1459 13142659 1459 142659 135 Insert(5) 514 2659 135 5 135 142659 14 Add new child Create a new root Split the leaf, but no space in parent! So, split the node. M = 3 L = 2

33 33 Insertion Algorithm 1.Insert the key in its leaf 2.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! 3.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! 4.Split an overflowed root in two and hang the new nodes under a new root This makes the tree deeper!

34 34 After More Routine Inserts 5 135 142659 14 5 135 265979 5989 14 89 Insert(89) Insert(79) M = 3 L = 2

35 35 Deletion 5 135 14265979 5989 14 89 5 135 142679 89 14 89 Delete(59) What could go wrong? 1.Delete item from leaf 2.Update keys of ancestors if necessary M = 3 L = 2

36 36 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 sibling M = 3 L = 2

37 37 Does Adoption Always Work? What if the sibling doesn’t have enough for you to borrow from? e.g. you have  L/2  -1 and sibling has  L/2  ?

38 38 Deletion and Merging 3 1 3 142679 8914 89 Delete(3) ? 1 142679 8914 89 1 142679 8914 89 A leaf has too few keys! And no sibling with surplus! So, delete the leaf But now an internal node has too few subtrees! M = 3 L = 2

39 39 Adopt a neighbor 1 142679 89 14 89 14 1 2679 89 79 89 Deletion with Propagation (More Adoption) M = 3 L = 2

40 40 Delete(1) (adopt a sibling) 14 1 2679 89 79 89 A Bit More Adoption 26 14 26 79 89 79 89 M = 3 L = 2

41 41 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 sibling with surplus! 14 79 89 79 89 So, delete the leaf; merge A node has too few subtrees and no neighbor with surplus! 14 79 89 Delete the node But now the root has just one subtree! M = 3 L = 2

42 42 Pulling out the Root (continued) 14 79 89 The root has just one subtree! 14 79 89 Simply make the one child the new root! M = 3 L = 2

43 43 Deletion Algorithm 1.Remove the key from its leaf 2.If the leaf ends up with fewer than  L/2  items, underflow! –Adopt data from a sibling; update the parent –If adopting won’t work, delete node and merge with neighbor –If the parent ends up with fewer than  M/2  items, underflow!

44 44 Deletion Slide Two 3.If an internal node ends up with fewer than  M/2  items, underflow! –Adopt from a neighbor; update the parent –If adoption won’t work, merge with neighbor –If the parent ends up with fewer than  M/2  items, underflow! 4.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!

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

46 46 Tree Names You Might Encounter FYI: –B-Trees with M = 3, L = x are called 2-3 trees Nodes can have 2 or 3 keys –B-Trees with M = 4, L = x are called 2-3-4 trees Nodes can have 2, 3, or 4 keys


Download ppt "CSE 326: Data Structures B-Trees Ben Lerner Summer 2007."

Similar presentations


Ads by Google