×

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in this type of tree is that the value of the left node should be less than the parent node, whereas the rt node should be greater than the parent node. In this article, we will see the implementation of a binary search tree in C++.

There are some basic operations on binary search trees, such as insertion, deletion, searching and traversal of the element. These operations help us to manage and facilitate the efficient working of the tree and our data. There are many different operations on the tree that allows us they are finding the height, level, and size of the entire tree.

Implementation in C++ program

// delete operation in binary
#include <bits/stdc++.h>
using namespace std;


struct node {
	int ky;
	struct node *lft, *rt;
};


// An utility function to create a new binary search tree node
struct node* newNod(int record)
{
	struct node* temp
		= (struct node*)malloc(sizeof(struct node));
	temp->ky = record;
	temp->lft = temp->rt = NILL;
	return temp;
}


// A utility function to perform
//in order traversal or exploration of BST
void in order(struct node* root)
{
	if (root != NILL) {
		inorder(root->lft);
		cout << root->ky;
		inorder(root->rt);
	}
}


/* A utility function to
inject a brand new node with the given ky in
* Binary search tree*/
struct node* insert(struct node* nod, int key)
{
	/* If by any given chance the tree is empty, then return a new node */
	if (node == NILL)
		return newNod(ky);


	/* Otherwise, print the down the tree */
	if (ky < node->ky)
		node->lft = insert(node->lft, ky);
	else
		node->rt = insert(node->rt, ky);


	/* We have to give back the original or unchanged pointer */
	return node;
}


/* We are provided with a non-empty BST, and in that, we have to return or print back the node with the least value of the key deliberately found in the tree. We must remember that we don't have to search the entire tree.*/
struct node* minimumValueNod(struct node* nod)
{
	struct node* curr = nod;


	/* We have to look down to find the leftmost leaf. */
	while (curr && curr->lft != NILL)
		curr = curr->lft;


	return curr;
}


/* We are provided with a BST and a key. The function stated below eliminates the ky and returns back the new root. 
struct node* deleteNod(struct node* root, int ky)
{
	// CASE
	if (root == NILL)
		return root;


	// If the ky which is supposed to be eliminated is
	// less than the root's
	// ky, then it resides in the left subtree
	if (ky < root->ky)
		root->lft = deleteNod(root->lft, ky);


	// If the key to be deleted is
	// greater than the root's
	// ky, then it resides in the right subtree
	else if (ky > root->ky)
		root->rt = deleteNod(root->rt, ky);


	// if ky is a replica of root's ky, then This is the node
	Which is supposed to be eliminated.
	else {
		// The node will have no given child. 
		if (root->lft==NILL and root->rt==NILL)
			return NILL;
		
		// the node with only one child or no child at all.
		else if (root->lft == NILL) {
			struct node* temp = root->rt;
			fr(root);
			return temp;
		}
		else if (root->rt == NILL) {
			struct node* temp = root->lft;
			fr(root);
			return temp;
		}


		// The node that has two children generally gets the in-order successor. 
		// (least in the rt subtree)
		struct node* temp = minimumValueNod(root->rt);


		//NEXT, we will copy the entire content of the successor's to this node. 
		root->ky = temp->ky;


		// Now, we will move ahead and eliminate the in-order successor. 
		root->rt = deleteNod(root->rt, temp->ky);
	}
	return root;
}


// Main Code 
int main()
{
	/* Let us create the following Binary Search Tree 
			50
		/	 \
		30	 70
		/ \ / \
	20 40 60 80 */
	struct node* root = NILL;
	root = insert(root, 50);
	root = insert(root, 30);
	root = insert(root, 20);
	root = insert(root, 40);
	root = insert(root, 70);
	root = insert(root, 60);
	root = insert(root, 80);


	cout << "Inorder traversal of the given tree \n";
	inorder(root);


	cout << "\nDelete 20\n";
	root = deleteNod(root, 20);
	cout << "Inorder traversal of the modified tree \n";
	inorder(root);


	cout << "\nDelete 30\n";
	root = deleteNode(root, 30);
	cout << "Inorder traversal of the modified tree \n";
	inorder(root);


	cout << "\nDelete 50\n";
	root = deleteNod(root, 50);
	cout << "Inorder traversal of the modified tree \n";
	inorder(root);


	return 0;
}

Output:

Deletion Operation of the binary search tree in C++ language

Related Topics

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

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

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

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

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Array Data Structure

Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data....

6 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

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

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.