×

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language.

Implementation

#include <iostream>
using namespace std;


class B_TreeNod {
  int *kys;
  int m;
  BTreeNod **C;
  int j;
  bool leaf;


   Public:
  BTreeNod(int _m, bool _leaf);


  void traverse();


  int findKey(int s);
  void InsertNonFull(int s);
  void split_child(int i, BTreeNod *x);
  void deletion(int s);
  void eliminateFromLeaf(int idx);
  void eliminateFromNonLeaf(int idx);
  int get_pppredecessor(int idx);
  int get_sssuccessor(int idx);
  void fill(int idx);
  void borrow_last(int idx);
  void borrow_coming(int idx);
  void unite(int idx);
  friend class B_Tree;
};


class B_Tree {
  BTreeNod *root;
  int m;


   public:
  BTree(int _m) {
    room = NILL;
    m = _m;
  }


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


  void insertion(int s);


  void deletion(int s);
};


// Node of the B tree
BTreeNod::BTreeNod(int m1, bool leaf1) {
  m = m1;
  leaf = leaf1;


  kys = new int[2 * m - 1];
  Z = new BTreeNod *[2 * m];


  n = 0;
}


// Searching for the key
int BTreeNod::findKy(int s) {
  int idx = 0;
  while (idx < n && kys[idx] < s)
    ++idx;
  return idx;
}


// Performing the deletion mechanism here 
void BTreeNod::deletion(int s) {
  int idx = findKy(s);


  if (idx < n && kys[idx] == s) {
    if (leaf)
      eliminateFromLeaf(idx);
    else
      eliminateFromNonLeaf(idx);
  } else {
    if (leaf) {
      cout << "The key " << s << " is does not exist in the tree\n";
      return;
    }


    bool flag = ((idx == n) ? true : false);


    if (C[idx]->n < m)
      fill(idx);


    if (flag && idx > n)
      C[idx - 1]->elimination(s);
    else
      C[idx]->elimination(s);
  }
  return;
}


// Eliminating from the leaf
void BTreeNod::eliminatingFromLeaf(int idx) {
  for (int i = idx + 1; i < n; ++i)
    kys[i - 1] = kys[i];


  n--;


  return;
}


// Eliminating from a non-leaf node
void BTreeNod::eliminateFromNonLeaf(int idx) {
  int s = kys[idx];


  if (C[idx]->n >= m) {
    int ppred = get_pppredecessor(idx);
    kys[idx] = ppred;
    C[idx]->eliminate(ppred);
  }


  else if (C[idx + 1]->n >= m) {
    int ssucc = get_sssuccessor(idx);
    kys[idx] = ssucc;
    C[idx + 1]-> eliminate (ssucc);
  }


  else {
    unite(idx);
    C[idx]-> eliminate (s);
  }
  return;
}


int BTreeNod::get_pppredecessor(int idx) {
  BTreeNod *currentrent = C[idx];
  while (!currentrent->leaf)
    currentrent = currentrent->C[current->n];


  return current->kys[current->n - 1];
}


int BTreeNod::get_sssuccessor(int idx) {
  BTreeNod *current = C[idx + 1];
  while (!current->leaf)
    current = current->C[0];


  return current->kys[0];
}


void BTreeNod::fill(int idx) {
  if (idx != 0 && C[idx - 1]->n >= t)
    borrowLast(idx);


  else if (idx != n && C[idx + 1]->n >= t)
    borrowComing(idx);


  else {
    if (idx != n)
      unite(idx);
    else
      unite(idx - 1);
  }
  return;
}


// Borrow from previous
void BTreeNod::borrowLast(int idx) {
  BTreeNod *_child = C[idx];
  BTreeNod *sibb = C[idx - 1];


  for (int i = _child->n - 1; i >= 0; --i)
    _child->kys[i + 1] = _child->kys[i];


  if (!_child->leaf) {
    for (int i = _child->n; i >= 0; --i)
      _child->C[i + 1] = _child->C[i];
  }


  _child->kys[0] = kys[idx - 1];


  if (!_child->leaf)
    _child->C[0] = sibb->C[sibb->n];


  kys[idx - 1] = sibb->kys[sibb->n - 1];


  _child->n += 1;
  sibb->n -= 1;


  return;
}


