Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mainly from Chapter 7 Adam Drozdek

Similar presentations


Presentation on theme: "Mainly from Chapter 7 Adam Drozdek"— Presentation transcript:

1 Mainly from Chapter 7 Adam Drozdek
B-TREES Mainly from Chapter 7 Adam Drozdek

2 WHAT ARE B-TREES??? To understand first, lets define a MULTI-WAY TREE
Multi-way tree of order m Each node has at the most m children If a node has k children then it has k-1 keys Multi-way search tree of order m Keys are arranged in the manner of a search tree Max keys for height h is mh-1 (Root is at height 1) REF:

3 B-TREES For a tree to be a B-tree the following should hold: A B-Tree is a multi-way search tree of order m All leaves are at the last level All internal nodes have at least ceil(m/2) children The root node (if it is not the leaf node) is allowed to have minimum of two child nodes Each leaf node must have at least ceil(m/2)-1 keys All keys are sorted in a node What is the maximum number of keys we can have in order m and l levels??? REF:

4 Insertion (example of a B-tree of order 5)
Insert the following: Insert 25 10 16 18 20 18 Insert 10 16 20 25 18 2 7 10 16 19 20 25 43

5 Insertion (contd…example of a B-tree of order 5)
Insert the following: 18 Insert 1 2 7 10 16 19 20 25 43 Middle key inserted here 7 18 1 2 10 16 19 20 25 43 This node was split in two

6 Insertion in general 2 cases to handle If node is full then:
Insert in the leaf node if there is space. We are lucky. No further processing to do If node is full then: Loop till root node is reached or till empty space is found in a node (every time we go up one level) {Split the node as follows: Find the middle key (all keys including the new key to insert) Keep all keys less than middle key in the older node Keep all keys greater than middle key in the new node Insert the middle key in the parent node }

7 Look at this example (B-tree of order 4)
50 10 20 30 70 80 90 1 2 3 11 12 15 21 23 27 34 37 38 Insert 16 51 53 55 71 73 77 82 84 88 91 92 95 50 Insert the mid key 15 10 20 30 70 80 90 1 2 3 11 12 16 21 23 27 34 37 38 51 53 55 71 73 77 82 84 88 91 92 95 Split level This is parentless right now

8 Example contd... Insert the mid key 15 50 Split level
10 20 30 70 80 90 1 2 3 11 12 16 21 23 27 34 37 38 51 53 55 71 73 77 82 84 88 91 92 95 Split level This is parentless right now 20 50 10 15 30 70 80 90 1 2 3 11 12 16 21 23 27 34 37 38 Split level 51 53 55 71 73 77 82 84 88 91 92 95 New node

9 B-tree of order 5 Delete T Delete R

10 Delete E

11 AN EXAMPLE OF DELETION (order 3)
root 27 15 40 12 14 17 24 30 35 42 L M delete 42 root 27 15 40 12 14 17 24 30 35 L M n root 27 15 35 12 14 17 24 30 40 L M s perform rotation a hole is created

12 DELETION MORE EXAMPLES…
Delete 50 swap with largest value on left DELETION MORE EXAMPLES… 50 31 62 22 37 58 65 12 25 55 61 34 40 64 67 40 31 62 22 37 58 65 12 25 55 61 34 64 67 hole here 40 31 62 22 58 65 12 25 55 61 34 37 64 67 40 62 58 65 12 25 55 61 64 67 hole here hole here

13 contd…Final tree: Notice how we propagated the hole upwards
Delete 22 58 65 12 25 55 61 64 67 58 65 25 55 61 64 67 Replace with largest value on left sibling has 1 key parent has 2 keys 31 58 65 55 61 64 67

14 DELETION If it is not a terminal node then swap the value with a leaf node Delete the value from the leaf node. There are three possibilities Leaf node has lots of keys and deleting one key does not violate the properties of the tree We need to perform rotation We need to join two nodes

15 INSERTION IN GENERAL When considering insertion at one level:
Case 0: The node has space, we are done Case 1: The node curr is to be split and parent has space (can be done as one case while programming) Case 1a: The parent of curr has space for middle key of curr Case 1b: curr has all keys > than all keys of parent and parent has space for middle key of curr Case 2: The node curr is to be split (LMG are keys in curr) (M is mid key, L is preceding to M key, G is successor key of M in the same node curr). The mid key of parent of curr is M1. parent does not have space for M so it has to be split. Case 2a: The parent of A is to be split and LMG are smaller than M1 Case 2b: The parent of A is to be split and LMG are greater than M1 case 2c: The parent of A is to be split and M=M1 case 2d: Think of the extreme case

16 Case 1a INSERTION Case 1a and 1b L G L M G curr G G M .. newNode
L G New key inserted A B L M G New link curr (mid key is M) A B curr newNode Case 1b G is highest key in node G Now M is highest key in node G M .. A A curr (mid key is M) newNode curr (mid key was M)

17 INSERTION L G CASE 2a: LMG are all < M1 newNode1
Insert M here this node has no space Mid key is M1 (including M) INSERTION L G A B curr (mid key is M) CASE 2a: LMG are all < M1 newNode1 All keys are > M1 L M G A B curr newNode

18 INSERTION L G CASE 2b: LMG are all > M1 All keys are < M1
Insert M here this node has no space Mid key is M1 (including M) INSERTION L G A B curr (mid key is M) CASE 2b: LMG are all > M1 All keys are < M1 newNode1 L M G A B curr newNode

19 INSERTION L G CASE 2c: M=M1 All keys are < M newNode1 .. … L
Insert M here this node has no space Mid key is M1 (including M) INSERTION L G A B curr (mid key is M) CASE 2c: M=M1 All keys are < M newNode1 .. L G A B curr newNode

20 DELETION 2 cases Rotation: (one for general case and one for extreme position) left rotate right rotate Join hole on left hole on right

21 ROTATION (left) Y Z W X X Y Z W parent … A2 A B sibling Curr (hole)
.. Curr (hole) Y Z W X parent A3 A B sibling A2 A4 .. Curr (hole) X Y Z W

22 ROTATION (right) W X Y Z X Y Z W parent … A3 sibling A B Curr (hole)
.. Curr (hole) A1 A2 are the last (highest) keys in sibling W X Y Z parent A2 A B sibling A1 A3 .. Empty slot Curr X Y Z W

23 JOIN (hole on left) X Y W Z Y W X Z move all keys from Curr to sibling
parent A1 B empty slots sibling A1 A2 A3 .. Curr (min number of keys last key is A1) X Y Z W parent (A1 removed) B .. sibling .. A1 A2 A3 Curr (delete it) X Y Z W move all keys from Curr to sibling

24 JOIN (hole on right) Do yourself


Download ppt "Mainly from Chapter 7 Adam Drozdek"

Similar presentations


Ads by Google