1
1
#include < iostream>
2
+ #include < bits/stdc++.h>
2
3
using namespace std ;
3
4
4
5
struct Node {
@@ -18,19 +19,123 @@ struct Node {
18
19
right = NULL ;
19
20
}
20
21
};
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
+ }
21
127
22
128
int main ()
23
129
{
24
-
130
+
25
131
/* create root*/
26
132
struct Node * root = new Node (1 );
27
133
/* following is the tree after above statement
28
-
29
134
1
30
135
/ \
31
136
NULL NULL
32
137
*/
33
-
138
+
34
139
root->left = new Node (2 );
35
140
root->right = new Node (3 );
36
141
/* 2 and 3 become left and right children of 1
@@ -51,6 +156,29 @@ int main()
51
156
/ \
52
157
NULL NULL
53
158
*/
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
+
55
182
return 0 ;
56
183
}
184
+
0 commit comments