-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-intro.cpp
More file actions
284 lines (264 loc) · 7.02 KB
/
binary-tree-intro.cpp
File metadata and controls
284 lines (264 loc) · 7.02 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "bits/stdc++.h"
#include <chrono>
using namespace std;
using namespace std::chrono;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define gcd(a,b) __gcd(a,b)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vc vector<char>
#define pb(n) push_back(n)
#define pp pop_back()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define type(a) typeid(a).name();
#define print(arr) cout << "[" ;for(unsigned int i=0;i<(unsigned int) arr.size();i++){ (i==arr.size()-1)?cout << arr[i]:cout << arr[i] << ",";}cout << "]" << endl
#define Arr(arr,l) cout << "[" ;for(unsigned int i=0;i<(unsigned int) l;i++){ (i==l-1)?cout << arr[i]:cout << arr[i] << ",";}cout << "]" << endl
#define console(arr) cout << "["<<endl; for(ll i=0;i<(ll)arr.size();++i){cout << " [";for(ll j=0;j<(ll)arr[i].size();++j){(j==(int)arr[i].size()-1)?cout << arr[i][j]:cout << arr[i][j] << ",";}cout << "]"<<endl;} cout << "]"<<endl
#define starting() auto start = high_resolution_clock::now()
#define ending() auto stop = high_resolution_clock::now();auto duration = duration_cast<microseconds>(stop - start);cout << endl << endl << "Execution Time : "<<duration.count()/1000 << " ms" << endl
template <typename T1,typename T2>
void mapped(map<T1,T2>m){for(auto ite=m.begin();ite!=m.end();++ite){cout << "'" << ite->first << "'"<<" : "<<ite->second <<endl;}}
template <typename T1 , typename T2>
void mappedArr(map<T1,vector<T2>>m){for(auto ity=m.begin();ity!=m.end();++ity){cout << "'"<< ity->first <<"'"<<" : "<<"[";
for(int i=0;i<(int)ity->second.size();++i){(i==(int)ity->second.size()-1)?cout << ity->second[i]:cout << ity->second[i] << ",";}cout << "]"<<endl;}}
string int_to_string(int x){ stringstream ss; ss << x; string ni = ss.str(); return ni;}
int char_to_int(char c){ int n = (int)c - 48; return n; }
int string_to_int(string x){ int n; stringstream s(x); s >> n; return n; }
template <typename T1,typename T2>
void ArrPair(vector<pair<T1,T2>>vp){for(int i=0;i<(int)vp.size();++i){
cout << "(" << vp[i].first << "," << vp[i].second << ")"<<endl ;}}
const int MOD = 1e9 + 7;
const ll INF = 1e18L + 5;
unordered_map<int,int> um;
unordered_set<int> us;
set<int> s;
// v.front() gives the first element of vector
// v.back() gives the last element of vector
// insert erase substr
//g++ -o Legendary-Grandmaster Legendary-Grandmaster.cpp&Legendary-Grandmaster.exe
//cd C:\Users\158si\Desktop\Algorithim\codeForces\elite
// for-bash cd C:/Users/158si/Desktop/Algorithim/codeForces/elite
class bst{
private:
struct node{
int key;
node* left;
node* right;
node* parent;
};
node* root;
public:
bst(){
root = NULL;
}
node* construct_node(int key,node* ptr){
node* n = new node;
n->key = key;
n->right = NULL;
n->left = NULL;
n->parent = ptr;
return n;
}
void addleafchecking(int key){
addleaf(key,root);
}
void addleaf(int key,node* ptr){
//ptr = root;
if(root==NULL){
root = construct_node(key,ptr);
}else{
if(key<ptr->key){
if(ptr->left==NULL){
ptr->left = construct_node(key,ptr);
}else{
addleaf(key,ptr->left);
}
}else if(key>ptr->key){
if(ptr->right==NULL){
ptr->right = construct_node(key,ptr);
}else{
addleaf(key,ptr->right);
}
}else{
cout << "The number is laready present in the tree";
}
}
}
void printAcc(){
if(root!=NULL){
printA(root);
}else{
cout << "Tree is empty\n";
}
}
void printA(node* ptr){
if(ptr->left!=NULL){
printA(ptr->left);
}
cout << ptr->key << " ";
if(ptr->right!=NULL){
printA(ptr->right);
}
}
void printChilds(int key){
printChild(key,root);
}
int value(node* ptr){
return ptr->key;
}
node* checking(int key){
return traversingtree(key,root);
}
node* traversingtree(int key,node* ptr){
if(ptr->key==key){
return ptr;
}else{
if(key<ptr->key){
if(ptr->left!=NULL){
return traversingtree(key,ptr->left);
}else{
return NULL;
}
}else if(key>ptr->key){
if(ptr->right!=NULL){
return traversingtree(key,ptr->right);
}else{
return NULL;
}
}
}
return NULL;
}
void finding(int key){
node* n = checking(key);
if(n!=NULL){
cout << "Present\n";
}else{
cout << "Absent\n";
}
}
void deleteNode(int key){
del(key);
}
node* lastRight(node* ptr){
return checkLeft(ptr->right);
}
node* checkLeft(node* ptr){
if(ptr->left==NULL){
return ptr;
}else{
return checkLeft(ptr->left);
}
}
void printChild(int key ,node* ptr){
cout << key << "\n";
ptr = checking(key);
if(ptr!=NULL){
//cout << ptr;
if(ptr->left!=NULL){
cout << "Left is " << value(ptr->left);
}else{
cout << "Left is null";
}
cout << "\n";
if(ptr->right!=NULL){
cout << "Right is " << value(ptr->right);
}else{
cout << "Right is null";
}
cout << "\n";
}else{
cout << "It is not present\n";
}
}
void printParent(int key){
parentNode(key,root);
}
void parentNode(int n,node* ptr){
if(root->key==n){
cout << "NULL";
}else{
node* w = checking(n);
node* k = w->parent;
cout << k->key << endl;
}
}
node* getParent(int key){
return parentcheck(key,root);
}
node* parentcheck(int n,node* ptr){
if(root->key==n){
return NULL;
}else{
node* w = checking(n);
node* k = w->parent;
return k;
}
}
void del(int key){
node* ptr;
ptr = checking(key);
node* n = getParent(key);
if(n==NULL){
root= NULL;
}else if(ptr==NULL){
cout << "It is not present";
}else{
if(ptr->left==NULL && ptr->right==NULL){
ptr->parent = NULL;
ptr = NULL;
}else if(ptr->left==NULL){
if(key==value(n->left)){
n->left = ptr->right;
}else if(key==value(n->right)){
n->right = ptr->right;
}
ptr->right->parent = n;
}else if(ptr->right==NULL){
if(key==value(n->left)){
n->left = ptr->left;
}else if(key==value(n->right)){
n->right = ptr->left;
}
ptr->left->parent = n;
}else{
node* w = lastRight(ptr);
ptr->key = value(w);
if(w->left==NULL && w->right==NULL){
ptr = NULL;
ptr->parent = NULL;
}else{
node* p = getParent(w->key);
p->left = w->right;
p->left->parent = p;
p->parent = NULL;
}
}
}
}
};
int main(){
IOS;
int words[10] = {1,5,46,32,54,48,62,52,56,72};
bst tree;
//tree.printAcc();
for(int i=0;i<10;++i){
tree.addleafchecking(words[i]);
}
//tree.printAcc();
tree.deleteNode(5);
// tree.printAcc();
//for(int i=0;i<8;++i){
// tree.printChilds(words[i]);
// }
tree.printParent(46);
// tree.printChilds(48);
// tree.printChilds(5);
//for(int i=0;i<8;++i){
// tree.printParent(words[i]);
//}
return 0;
}