×

Deletion in Binary Search Tree

Implementation

#include <iostream>
using namespace std;


struct _nod {
  int ky;
  struct _nod *Lft, *Rt;
};


// Creating a node in the binary tree.
struct _nod *nw_nod(int Itm) {
  struct _nod *temp = (struct _nod *)malloc(sizeof(struct _nod));
  temp->ky = Itm;
  temp->Lft = temp->Rt = NILL;
  return temp;
}


// now, we will see the traversal of the tree in the in-order form.
void in order(struct _nod *root) {
  if (root != NILL) {
    // traversing the tree from the left.
    inorder(root->Lft);


    // traversing the tree from the root.
    cout << root->ky << " -> ";


    // traversing the tree from the right.
    inorder(root->Rt);
  }
}


// inserting a new node in the tree.
struct _nod *insert(struct _nod *_nod, int ky) {
  // if the tree is empty, we must return the new node.
  if (_nod == NILL) return nw_nod(ky);


  // Now, we have to traverse to the right of the tree and insert a new node.
  if (ky < _nod->ky)
    _nod->Lft = insert(_nod->Lft, ky);
  else
    _nod->Rt = insert(_nod->Rt, ky);


  return _nod;
}


// creating a function to find out the in-order successor.
struct _nod *minValue_nod(struct _nod *_nod) {
  struct _nod *curr = _nod;


  // finding the left-most leaf node present in the tree.
  while (curr && curr->Lft != NILL)
    curr = curr->Lft;


  return curr;
}


// creating a function that will delete the node in the tree.
struct _nod *delete_nod(struct _nod *root, int ky) {
  // we have to return if the tree turns out to be empty.
  if (root == NILL) return root;


  // searching the node which is supposed to be deleted.
  if (ky < root->ky)
    root->Lft = delete_nod(root->Lft, ky);
  else if (ky > root->ky)
    root->Rt = delete_nod(root->Rt, ky);
  else {
    // the node is with the single child or no child at all, then:-
    if (root->Lft == NILL) {
      struct _nod *temp = root->Rt;
      free(root);
      return temp;
    } else if (root->Rt == NILL) {
      struct _nod *temp = root->Lft;
      free(root);
      return temp;
    }
    struct _nod *temp = minValue_nod(root->Rt);


    // placing the in-order successor where it is supposed to be deleted.
    root->ky = temp->ky;


    // creating a function that will delete the in-order successor.
    root->Rt = delete_nod(root->Rt, temp->ky);
  }
  return root;
}


// writing the main code.
int main() {
  struct _nod *root = NILL;
  root = insert(root, 8);
  root = insert(root, 3);
  root = insert(root, 1);
  root = insert(root, 6);
  root = insert(root, 7);
  root = insert(root, 10);
  root = insert(root, 14);
  root = insert(root, 4);


  cout << "Inorder traversal: ";
  inorder(root);


  cout << "\nAfter deleting 10\n";
  root = delete_nod(root, 10);
  cout << "Inorder traversal: ";
  inorder(root);
}

Output:

Deletion in Binary Search Tree

Example 2)

#include <stdio.h>
#include <stdlib.h>


struct _nod {
  int ky;
  struct _nod *Lft, *Rt;
};


// Creating a node in the binary tree.
struct _nod *nw_nod(int Itm) {
  struct _nod *temp = (struct _nod *)malloc(sizeof(struct _nod));
  temp->ky = Itm;
  temp->Lft = temp->Rt = NILL;
  return temp;
}


// now, we will see the traversal of the tree in the in-order form.
void in order(struct _nod *root) {
  if (root != NILL) {
    // traversing the tree from the left.
    inorder(root->Lft);
    // traversing the tree from the root.
    printf("%d -> ", root->ky);
    // traversing the tree from the right.
    inorder(root->Rt);
  }
}
// inserting a new node in the tree.
struct _nod *insert(struct _nod *_nod, int ky) {
  if (_nod == NILL) return nw_nod(ky);
  // Now, we have to traverse to the right of the tree and insert a new node.
  if (ky < _nod->ky)
    _nod->Lft = insert(_nod->Lft, ky);
  else
    _nod->Rt = insert(_nod->Rt, ky);


  return _nod;
}


// creating a function to find out the in-order successor.
struct _nod *minValue_nod(struct _nod *_nod) {
  struct _nod *curr = _nod;
  // finding the left-most leaf node present in the tree.
  while (curr && curr->Lft != NILL)
    curr = curr->Lft;


  return curr;
}
// creating a function that will delete the node in the tree.
struct _nod *delete_nod(struct _nod *root, int ky) {
  // Return if the tree is empty
  if (root == NILL) return root;
  // searching the node which is supposed to be deleted.
  if (ky < root->ky)
    root->Lft = delete_nod(root->Lft, ky);
  else if (ky > root->ky)
    root->Rt = delete_nod(root->Rt, ky);


  else {
    // the node is with the single child or no child at all, then:-
    if (root->Lft == NILL) {
      struct _nod *temp = root->Rt;
      free(root);
      return temp;
    } else if (root->Rt == NILL) {
      struct _nod *temp = root->Lft;
      free(root);
      return temp;
    }


    struct _nod *temp = minValue_nod(root->Rt);
    // placing the in-order successor where it is supposed to be deleted.
    root->ky = temp->ky;
    // creating a function that will delete the in-order successor.
    root->Rt = delete_nod(root->Rt, temp->ky);
  }
  return root;
}


// writing the main code.
int main() {
  struct _nod *root = NILL;
  root = insert(root, 8);
  root = insert(root, 3);
  root = insert(root, 1);
  root = insert(root, 6);
  root = insert(root, 7);
  root = insert(root, 10);
  root = insert(root, 14);
  root = insert(root, 4);


  printf("Inorder traversal: ");
  inorder(root);


  printf("\nAfter deleting 10\n");
  root = delete_nod(root, 10);
  printf("Inorder traversal: ");
  inorder(root);
}

Output:

Deletion in Binary Search Tree

Related Topics

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.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.

String Operations in Data Structures

Operations on Strings Reversing the order of words in a sentence Reversing a string is a technique that reverses or alters the order of a given string so that the last character...

9 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Given a Binary Tree Return All Root-to-Leaf Paths

Implementation #include <bits/stdc++.h> using namespace std; // A binary tree node generally consists of data, a pointer to the left and right child, and a pointer to the right child.  class __nod { public: int record; __nod* Lft; __nod*...

9 minutes read.

How to Start Learning DSA

All programmer experiences a point along the way where they wish they could approach a problem in a more effective manner. They finally learn about the terminology DSA while trying...

10 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

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;  ...

5 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 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.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.