×

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

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

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.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Why is Binary Heap Preferred over BST for Priority Queue

A priority queue is a linear and ordered collection of elements in which each element has an attribute named priority and the priority attribute decides the order in which elements...

2 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

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

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.