You are on page 1of 43

File Structures

Balanced Trees

B-Tree
Balanced Trees
A multi level indexed tree having
Every node has m descendants
Every node except has at least m/2
descendents
The root has at least 2 descendants unless it s
a leaf
All leaves appear on same level

February 1 & 3

Creation of B-Tree
Basic algorithm
Insert every element from list to node until it is
full
Once it is full, split the node into 2 nodes ,
distribute the keys equally among the next
nodes
From each node , promote the highest element
to parent node
Continue the process until the list is complete

February 1 & 3

Creation of B-Tree
C,S,D,T,A,M,P,I,B,W,N,G,U,R,K,E,H,O
,L,J,Y,Q,Z,F,X,V
ORDER 4

February 1 & 3

B.Meena,Asst.Prof.,IT Dept,ANITS

B.Meena,Asst.Prof.,IT Dept,ANITS

B.Meena,Asst.Prof.,IT Dept,ANITS

B.Meena,Asst.Prof.,IT Dept,ANITS

B.Meena,Asst.Prof.,IT Dept,ANITS

B.Meena,Asst.Prof.,IT Dept,ANITS

10

B.Meena,Asst.Prof.,IT Dept,ANITS

11

B.Meena,Asst.Prof.,IT Dept,ANITS

12

B.Meena,Asst.Prof.,IT Dept,ANITS

13

B.Meena,Asst.Prof.,IT Dept,ANITS

14

B.Meena,Asst.Prof.,IT Dept,ANITS

15

B.Meena,Asst.Prof.,IT Dept,ANITS

16

B.Meena,Asst.Prof.,IT Dept,ANITS

17

B.Meena,Asst.Prof.,IT Dept,ANITS

18

B.Meena,Asst.Prof.,IT Dept,ANITS

19

B.Meena,Asst.Prof.,IT Dept,ANITS

20

B.Meena,Asst.Prof.,IT Dept,ANITS

21

B.Meena,Asst.Prof.,IT Dept,ANITS

22

B.Meena,Asst.Prof.,IT Dept,ANITS

23

B.Meena,Asst.Prof.,IT Dept,ANITS

24

B.Meena,Asst.Prof.,IT Dept,ANITS

25

B.Meena,Asst.Prof.,IT Dept,ANITS

26

B.Meena,Asst.Prof.,IT Dept,ANITS

27

B.Meena,Asst.Prof.,IT Dept,ANITS

28

B-Tree Example

Operations
B-Tree of order 4
Each node has at most 4 pointers and 3 keys, and at
least 2 pointers and 1 key.

Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8


Delete: 2, 21, 10, 3, 4

Insert 5, 3, 21
*5*

*3*5*

* 3 * 5 * 21 *

Insert 9
*9*

c
*3*5*

Node a splits creating 2 children: b and c

* 21 *

Insert 1, 13
*9*

c
*1*3*5*

Nodes b and c have room to insert more elements

* 13 * 21 *

Insert 2
*3*9*
b

d
*1*2*

c
*5*

Node b has no more room, so it splits creating node d.

* 13 * 21 *

Insert 7, 10
*3*9*
b

d
*1*2*

c
*5*7*

Nodes d and c have room to add more elements

* 10 * 13 * 21 *

Insert 12
* 3 * 9 * 13 *
b

d
*1*2*

*5*7*

Nodes c must split into nodes c and e

a
c

* 10 * 12 *

* 21 *

Insert 4
* 3 * 9 * 13 *
b

d
*1*2*

*4*5*7*

Node d has room for another element

c
* 10 * 12 *

e
* 21 *

Insert 8
*9*

g
*3*7*

d
*1*2*

* 13 *

h
*4*5*

*8*

e
* 10 * 12 *

* 21 *

Node d must split into 2 nodes. This causes node a to split into 2 nodes and the tree grows a level.

Delete 2
*9*

g
*3*7*

d
*1*

* 13 *

h
*4*5*

*8*

Node b can loose an element without underflow.

e
* 10 * 12 *

* 21 *

Delete 21
*9*

g
*3*7*

d
*1*

* 12 *

h
*4*5*

*8*

e
* 10 *

* 13 *

Deleting 21 causes node e to underflow, so elements are redistributed between nodes c, g, and
e

Delete 10
*3*7*9*

b
*1*

h
*4*5*

e
*8*

* 12 * 13 *

Deleting 10 causes node c to underflow. This causes the parent, node g to recombine with
nodes f and a. This causes the tree to shrink one level.

Delete 3
*4*7*9*

b
*1*

h
*5*

e
*8*

* 12 * 13 *

Because 3 is a pointer to nodes below it, deleting 3 requires keys to be redistributed between
nodes a and d.

Delete 4
*7*9*

h
*1*5*

*8*

e
* 12 * 13 *

Deleting 4 requires a redistribution of the keys in the subtrees of 4; however, nodes b and d do
not have enough keys to redistribute without causing an underflow. Thus, nodes b and d must
be combined.

You might also like