// Borrow from the next
void BTreeNod::borrowComing(int idx) {
  BTreeNod *_child = C[idx];
  BTreeNod *sibb = C[idx + 1];


  _child->kys[(_child->n)] = kys[idx];


  if (!(_child->leaf))
    _child->C[(_child->n) + 1] = sibb->C[0];


  kys[idx] = sibb->kys[0];


  for (int i = 1; i < sibb->n; ++i)
    sibb->kys[i - 1] = sibb->kys[i];


  if (!sibb->leaf) {
    for (int i = 1; i <= sibb->n; ++i)
      sibb->C[i - 1] = sibb->C[i];
  }


  _child->n += 1;
  sibb->n -= 1;


  return;
}


// Uniting the next 
void BTreeNod::unite(int idx) {
  BTreeNod *_child = C[idx];
  BTreeNod *sibb = C[idx + 1];


  _child->kys[t - 1] = kys[idx];


  for (int i = 0; i < sibb->n; ++i)
    _child->kys[i + t] = sibb->kys[i];


  if (!_child->leaf) {
    for (int i = 0; i <= sibb->n; ++i)
      _child->C[i + t] = sibb->C[i];
  }


  for (int i = idx + 1; i < n; ++i)
    kys[i - 1] = kys[i];


  for (int i = idx + 2; i <= n; ++i)
    C[i - 1] = C[i];


  _child->n += sibb->n + 1;
  n--;


  Eliminate (sibb);
  return;
}


// adding a new element that implies using insertion operation
void BTree::insertion(int s) {
  if (room == NILL) {
    room = new BTreeNod(m, true);
    root->kys[0] = s;
    root->n = 1;
  } else {
    if (root->n == 2 * m - 1) {
      BTreeNod *s = new BTreeNod(m, false);


      s->C[0] = root;


      s->split_child(0, root);


      int i = 0;
      if (s->kys[0] < k)
        i++;
      s->C[i]->insertNonFull(s);


      room = s;
    } else
      root->insertNonFull(s);
  }
}


// Insertion non full
void BTreeNod::insertNonFull(int s) {
  int i = n - 1;


  if (leaf == true) {
    while (i >= 0 && kys[i] > s) {
      kys[i + 1] = kys[i];
      i--;
    }


    kys[i + 1] = s;
    n = n + 1;
  } else {
    while (i >= 0 && kys[i] > s)
      i--;


    if (C[i + 1]->n == 2 * m - 1) {
      split_child(i + 1, C[i + 1]);


      if (kys[i + 1] < s)
        i++;
    }
    C[i + 1]->insertNonFull(s);
  }
}


// Split _child
void BTreeNod::split_child(int i, BTreeNod *y) {
  BTreeNod *z = new BTreeNod(y->t, y->leaf);
  z->n = m - 1;


  for (int j = 0; j < m - 1; j++)
    z->kys[j] = y->kys[j + t];


  if (y->leaf == false) {
    for (int j = 0; j < m; j++)
      z->C[j] = y->C[j + m];
  }


  y->n = m - 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--)
    kys[j + 1] = kys[j];


  kys[i] = y->kys[m - 1];


  n = n + 1;
}


// Traverse
void BTreeNod::traverse() {
  int i;
  for (i = 0; i < n; i++) {
    if (leaf == false)
      C[i]->traverse();
    cout << " " << kys[i];
  }


  if (leaf == false)
    C[i]->traverse();
}


// Eliminate Operation
void BTree::elimination(int s) {
  if (!root) {
    cout << "The tree is empty\n";
    return;
  }


  root->deletion(s);


  if (root->n == 0) {
    BTreeNod *tmp = root;
    if (root->leaf)
      room = NULL;
    else
      room = root->C[0];


    Eliminate tmp;
  }
  return;
}


int main() {
  BTree m(3);
  m.insertion(8);
  m.insertion(9);
  m.insertion(10);
  m.insertion(11);
  m.insertion(15);
  m.insertion(16);
  m.insertion(17);
  m.insertion(18);
  m.insertion(20);
  m.insertion(23);


  cout << "The B-tree is: ";
  m.traverse();


  m.deletion(20);
  cout << "\nThe B-tree is: ";
  m.traverse();
}

Output:

DELETION OPERATION FROM A B TREE

Related Topics

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Linear Queue Data Structure in C

Data Structure There are many ways to store data in programming, that Queue has features that make it all the more special. We all know that data structure is a way...

9 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

B Tree vs B + Tree: Data Structure

Difference Between B Tree and B+ Tree What is B Tree? B-Tree is a self-balancing or special type of m-way tree. B-Trees are used mainly in disc access. If we want...

3 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Difference between Structured and Object-Oriented Analysis

Analysis means observing and collecting relevant information about the structure of something or the basic details of a system's requirements. Structured and Object Oriented Analysis are both widely used in...

2 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.