Updated March 14, 2023

## Introduction to Binary Tree Deletion

Binary tree deletion is known as the delete a node from the binary tree. Delete a node from a binary tree shrinks the tree from the rightmost bottom. That means if you delete a node from a binary tree, it will be replaced by the rightmost bottom node. This deletion is something different from the binary search tree. Unlike the binary search tree, the binary tree does not have any order; that’s why we replace delete a node from the rightmost bottom node. To delete a node, first, we will start from the root and go for the rightmost bottom node. Then we will search the node which we want to delete and replace it with the rightmost bottom node. Then finally, we will delete the rightmost bottom node.

**Syntax of Binary Tree Deletion**

Given below is the syntax mentioned:

`void deleteNode(Node *root, int data)`

{

if(root == NULL)

{

Cout << “Tree is empty\n”;

return;

}

queue<Node*> q;

q.push(root);

while(!q.empty())

{

Node *temp = q.front();

q.pop();

if(temp->data == data)

{

Node *current = root;

Node *prev;

while(current->right != NULL)

{

prev = current;

current = current->right;

}

temp ->data = current->data;

prev->right = NULL;

free(current);

cout << “Deleted\n”;

return;

}

if(temp->left != NULL)

q.push(temp->left);

if(temp->right != NULL)

q.push(temp->right);

}

cout << “Node not found for deletion\n”;

}

### Binary Tree Traversal

A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal.

We will consider several traversal algorithms with a group in the following two kinds:

- depth-first traversal
- breadth-first traversal

There are three different types of depth-first traversal:

**PreOdrer traversal:**Visit the parent first and then the left and right child.**InOrder traversal:**Visit the left child, then the parent and the right child.**PostOrder traversal:**Visit the left child, then the right child and then the parent.

There is only one kind of breadth-first traversal: The level order traversal. This traversal visits nodes by levels from top to bottom and from left to right.

### How to Perform Binary Tree Deletion?

To perform binary tree deletion, first, we will maintain a queue and use the push root node into it. Then, while q is not empty, we will do the processing. Let’s say we keep two pointers, key_node denoted as Node*, NULL pointer and last node or deepest node.

Now, we will pick one element, let’s say the current node and the current node will always be the element that we pop from the queue. We will check if this value matches with the key which we want to delete. Every node has some data of some data type, then left and right pointers. So we will compare this data of the current node with the deletion key; if it matches, then we will update key_node. So, if the current node data is equal to the key, then the key node is equal to the current node. When this loop ends, the queue is empty, and every node has been processed. So whatever was the last value of last_node, that will denote the leaf node of the tree.

### Algorithm For Binary Tree Deletion

Given below is the algorithm of Binary Tree Deletion:

Algorithm Bideletionseq(key)

{

//key is node to be deleted

l = BTSearchseq(i, key);

if((A[2*l] == NULL) && A[2*l +1] == NULL) then

A[l] = NULL;

else

printf(“the node not present”);

}

Algorithm Bideletionlink(item)

{

//item is a data to be deleted

Parent = BTSearchlink(root, item);

{

if(parent→lchild != NULL && parent→rchild != NULL)

{

ptr1 = parent→lchild;

ptr2 = parent→rchild;

if(ptr1→child == NULL && ptr1→ rchild == NULL)

{

ptr1→lchild = NULL;

}

if(ptr2→ lchild == NULL && ptr2→rchild == NULL) then

{

ptr2→rchild = NULL;

}

}

else

printf(“deletion is not possible”);

}

**Example:**

1

/ \

2 3

/ /

4 5

\

6

So this is the binary tree in which we have to delete node with data 3 in the inorder traversal.

**Code for deletion in C++:**

`#include<iostream>`

#include<queue>

using namespace std;

struct Node{

int data;

Node *left;

Node *right;

Node(int val): data(val), left(NULL), right(NULL){}

~Node(){}

};

void inorder(Node *root){

if(!root){

return;

}

inorder(root->left);

std::cout<<root->data<<"\t";

inorder(root->right);

}

void remove_node(Node *root, Node *n){

if(root == NULL)

return;

if(root == n){

delete n;

root = NULL;

return;

}

if(root->left == n){

delete n;

root->left = NULL;

return;

}

if(root->right == n){

delete n;

root->right = NULL;

return;

}

remove_node(root->left, n);

remove_node(root->right, n);

}

Node* delete_node(Node *root, int key){

if(root == NULL)

return NULL;

if(!root->left && !root->right){

if(root->data == key){

delete root;

root = NULL;

return root;

}

return root;

}

std::queue<Node *>Q;

Q.push(root);

Node *key_node = NULL;

Node *curr_node = NULL;

while(!Q.empty()){

curr_node = Q.front();

Q.pop();

if(curr_node->data == key){

key_node = curr_node;

}

if(curr_node->left)

Q.push(curr_node->left);

if(curr_node->right)

Q.push(curr_node->right);

}

if(key_node){

key_node->data = curr_node->data;

remove_node(root, curr_node);

}

return root;

}

int main(){

Node *n1 = new Node(1);

Node *n2 = new Node(2);

Node *n3 = new Node(3);

Node *n4 = new Node(4);

Node *n5 = new Node(5);

Node *n6 = new Node(6);

n1->left = n2;

n1->right = n3;

n2->left = n4;

n3->left = n5;

n4->right = n6;

Node *root = n1;

inorder(root);

std::cout<<endl;

root = delete_node(root, 3);

inorder(root);

std::cout<<endl;

return 0;

}

**Output:**

### Advantages of Binary Tree

Trees are so useful and frequently used because they have some very serious advantages:

- First, trees reflect structural relationships in the data.
- Second, trees are used to represent hierarchies.
- Third, trees provide an efficient insertion and searching.
- Finally, trees are very flexible data, allowing to move.
- Subtrees around with minimum effort.

### Conclusion

After this article, we now know about the binary tree, and the traversal theory in a binary tree means how we travel in the binary tree. How we delete a node in a binary tree, its syntax, code in C++ language, and give an example to easily understand the deletion of a node in a binary tree.

### Recommended Articles

This is a guide to Binary Tree Deletion. Here we discuss the introduction, how to perform binary tree deletion? algorithm and advantages, respectively. You may also have a look at the following articles to learn more –