

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deleting a binary tree using the delete keyword in C++?
<p>Let us first define our binary tree using a class containing int data, btree_node * rightChild, btree_node * leftChild. The leftChild and rightChild are pointers to btree_node. All the members in our class are public.</p><pre class="prettyprint notranslate">class btree_node { public: int data; btree_node* leftChild; btree_node* rightChild;</pre><p>For creating a new node we have the constructor function that takes the int value as parameter to assign it to the newly created node value. The leftChild and rightChild are set to null.</p><pre class="prettyprint notranslate">btree_node(int data){ this->data = data; this->leftChild = NULL; this-> = NULL; }</pre><p>The class deconstructor deletes the left and right child of the binary tree using the delete keyword when being invoked.</p><pre class="prettyprint notranslate">~btree_node(){ delete leftChild; delete rightChild; cout << this->data << "is being deleted"<<endl; }</pre><p>To trigger the deletion of the tree we call the delete on the root node as its left and right subtree will be deleted along.</p><pre class="prettyprint notranslate">delete root;</pre><h2>Example</h2><p>Let us see the following implementation for deleting binary tree using the delete keyword −</p><p><a class="demo" href="http://tpcg.io/sxPtQ3AJ" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">#include <iostream> using namespace std; class btree_node { public: int data; btree_node* leftChild; btree_node* rightChild; btree_node(int data){ this->data = data; this->leftChild = NULL; this->rightChild = NULL; } ~btree_node(){ delete leftChild; delete rightChild; cout << this->data << " is being deleted"<<endl; } }; int main(){ btree_node* root = new btree_node(2); btree_node* node1 = new btree_node(4); btree_node* node2 = new btree_node(6); btree_node* node3 = new btree_node(8); btree_node* node4 = new btree_node(10); root->leftChild = node1; root->rightChild = node2; node1->leftChild = node3; node1->rightChild = node4; delete root; return 0; }</pre><h2>Output</h2><p>The above code will produce the following output −</p><pre class="result notranslate">8 is being deleted 10 is being deleted 4 is being deleted 6 is being deleted 2 is being deleted</pre>
- Related Questions & Answers
- Deleting a binary tree using the delete keyword in C++ program
- Deleting desired node from a Binary Search Tree in JavaScrip
- Binary Search Tree - Delete Operation in C++
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- Program to delete all leaves with even values from a binary tree in Python
- Program to change the root of a binary tree using Python
- Binary Tree to Binary Search Tree Conversion in C++
- Python Program to Sort using a Binary Search Tree
- Program to fix a erroneous binary tree using Python
- Delete Tree Nodes in C++
- Check if a binary tree is subtree of another binary tree in C++
- Inverting a binary tree in JavaScript
- Create a mirror tree from the given binary tree in C++ Program
Advertisements