Skip to content

Commit a80eb74

Browse files
committed
Added deletion of node functionality to binary tree in c++
1 parent 3ff1f5c commit a80eb74

File tree

1 file changed

+132
-4
lines changed

1 file changed

+132
-4
lines changed

classical_algorithms/c++/binary_tree.cpp

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include<bits/stdc++.h>
23
using namespace std;
34

45
struct Node {
@@ -18,19 +19,123 @@ struct Node {
1819
right = NULL;
1920
}
2021
};
22+
/*Function which print level order of the given tree*/
23+
void level_order(Node* root)
24+
{
25+
if(root==NULL)
26+
{
27+
return;
28+
}
29+
queue<Node*> q;
30+
q.push(root);
31+
while(!q.empty())
32+
{
33+
Node* temp=q.front();
34+
cout<<temp->data<<" ";
35+
q.pop();
36+
if(temp->left!=NULL)
37+
{
38+
q.push(temp->left);
39+
}
40+
if(temp->right!=NULL)
41+
{
42+
q.push(temp->right);
43+
}
44+
}
45+
cout<<endl;
46+
}
47+
pair<Node*,int> last_node(Node* root)
48+
{
49+
if(root==NULL)
50+
{
51+
return make_pair(root,0);
52+
}
53+
queue<Node*> q;
54+
q.push(root);
55+
Node* last_parent=NULL;
56+
int last_value;
57+
while(!q.empty())
58+
{
59+
Node* temp=q.front();
60+
/*store the parent of last node*/
61+
if((temp->left!=NULL)&&(temp->right!=NULL))//if left and right sub tree are present;
62+
{
63+
if(temp->left->left==NULL&&temp->left->right==NULL&&temp->right->left==NULL&&temp->right->right==NULL)
64+
{
65+
last_parent=temp;
66+
}
67+
}
68+
if(temp->left!=NULL)//if left subtree only present;
69+
{
70+
if(temp->left->left==NULL&&temp->left->right==NULL)
71+
{
72+
last_parent=temp;
73+
}
74+
}
75+
if(temp->right!=NULL)//if right subtree only present;
76+
{
77+
if(temp->right->left==NULL&&temp->right->right==NULL)
78+
{
79+
last_parent=temp;
80+
}
81+
}
82+
83+
/*for storing the value of last node.*/
84+
last_value=temp->data;
85+
q.pop();
86+
if(temp->left!=NULL)
87+
{
88+
q.push(temp->left);
89+
}
90+
if(temp->right!=NULL)
91+
{
92+
q.push(temp->right);
93+
}
94+
}
95+
/*return the parent of last node and the data of the last node*/
96+
return make_pair(last_parent,last_value);
97+
}
98+
void delete_node(Node* root,int value,int last_value)
99+
{
100+
/*if only one node is present and we want to delete it*/
101+
if(root->left==NULL&&root->right==NULL)
102+
{
103+
root=NULL;
104+
return;
105+
}
106+
queue<Node*> q;
107+
q.push(root);
108+
while(!q.empty())
109+
{
110+
Node* temp=q.front();
111+
q.pop();
112+
/*if we find the node which we want to delete then change the data with last node data*/
113+
if(temp->data==value)
114+
{
115+
temp->data=last_value;
116+
}
117+
if(temp->left!=NULL)
118+
{
119+
q.push(temp->left);
120+
}
121+
if(temp->right!=NULL)
122+
{
123+
q.push(temp->right);
124+
}
125+
}
126+
}
21127

22128
int main()
23129
{
24-
130+
25131
/*create root*/
26132
struct Node* root = new Node(1);
27133
/* following is the tree after above statement
28-
29134
1
30135
/ \
31136
NULL NULL
32137
*/
33-
138+
34139
root->left = new Node(2);
35140
root->right = new Node(3);
36141
/* 2 and 3 become left and right children of 1
@@ -51,6 +156,29 @@ int main()
51156
/ \
52157
NULL NULL
53158
*/
54-
159+
cout<<"Level order traversal before Deletion of node: ";
160+
level_order(root);
161+
162+
int value;
163+
cout<<"Enter the value of node to be deleted"<<endl;
164+
cin>>value;//input the data of node which we want to delete;
165+
166+
/*Find the last node and its parent to remove last node from tree.*/
167+
pair<Node*,int> last=last_node(root);
168+
/*remove last node*/
169+
if(last.first->left!=NULL&&last.first->left->data==last.second)
170+
{
171+
last.first->left=NULL;
172+
}
173+
if(last.first->right!=NULL&&last.first->right->data==last.second)
174+
{
175+
last.first->right=NULL;
176+
}
177+
/*delete the node*/
178+
delete_node(root, value,last.second);
179+
cout<<"Level order traversal after Deletion of node: ";
180+
level_order(root);
181+
55182
return 0;
56183
}
184+

0 commit comments

Comments
 (0)