B+tree by Prabhat Raghav

32
Prabhat Raghav 08/07/16 1 B+ Trees Similar to B trees, with a few slight differences All data is stored at the leaf nodes (leaf pages); all other nodes (index pages) only store keys Leaf pages are linked to each other Keys may be duplicated; every key to the right of a particular key is >= to that key

Transcript of B+tree by Prabhat Raghav

Page 1: B+tree by Prabhat Raghav

Prabhat Raghav 08/07/16 1

B+ Trees

Similar to B trees, with a few slight differences

All data is stored at the leaf nodes (leaf pages); all other nodes (index pages) only store keys

Leaf pages are linked to each other Keys may be duplicated; every key to the

right of a particular key is >= to that key

Page 2: B+tree by Prabhat Raghav

2

B+ Tree Example9, 16

2, 7 12 18

1 7

93, 4, 6 12

16 19

Prabhat Raghav 08/07/16

Page 3: B+tree by Prabhat Raghav

3

B+ Tree Insertion

Insert at bottom level If leaf page overflows, split page and copy

middle element to next index page If index page overflows, split page and move

middle element to next index page

Prabhat Raghav 08/07/16

Page 4: B+tree by Prabhat Raghav

4

B+ Tree Insertion Example9, 16

2, 7 12 18

1 7

93, 4, 6 12

16 19

Insert 5

Prabhat Raghav 08/07/16

Page 5: B+tree by Prabhat Raghav

5

B+ Tree Insertion Example9, 16

2, 7 12 18

1 7

93, 4, 5,6 12

16 19

Insert 5

Prabhat Raghav 08/07/16

Page 6: B+tree by Prabhat Raghav

6

B+ Tree Insertion Example9, 16

2, 5, 7 12 18

1 7

93, 4

12

16 19

Split page, copy 5

5, 6Prabhat Raghav 08/07/16

Page 7: B+tree by Prabhat Raghav

7

B+ Tree Insertion Example 2

9, 13, 16

93, 4, 6 14 16, 18, 20

Insert 17

Prabhat Raghav 08/07/16

Page 8: B+tree by Prabhat Raghav

8

B+ Tree Insertion Example 2Insert 17

9, 13, 16

93, 4, 6 14 16, 17, 18, 20

Prabhat Raghav 08/07/16

Page 9: B+tree by Prabhat Raghav

9

B+ Tree Insertion Example 2Split leaf

page, copy 18

9, 13, 16, 18

93, 4, 6 14 16, 17 18, 20

Prabhat Raghav 08/07/16

Page 10: B+tree by Prabhat Raghav

10

B+ Tree Insertion Example 2Split index

page, move 13

16, 18

93, 4, 6 14

9

13

16, 17 18, 20

Prabhat Raghav 08/07/16

Page 11: B+tree by Prabhat Raghav

11

B+ Tree Deletion

Delete key and data from leaf page If leaf page underflows, merge with sibling

and delete key in between them If index page underflows, merge with sibling

and move down key in between them

Prabhat Raghav 08/07/16

Page 12: B+tree by Prabhat Raghav

12

B+ Tree Deletion ExampleRemove 9

93, 4, 6

9

13

16, 18

14 16, 17 18, 20

Prabhat Raghav 08/07/16

Page 13: B+tree by Prabhat Raghav

13

B+ Tree Deletion ExampleRemove 9

3, 4, 6

9

13

16, 18

14 16, 17 18, 20

Prabhat Raghav 08/07/16

Page 14: B+tree by Prabhat Raghav

14

B+ Tree Deletion ExampleLeaf page underflow, so merge with sibling

and remove 9

3, 4, 6

13

16, 18

14 16, 17 18, 20

Prabhat Raghav 08/07/16

Page 15: B+tree by Prabhat Raghav

15

B+ Tree Deletion ExampleIndex page underflow, so merge with sibling

and demote 13

13, 16, 18

3, 4, 6 14 16, 17 18, 20

Prabhat Raghav 08/07/16

Page 16: B+tree by Prabhat Raghav

16

Threaded Trees

Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers

We can use these pointers to help us in inorder traversals

We have the pointers reference the next node in an inorder traversal; called threads

We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer

Prabhat Raghav 08/07/16

Page 17: B+tree by Prabhat Raghav

17

Threaded Tree Code

Example code:

class Node { Node left, right; boolean leftThread, rightThread;}

Prabhat Raghav 08/07/16

Page 18: B+tree by Prabhat Raghav

18

Threaded Tree Example

8

753

11

13

1

6

9

Prabhat Raghav 08/07/16

Page 19: B+tree by Prabhat Raghav

19

Threaded Tree Traversal

We start at the leftmost node in the tree, print it, and follow its right thread

If we follow a thread to the right, we output the node and continue to its right

If we follow a link to the right, we go to the leftmost node, print it, and continue

Prabhat Raghav 08/07/16

Page 20: B+tree by Prabhat Raghav

20

Threaded Tree Traversal

8

753

11

13

1

6

9

Start at leftmost node, print it

Output1

Prabhat Raghav 08/07/16

Page 21: B+tree by Prabhat Raghav

21

Threaded Tree Traversal

8

753

11

13

1

6

9

Follow thread to right, print node

Output13

Prabhat Raghav 08/07/16

Page 22: B+tree by Prabhat Raghav

22

Threaded Tree Traversal

8

753

11

13

1

6

9Follow link to right, go to leftmost node and print

Output135

Prabhat Raghav 08/07/16

Page 23: B+tree by Prabhat Raghav

23

Threaded Tree Traversal

8

753

11

13

1

6

9

Follow thread to right, print node

Output1356

Prabhat Raghav 08/07/16

Page 24: B+tree by Prabhat Raghav

24

Threaded Tree Traversal

8

753

11

13

1

6

9Follow link to right, go to leftmost node and print

Output13567

Prabhat Raghav 08/07/16

Page 25: B+tree by Prabhat Raghav

25

Threaded Tree Traversal

8

753

11

13

1

6

9

Follow thread to right, print node

Output135678

Prabhat Raghav 08/07/16

Page 26: B+tree by Prabhat Raghav

26

Threaded Tree Traversal

8

753

11

13

1

6

9Follow link to right, go to leftmost node and print

Output1356789

Prabhat Raghav 08/07/16

Page 27: B+tree by Prabhat Raghav

27

Threaded Tree Traversal

8

753

11

13

1

6

9

Follow thread to right, print node

Output135678911

Prabhat Raghav 08/07/16

Page 28: B+tree by Prabhat Raghav

28

Threaded Tree Traversal

8

753

11

13

1

6

9Follow link to right, go to leftmost node and print

Output13567891113

Prabhat Raghav 08/07/16

Page 29: B+tree by Prabhat Raghav

29

Threaded Tree Traversal CodeNode leftMost(Node n) { Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans;}

void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); } }}

Prabhat Raghav 08/07/16

Page 30: B+tree by Prabhat Raghav

30

Threaded Tree Modification

We’re still wasting pointers, since half of our leafs’ pointers are still null

We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals

Prabhat Raghav 08/07/16

Page 31: B+tree by Prabhat Raghav

31

Threaded Tree Modification

8

753

11

13

1

6

9

Prabhat Raghav 08/07/16

Page 32: B+tree by Prabhat Raghav

Thank You!

32Prabhat Raghav 08/07/16