
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete leaf nodes with value k in C++?
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 k) 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 k) { if (root == NULL) return nullptr; root->leftChild = deleteLeafNode(root->leftChild, k); root->rightChild = deleteLeafNode(root->rightChild, k); if (root->data == k && 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 k.
#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* deleteLeafNode(Node* root, int k){ if (root == NULL) return nullptr; root->leftChild = deleteLeafNode(root->leftChild, k); root->rightChild = deleteLeafNode(root->rightChild, k); if (root->data == k && 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(6); root->leftChild = newNode(7); root->rightChild = newNode(7); root->leftChild->leftChild = newNode(5); root->leftChild->rightChild = newNode(3); root->rightChild->rightChild = newNode(7); deleteLeafNode(root, 7); cout << "Inorder traversal after deleting given leaf node: "; inorder(root); return 0; }
Output
The above code will produce the following output −
Inorder traversal after deleting given leaf node: 5 3 7 6
- Related Articles
- Delete leaf nodes with value k in C++ program
- Delete leaf nodes with value as x in C++?
- Delete leaf nodes with value as x in C++ Program
- C++ Remove Nodes on Root to Leaf Paths of Length < K
- Print all nodes that are at distance k from a leaf node in C++
- Delete Tree Nodes in C++
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Count Non-Leaf nodes in a Binary Tree in C++
- Print Leaf Nodes at a given Level in C language
- Product of all leaf nodes of binary tree in C++
- Delete N nodes after M nodes of a linked list in C++?
- Reverse Nodes in k-Group in C++
- Delete Leaves With a Given Value in C++
- Delete N nodes after M nodes of a linked list in C++ program
- Delete alternate nodes of a Linked List in C++
