Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

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



ADVERTISEMENT
ADVERTISEMENT