×

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

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Timsort

TimSort Time Complexity Timsort is a sorting algorithm that is quite efficient for real-world data. Timsort is created in 2001 by Tim Peters for the python programming language. Timsort is a...

3 minutes read.

Finding the Minimum and Maximum Value of a Binary Tree

Implementation // Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.  #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new class tree node. class...

5 minutes read.

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

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

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.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.