-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree2.cpp
233 lines (222 loc) · 4.8 KB
/
btree2.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <queue>
#include <stack>
// INSERTION(all cases)
// Deletion(all cases)
// Mirror
// BST
// NO of leaf nodes and display
// NO of total nodes and display
// Height
using namespace std;
struct node
{
int data;
struct node *left, *right;
};
struct node *create(int data)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->right = nullptr;
temp->left = nullptr;
temp->data = data;
return temp;
}
struct node *insertNode(struct node *T, int data)
{
if (T == nullptr)
{
return create(data);
}
struct node *child = T, *parent = nullptr, *temp = create(data);
while (child != NULL)
{
if (data < child->data)
{
parent = child;
child = child->left;
}
else
{
parent = child;
child = child->right;
}
}
if (data < parent->data)
{
parent->left = temp;
}
else
{
parent->right = temp;
}
return T;
};
struct node *findsuc(struct node *T)
{
T = T->right;
while (T && T->left != nullptr)
{
T = T->left;
}
return T;
}
struct node *deleteBST(struct node *T, int data)
{
if (T == nullptr)
{
return T;
}
if (data > T->data)
{
T->right = deleteBST(T->right, data);
}
else if (data < T->data)
{
T->left = deleteBST(T->left, data);
}
else
{
if (T->left == nullptr)
{
struct node *temp = T->right;
free(T);
return temp;
}
else if (T->right == nullptr)
{
struct node *temp = T->left;
free(T);
return temp;
}
struct node *temp = findsuc(T);
T->data = temp->data;
T->right = deleteBST(T->right, temp->data);
}
return T;
}
void BFS(struct node *T)
{
if (T == nullptr)
{
return;
}
queue<node *> q;
q.push(T);
while (!q.empty())
{
node *current = nullptr;
int levelsize = q.size();
for (int i = 0; i < levelsize; i++)
{
current = q.front();
q.pop();
cout << current->data << " ";
if (current->left != nullptr)
{
q.push(current->left);
}
if (current->right != nullptr)
{
q.push(current->right);
}
}
}
}
int height(struct node *T)
{
int height = 0;
queue<node *> q;
q.push(T);
while (!q.empty())
{
node *current = nullptr;
int levelsize = q.size();
for (int i = 0; i < levelsize; i++)
{
current = q.front();
q.pop();
if (current->left != nullptr)
{
q.push(current->left);
}
if (current->right != nullptr)
{
q.push(current->right);
}
}
height++;
}
return height;
}
struct node *copytree(struct node *T)
{
if (T == nullptr)
return T;
struct node *newnode = nullptr;
queue<node *> q;
q.push(T);
while (!q.empty())
{
node *origin = q.front();
q.pop();
newnode = insertNode(newnode, origin->data);
if (origin->left != nullptr)
{
q.push(origin->left);
}
if (origin->right != nullptr)
{
q.push(origin->right);
}
}
return newnode;
}
// NOt distrub the original tree
void mirror(struct node *T)
{
if (T == nullptr)
return;
stack<node *> s;
s.push(T);
struct node *temp = nullptr;
while (!s.empty())
{
temp = s.top();
s.pop();
swap(temp->left, temp->right);
if (temp->left != nullptr)
{
s.push(temp->left);
}
if (temp->right != nullptr)
{
s.push(temp->right);
}
}
}
int main()
{
struct node *root = nullptr;
root = insertNode(root, 3);
root = insertNode(root, 1);
root = insertNode(root, 5);
root = insertNode(root, 2);
root = insertNode(root, 4);
root = insertNode(root, 6);
cout << "Tree before deletion:" << endl;
BFS(root);
cout << endl;
root = deleteBST(root, 5);
root = deleteBST(root, 1);
cout << "Tree after deletion:" << endl;
BFS(root);
cout << endl
<< "Height " << height(root) << endl;
struct node *mirror_tree = copytree(root);
mirror(mirror_tree);
cout << endl
<< "Mirror is " << endl;
BFS(root);
return 0;
}