Draw the Btree of Order 3

In the previous post, we introduced B-Tree. We also discussed search() and traverse() functions.
In this post, insert() operation is discussed. A new key is always inserted at the leaf node. Let the key to be inserted be k. Like BST, we start from the root and traverse down till we reach a leaf node. Once we reach a leaf node, we insert the key in that leaf node. Unlike BSTs, we have a predefined range on the number of keys that a node can contain. So before inserting a key to the node, we make sure that the node has extra space.

How to make sure that a node has space available for a key before the key is inserted? We use an operation called splitChild() that is used to split a child of a node. See the following diagram to understand split. In the following diagram, child y of x is being split into two nodes y and z. Note that the splitChild operation moves a key up and this is the reason B-Trees grow up, unlike BSTs which grow down.

BTreeSplit

As discussed above, to insert a new key, we go down from root to leaf. Before traversing down to a node, we first check if the node is full. If the node is full, we split it to create space. Following is the complete algorithm.

Insertion
1) Initialize x as root.
2) While x is not leaf, do following
..a) Find the child of x that is going to be traversed next. Let the child be y.
..b) If y is not full, change x to point to y.
..c) If y is full, split it and change x to point to one of the two parts of y. If k is smaller than mid key in y, then set x as the first part of y. Else second part of y. When we split y, we move a key from y to its parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as we have been splitting all nodes in advance. So simply insert k to x.

Note that the algorithm follows the Cormen book. It is actually a proactive insertion algorithm where before going down to a node, we split it if it is full. The advantage of splitting before is, we never traverse a node twice. If we don't split a node before going down to it and split it only if a new key is inserted (reactive), we may end up traversing all nodes again from leaf to root. This happens in cases when all nodes on the path from the root to leaf are full. So when we come to the leaf node, we split it and move a key up. Moving a key up will cause a split in parent node (because the parent was already full). This cascading effect never happens in this proactive insertion algorithm. There is a disadvantage of this proactive insertion though, we may do unnecessary splits.

Let us understand the algorithm with an example tree of minimum degree 't' as 3 and a sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
Initially root is NULL. Let us first insert 10.

Btree1

Let us now insert 20, 30, 40 and 50. They all will be inserted in root because the maximum number of keys a node can accommodate is 2*t – 1 which is 5.

BTree2Ins

Let us now insert 60. Since root node is full, it will first split into two, then 60 will be inserted into the appropriate child.

BTreeIns3

Let us now insert 70 and 80. These new keys will be inserted into the appropriate leaf without any split.

BTreeIns4

Let us now insert 90. This insertion will cause a split. The middle key will go up to the parent.

BTreeIns6

Following is C++ implementation of the above proactive algorithm.

C++

#include<iostream>

using namespace std;

class BTreeNode

{

int *keys;

int t;

BTreeNode **C;

int n;

bool leaf;

public :

BTreeNode( int _t, bool _leaf);

void insertNonFull( int k);

void splitChild( int i, BTreeNode *y);

void traverse();

BTreeNode *search( int k);

friend class BTree;

};

class BTree

{

BTreeNode *root;

int t;

public :

BTree( int _t)

{  root = NULL;  t = _t; }

void traverse()

{ if (root != NULL) root->traverse(); }

BTreeNode* search( int k)

{ return (root == NULL)? NULL : root->search(k); }

void insert( int k);

};

BTreeNode::BTreeNode( int t1, bool leaf1)

{

t = t1;

leaf = leaf1;

keys = new int [2*t-1];

C = new BTreeNode *[2*t];

n = 0;

}

void BTreeNode::traverse()

{

int i;

for (i = 0; i < n; i++)

{

if (leaf == false )

C[i]->traverse();

cout << " " << keys[i];

}

if (leaf == false )

C[i]->traverse();

}

BTreeNode *BTreeNode::search( int k)

{

int i = 0;

while (i < n && k > keys[i])

i++;

if (keys[i] == k)

return this ;

if (leaf == true )

return NULL;

return C[i]->search(k);

}

void BTree::insert( int k)

{

if (root == NULL)

{

root = new BTreeNode(t, true );

root->keys[0] = k;

root->n = 1;

}

else

{

if (root->n == 2*t-1)

{

BTreeNode *s = new BTreeNode(t, false );

s->C[0] = root;

s->splitChild(0, root);

int i = 0;

if (s->keys[0] < k)

i++;

s->C[i]->insertNonFull(k);

root = s;

}

else

root->insertNonFull(k);

}

}

void BTreeNode::insertNonFull( int k)

{

int i = n-1;

if (leaf == true )

{

while (i >= 0 && keys[i] > k)

{

keys[i+1] = keys[i];

i--;

}

keys[i+1] = k;

n = n+1;

}

else

{

while (i >= 0 && keys[i] > k)

i--;

if (C[i+1]->n == 2*t-1)

{

splitChild(i+1, C[i+1]);

if (keys[i+1] < k)

i++;

}

C[i+1]->insertNonFull(k);

}

}

void BTreeNode::splitChild( int i, BTreeNode *y)

{

BTreeNode *z = new BTreeNode(y->t, y->leaf);

z->n = t - 1;

for ( int j = 0; j < t-1; j++)

z->keys[j] = y->keys[j+t];

if (y->leaf == false )

{

for ( int j = 0; j < t; j++)

z->C[j] = y->C[j+t];

}

y->n = t - 1;

for ( int j = n; j >= i+1; j--)

C[j+1] = C[j];

C[i+1] = z;

for ( int j = n-1; j >= i; j--)

keys[j+1] = keys[j];

keys[i] = y->keys[t-1];

n = n + 1;

}

int main()

{

BTree t(3);

t.insert(10);

t.insert(20);

t.insert(5);

t.insert(6);

t.insert(12);

t.insert(30);

t.insert(7);

t.insert(17);

cout << "Traversal of the constructed tree is " ;

t.traverse();

int k = 6;

(t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present" ;

k = 15;

(t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present" ;

return 0;

}

Output:

Traversal of the constructed tree is  5 6 7 10 12 17 20 30 Present Not Present

References:
Introduction to Algorithms 3rd Edition by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture17.html
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


palmoresaws1972.blogspot.com

Source: https://www.geeksforgeeks.org/insert-operation-in-b-tree/

0 Response to "Draw the Btree of Order 3"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel