Você está na página 1de 23

Lecture 6.

2: B+ Tree
Root
Index Entries

Data Entries

Example B+ Tree
Search begins at root, and key comparisons direct it to a leaf. Search for
5* 19* , all data entries >= 24* ...
13 17 24 30

2*

3*

5*

7*

14* 16*

19* 20* 22*

24* 27* 29*

33* 34* 38* 39*

Inserting a Data Entry into a B+ Tree


Find correct leaf X. Put data entry onto X.
If X has enough space, done! Else, must split X (into X and a new node X2)
Redistribute entries evenly, copy up middle key. Insert index entry pointing to X2 into parent of X.

This can happen recursively


To split index node, redistribute entries evenly, but push up middle key.

Insertion in a B+ Tree
Insert (K, P) Find leaf where K belongs, insert If no overflow (2t keys or less), halt. If overflow (2t+1 keys), split node, insert in parent:
(K3
K1 P1 P2 K2 P3 K3 P4 K4 P5 K5 p6 K1 P1 K2 P2 P3 K4 P4

) to parent
K5 P5 p6

If leaf, keep K3 too in right node When root splits, new root has 1 key only

Insertion in a B+ Tree
Bc ca cy t=2 Insert K=19
80

20

60

100

120

140

10

15

18

20

30

40

50

60

65

80

8 5

90

10

15

18

20

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
After insertion
80

20

60

100

120

140

10

15

18

19

20

30

40

50

60

65

80

85

90

10

15

18

19

20

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
Now insert 25
80

20

60

100

120

140

10

15

18

19

20

30

40

50

60

65

80

85

90

10

15

18

19

20

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
After insertion
80

20

60

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
But now have to split !
80

20

60

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
After the split
80

20

30

60

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
Another B+ Tree
80

20

30

60

70

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
Now Insert 12
80

20

30

60

70

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Insertion in a B+ Tree
Need to split leaf
80

20

30

60

70

100

12 0

140

10

1 2

15

1 8

1 9

20

2 5

30

4 0

5 0

10

12

15

18

19

20

25

30

40

50

Insertion in a B+ Tree
Need to split branch
80

15

2 0

30

6 0

7 0

100

12 0

140

10

1 2

15

1 8

1 9

20

2 5

30

4 0

5 0

10

12

15

18

19

20

25

30

40

50

Insertion in a B+ Tree
After split
30 80

15

20

60

70

100

12 0

140

10

1 2

15

1 8

1 9

20

2 5

30

4 0

5 0

10

12

15

18

19

20

25

30

40

50

Deleting a Data Entry from a B+ Tree


Start at root, find leaf X where entry belongs. Remove the entry.
If X is at least half-full, done! If X has only t-1 entries, Try to re-distribute, borrowing from sibling (adjacent node with same parent as X). If re-distribution fails, merge X and sibling.

If merge occurred, must delete entry (pointing to X or sibling) from parent of X. Merge could propagate to root, decreasing height.

Deletion from a B+ Tree


Bc ca cy t=2 Delete 30

80

20

30

60

100

12 0

140

10

1 5

18

19

20

2 5

30

4 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

30

40

50

60

65

80

85

90

Deletion from a B+ Tree


After deleting 30
May change to 40, or not 20 30 60 80

100

12 0

140

10

1 5

18

19

20

2 5

40

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

40

50

60

65

80

85

90

Deletion from a B+ Tree


Now delete 25
80

20

30

60

100

12 0

140

10

1 5

18

19

20

2 5

40

5 0

60

6 5

80

8 5

90

10

15

18

19

20

25

40

50

60

65

80

85

90

Deletion from a B+ Tree


After deleting 25 Need to rebalance Rotate
20 30 60

80

100

12 0

140

10

1 5

18

19

20

40

5 0

60

6 5

80

8 5

90

10

15

18

19

20

40

50

60

65

80

85

90

Deletion from a B+ Tree


Now delete 40
80

19

30

60

100

12 0

140

10

1 5

18

19

2 0

40

5 0

60

6 5

80

8 5

90

10

15

18

19

20

40

50

60

65

80

85

90

Deletion from a B+ Tree


After deleting 40 Rotation not possible Need to merge nodes
19 30 60

80

100

12 0

140

10

1 5

18

19

2 0

50

60

6 5

80

8 5

90

10

15

18

19

20

50

60

65

80

85

90

Deletion from a B+ Tree


Final tree
80

19

60

100

12 0

140

10

1 5

18

19

2 0

5 0

60

6 5

80

8 5

90

10

15

18

19

20

50

60

65

80

85

90

Você também pode gostar