Top Banner
B-TREE B-TREE
40

B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Jan 06, 2018

Download

Documents

Delphia Spencer

Problem with Big `O’ notation Big ‘O’ assumes that all operations take equal time Suppose all data does not fit in memory Then some part of data may be stored on hard disk. CPU speed is in millions of instructions per second  3GHz machines common now Equals roughly 3000 million instructions per seconds Typical disk speeds about 7,200 RPM  Roughly 120 disk accesses per second So accessing disk is incredibly expensive. So we may be willing to do more computation to organize our data better and make fewer disk accesses.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

B-TREEB-TREE

Page 2: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Motivation for B-TreesMotivation for B-Trees

• So far we have assumed that we can store an entire data structure in main memory

• What if we have so much data that it won’t fit?• We will have to use disk storage but when this

happens our time complexity fails• The problem is that Big-Oh analysis assumes

that all operations take roughly equal time• This is not the case when disk access is involved

Page 3: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Problem with Big `O’ notationProblem with Big `O’ notation

• Big ‘O’ assumes that all operations take equal time• Suppose all data does not fit in memory• Then some part of data may be stored on hard disk.

• CPU speed is in millions of instructions per second 3GHz machines common now

• Equals roughly 3000 million instructions per seconds• Typical disk speeds about 7,200 RPM

Roughly 120 disk accesses per second

• So accessing disk is incredibly expensive.• So we may be willing to do more computation to organize our data

better and make fewer disk accesses.

Page 4: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Problem with binary treesProblem with binary trees

• There is no guarantee that binary trees will be balanced

If stored on disk, we have potentially O(N)disk operations

Page 5: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

M-ary TreesM-ary Trees

• Allows up to M children for each node Instead of max. 2 for binary trees

• A complete M-ary tree of N nodes has a depth of logMN

• Example of complete 5-ary tree of 31 nodes

Page 6: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

MM-ary search tree-ary search tree

• Similar to binary search tree, except that Each node has (M-1) keys to decide which of

the M branches to follow.

• Larger M smaller tree depth

• But how to make M-ary tree balanced?

Page 7: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

B-treeB-tree

• Yet another technique of making a search tree balanced.

• It is not a binary search tree• There are many new ideas.• B-tree is used in organising objects in files

and it is a part of file structure • You will read in detail the application of B-

tree in your Database course.

Page 8: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

B-treeB-tree

• B-tree is developed with many new ideas.

Page 9: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s see the BSTLet’s see the BST

5 10 15 7 2023 11 24

How do you start building the binary search tree for this set of objects?

First step to decide who should be the root node?

Then decide the root of the left subtree and root of right subtree and soon.

Page 10: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Top-down vs. bottom upTop-down vs. bottom up

• Can we think of generating a search tree that is bottom-up.

• That is, we first decide the leaf-nodes and gradually make the process evolve the root node and subtrees.

Page 11: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Formal DefinitionFormal Definition

• Structure of a nodeA node contains some keys and some pointers.Number of pointers in a node is one more than

the number of keys. pointers point to descendants.The keys are sorted in non-decreasing order.

Page 12: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

A nodeA node

key1 key2 key3 key4 key5

This is full node.

key1 key2 key3

Page 13: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Formal DefinitionFormal Definition

• B-tree of order mEvery node has a maximum of m child-nodesA node, other than the root, contains at least m/2 -1

keys and no more than m-1 keys.Every node, except the root and the leaves, has at least

m/2 child nodes.The root has atleast 2 child nodesAll leaf nodes are on the same level.A nonleaf node with k descendents contains k-1 keys

Page 14: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s build a B-tree Let’s build a B-tree

5 10 15 7 2023 11 24

Page 15: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s build a B-tree Let’s build a B-tree

5 10 15 7 2023 11 24

5

Page 16: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s build a B-tree Let’s build a B-tree

5 10 15 7 2023 11 24

5 23

Page 17: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Now the node is full Now the node is full

5 10 15 7 2023 11 24

5 10 11 15 23

Page 18: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

24 comes in 24 comes in

5 10 15 7 2023 11 24

5 10 11 15 23

Page 19: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s build a B-tree Let’s build a B-tree

5 10 15 7 2023 11 24

15

11105 2423

Page 20: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Let’s build a B-tree Let’s build a B-tree

5 10 15 7 2023 11 24

15

11105 24237

Page 21: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Searching a B-treeSearching a B-tree

• By comparing the key we decide which internal node to reach.

• At each internal node, use a linear search to decide the next descendant.

Page 22: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-treeConstructing a B-tree

Add 25 to the tree

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

12128811 22 2525

Exceeds Order. Promote middle and split.

Page 23: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

6, 14, 28 get added to the leaf nodes:

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1212

88

11 22 2525

1212

88

11 22 25256611 22 28281414

Page 24: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1212

88

22 25256611 22 28281414 28281717

Page 25: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

7, 52, 16, 48 get added to the leaf nodes

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1212

88

25256611 22 28281414

1717

77 52521616 4848

Page 26: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

Adding 68 causes us to split the right most leaf, promoting 48 to the root

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

88 1717

77662211 161614141212 5252484828282525 6868

Page 27: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

Adding 3 causes us to split the left most leaf

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

4848171788

77662211 161614141212 2525 2828 5252 686833 77

Page 28: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

Add 26, 29, 53, 55 then go into the leaves

484817178833

11 22 66 77 5252 68682525 2828161614141212 2626 2929 5353 5555

Page 29: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Constructing a B-tree (contd.)Constructing a B-tree (contd.)

Add 45 increases the trees level

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

484817178833

2929282826262525 686855555353525216161414121266 7711 22 4545

Exceeds Order. Promote middle and split.

Exceeds Order. Promote middle and split.

Page 30: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Inserting into a B-TreeInserting into a B-Tree

• Attempt to insert the new key into a leaf• If this would result in that leaf becoming too big, split the

leaf into two, promoting the median key to the leaf’s parent

• If this would result in the parent becoming too big, split the parent into two, promoting the middle key

• This strategy might have to be repeated all the way to the top

• If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

Page 31: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Delete from a B-treeDelete from a B-tree

• During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this:

If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.

If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position.

Page 32: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Delete from a B-tree (2)Delete from a B-tree (2)

• If a leaf node containing less than the minimum number of keys CONCATENATE if one of them has more than the min’

number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf

REDISTRIBUTE if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required

Page 33: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Simple leaf deletionSimple leaf deletion

1212 2929 5252

22 77 99 1515 2222 5656 6969 72723131 4343

Delete 2: Since there are enoughkeys in the node, just delete it

Assuming a 5-wayB-Tree, as before...

Page 34: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Simple non-leaf deletionSimple non-leaf deletion

1212 2929 5252

77 99 1515 2222 5656 6969 72723131 4343

Delete 52

Borrow the predecessoror (in this case) successor

5656

Page 35: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Too few keys in node and its siblingsToo few keys in node and its siblings

1212 2929 5656

77 99 1515 2222 6969 72723131 4343

Delete 72Too few keys!

Join back together

Page 36: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Too few keys in node and its siblingsToo few keys in node and its siblings

1212 2929

77 99 1515 2222 696956563131 4343

Page 37: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Enough siblingsEnough siblings

1212 2929

77 99 1515 2222 696956563131 4343

Delete 22

Demote root key andpromote leaf key

Page 38: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Enough siblingsEnough siblings

1212

292977 99 1515

3131

696956564343

Page 39: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Analysis of B-TreesAnalysis of B-Trees

• The maximum number of items in a B-tree of order m and height h:root m – 1level 1 m(m – 1)level 2 m2(m – 1). . .level h mh(m – 1)

• So, the total number of items is(1 + m + m2 + m3 + … + mh)(m – 1) =[(mh+1 – 1)/ (m – 1)] (m – 1) = mmhh+1+1 – 1 – 1

• When m = 5 and h = 2 this gives 53 – 1 = 124

Page 40: B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Reasons for using B-TreesReasons for using B-Trees

• When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferredIf we use a B-tree of order 101, say, we can transfer each node in

one disc read operationA B-tree of order 101 and height 3 can hold 1014 – 1 items

(approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)

• If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)B-Trees are always balanced (since the leaves are all at the same

level), so 2-3 trees make a good type of balanced tree