-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
129 lines (127 loc) · 2.45 KB
/
binary_tree.cpp
File metadata and controls
129 lines (127 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<bits/stdc++.h>
#include<queue>
using namespace std;
class node {
public:
int data;
public:
node *left;
node *right;
node(int x = 0) {
this->data = x;
}
void insert();
void inorder(node*);
node* deletion(node* , int) ;
void delet_deepest(node* , node*);
};
void node :: insert()
{
queue<node *> q;
q.push(this);
while(!q.empty()) {
node *temp = q.front();
q.pop();
cout<<"enter the left data of "<<temp->data<<"or -1 for stop"<<endl;
int data1;
cin>>data1;
cout<<"enter the right data of "<<temp->data<<"or -1 for stop"<<endl;
int data2;
cin>>data2;
if(data1 != -1) {
temp->left = new node(data1);
q.push(temp->left);
}
if(data2 != -1) {
temp->right = new node(data2);
q.push(temp->right);
}
}
return ;
}
void node :: inorder(node *temp)
{
if (!temp)
return;
inorder(temp->left);
cout << temp->data << " ";
inorder(temp->right);
}
void node :: delet_deepest(node *root , node *d_node)
{
queue<node*> q;
q.push(root);
while(!q.empty()) {
node* temp = q.front();
q.pop();
if(temp == d_node) {
temp = NULL;
delete d_node;
return ;
}
if(temp->left) {
if(temp->left == d_node) {
temp->left = NULL;
delete d_node;
return ;
}
else
q.push(temp->left);
}
if(temp->right) {
if(temp->right == d_node) {
temp->right = NULL;
delete d_node;
return ;
}
else
q.push(temp->right);
}
}
}
node* node :: deletion(node* root , int key) {
if(root == NULL)
return NULL;
if(root->left == NULL && root->right == NULL) {
if(root->data == key)
return NULL;
else
return root;
}
node *temp = NULL;
queue<node*> q;
node* key_node = NULL;
q.push(root);
while(!q.empty()) {
temp = q.front();
q.pop();
if(temp->data == key)
key_node = temp;
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
if(key_node) {
int x = temp->data;
delet_deepest(root , temp);
key_node->data = x;
}
return root;
}
int main()
{
int choice,data;
node *root1;
cout<<"enter the data"<<endl;
cin>>data;
node *root = new node(data);
//insertion of data
root->insert();
root->inorder(root);
// deletion of data;
cout<<"enter the data to be deleted"<<endl;
cin>>data;
root->deletion(root,data);
root->inorder(root);
}