Delete leaf nodes with value as x in C++?

C++Server Side ProgrammingProgramming

Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.

struct Node {
   int data;
   struct Node *leftChild, *rightChild;
};

Next we create our newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also the left and right child of the newly created node are set to null.

struct Node* newNode(int data){
   struct Node* newNode = new Node;
   newNode->data = data;
   newNode->leftChild = newNode->rightChild = NULL;
   return (newNode);
}

Now we create our deleteNode(Node* root, int x) function which takes the root node and the data value of the node to be deleted. If the given node is a parent node then it also deletes its left and right child. The function returns the modified root node after deletion of the given node.

Node* deleteLeafNode(Node* root, int x){
   if (root == NULL)
      return nullptr;
   root->leftChild = deleteLeafNode(root->leftChild, x);
   root->rightChild = deleteLeafNode(root->rightChild, x);
   if (root->data == x && root->leftChild == NULL && root->rightChild == NULL)
      return nullptr;
   return root;
}

Finally for dispaying the tree after deletion we have a function inorder(Node* root) which traverses the tree in inorder function.

void inorder(Node* root){
   if (root != NULL){
      inorder(root->leftChild);
      inorder(root->rightChild);
      cout << root->data << " ";
   }
}

Example

Let us see the following implementation of deleting leaf nodes that have value equal to x

 Live Demo

#include <iostream>
using namespace std;
struct Node {
   int data;
   struct Node *leftChild, *rightChild;
};
struct Node* newNode(int data){
   struct Node* newNode = new Node;
   newNode->data = data;
   newNode->leftChild = newNode->rightChild = NULL;
   return (newNode);
}
Node* deleteNode(Node* root, int x){
   if (root == NULL)
      return nullptr;
   root->leftChild = deleteNode(root->leftChild, x);
   root->rightChild = deleteNode(root->rightChild, x);
      if (root->data == x && root->leftChild == NULL &&
      root->rightChild == NULL)
         return nullptr;
   return root;
}
void inorder(Node* root){
   if (root != NULL){
      inorder(root->leftChild);
      inorder(root->rightChild);
      cout << root->data << " ";
   }
}
int main(void){
   struct Node* root = newNode(4);
   root->leftChild = newNode(2);
   root->rightChild = newNode(12);
   root->leftChild->leftChild = newNode(3);
   root->leftChild->rightChild = newNode(5);
   root->rightChild->rightChild = newNode(9);
   deleteNode(root, 3);
   cout << "Inorder traversal after deletion : ";
   inorder(root);
   return 0;
}

Output

The above code will produce the following output −

Inorder traversal after deletion : 5 2 9 12 4
raja
Published on 16-Jan-2021 07:03:34
Advertisements