-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.h
More file actions
255 lines (229 loc) · 5.35 KB
/
BinaryTree.h
File metadata and controls
255 lines (229 loc) · 5.35 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
#pragma once
#include <iostream>
#include<algorithm>
#include <string>
#include <sstream>
#include<vector>
#include<math.h>
#include <iomanip>
#include <map>
#include<set>
#include <fstream>
#include <numeric>
#include <queue>
using namespace std;
//ÄÁÏ
struct tree {
int inf;
tree* right;
tree* left;
tree* parent;
};
tree* node(int x) {//íà÷àëüíûé óçåë
tree* n = new tree;
n->inf = x;
n->left = n->right = NULL;
n->parent = NULL;
return n;
}
void insert(tree*& tr, int x) {//âñòàâêà
tree* n = node(x);
if (!tr) tr = n; //åñëè äåðåâî ïóñòîå - êîðåíü
else {
tree* y = tr;
while (y) { //èùåì êóäà âñòàâëÿòü
if (n->inf >= y->inf) //ïðàâàÿ âåòêà
if (y->right)
y = y->right;
else {
n->parent = y; //óçåë ñòàíîâèòñÿ ïðàâûì ðåáåíêîì
y->right = n;
break;
}
else if (n->inf < y->inf)//ëåâàÿ âåòêà
if (y->left)
y = y->left;
else {
n->parent = y;//óçåë ñòàíîâèòñÿ ëåâûì ðåáåíêîì
y->left = n;
break;
}
}
}
}
void add(tree*& tr, int value, int amount) {
for (int i = 0; i < amount; i++)
{
insert(tr, value);
}
}
void inorder(tree* tr) {//ñèììåòðè÷íûé îáõîä
if (tr) {
inorder(tr->left);
cout << tr->inf << " ";
inorder(tr->right);
}
}
void preorder(tree* tr) {//ñèììåòðè÷íûé îáõîä
if (tr) {
cout << tr->inf << " ";
inorder(tr->left);
inorder(tr->right);
}
}
void postorder(tree* tr) {//ñèììåòðè÷íûé îáõîä
if (tr) {
inorder(tr->left);
inorder(tr->right);
cout << tr->inf << " ";
}
}
tree* find(tree* tr, int x) {//ïîèñê
if (!tr || x == tr->inf)//íàøëè èëè äîøëè äî êîíöà âåòêè
return tr;
if (x < tr->inf)
return find(tr->left, x);//èùåì ïî ëåâîé âåòêå
else
return find(tr->right, x);//èùåì ïî ïðàâîé âåòêå
}
tree* Min(tree* tr) {//ïîèñê min
if (!tr->left) return tr;//íåò ëåâîãî ðåáåíêà
else return Min(tr->left);//èäåì ïî ëåâîé âåòêå äî êîíöà
}
tree* Max(tree* tr) {//ïîèñê max
if (!tr->right) return tr;//íåò ïðàâîãî ðåáåíêà
else return Max(tr->right);//èäåì ïî ïðàâîé âåòêå äî êîíöà
}
tree* Next(tree* tr, int x) {//ïîèñê ñëåäóþùåãî
tree* n = find(tr, x);
if (n->right)//åñëè åñòü ïðàâûé ðåáåíîê
return Min(n->right);//min ïî ïðàâîé âåòêå
tree* y = n->parent; //ðîäèòåëü
while (y && n == y->right) {//ïîêà íå äîøëè äî êîðíÿ èëè óçåë - ïðàâûé ðåáåíîê
n = y;//èäåì ââåðõ ïî äåðåâó
y = y->parent;
}
return y;//âîçâðàùàåì ðîäèòåëÿ
}
tree* Prev(tree* tr, int x) {//ïîèñê ïðåäûäóùåãî
tree* n = find(tr, x);
if (n->left)//åñëè åñòü ëåâûé ðåáåíîê
return Max(n->left);//max ïî ëåâîé âåòêå
tree* y = n->parent;//ðîäèòåëü
while (y && n == y->left) {//ïîêà íå äîøëè äî êîðíÿ èëè óçåë - ëåâûé ðåáåíîê
n = y;//èäåì ââåðõ ïî äåðåâó
y = y->parent;
}
return y;//âîçâðàùàåì ðîäèòåëÿ
}
//void Delete(tree*& tr, tree* v) {//óäàëåíèå óçëà
// tree* p = v->parent;
// if (!p) tr = NULL; //äåðåâî ñîäåðæèò îäèí óçåë
// else if (!v->left && !v->right) {//åñëè íåò äåòåé
// if (p->left == v) //óêàçàòåëü ó ðîäèòåëÿ ìåíÿåì íà NULL
// p->left = NULL;
// if (p->right == v)
// p->right = NULL;
// delete v;
//
// }
// else if (!v->left || !v->right) {//åñëè òîëüêî îäèí ðåáåíîê
// if (!p) { //åñëè óäàëÿåì êîðåíü, ó êîòîðîãî 1 ðåáåíîê
// if (!v->left) { //åñëè åñòü ïðàâûé ðåáåíîê
// tr = v->right; //îí ñòàíîâèòñÿ êîðíåì
// v->parent = NULL;
//
// }
// else { //àíàëîãè÷íî äëÿ ëåâîãî
//
// tr = v->left;
// v->parent = NULL;
//
// }
//
// }
// else {
// if (!v->left) {//åñëè åñòü ïðàâûé ðåáåíîê
// if (p->left == v) //åñëè óäàëÿåìûé óçåë ÿâë. ëåâûì ðåáåíêîì
// p->left = v->right; //ðåáåíîê óäàëÿåìîãî óçëà ñòàíîâèòñÿ ëåâûì ðåáåíêîì ñâîåãî "äåäà"
// else
// p->right = v->right; ////ðåáåíîê óäàëÿåìîãî óçëà ñòàíîâèòñÿ ïðàâûì ðåáåíêîì ñâîåãî "äåäà"
// v->right->parent = p; //ðîäèòåëåì ðåáåíêà ñòàíîâèòñÿ åãî "äåä"
//
// }
// else {//àíàëîãè÷íî äëÿ ëåâîãî ðåáåíêà
// if (p->left == v)
// p->left = v->left;
// else
// p->right = v->left;
// v->left->parent = p;
//
// }
// delete v;
//
// }
//
// }
// else {//åñòü îáà ðåáåíêà
// tree* succ = Next(tr, v->inf);//ñëåäóþùèé çà óäàëÿåìûì óçëîì
// v->inf = succ->inf; //ïðèñâàèâàåì çíà÷åíèå
// if (succ->parent->left == succ) {//åñëè succ ëåâûé ðåáåíîê
// succ->parent->left = succ->right; //åãî ïðàâûé ðåáåíîê ñòàíîâèòñÿ ëåâûì ðåáåíêîì ñâîåãî "äåäà"
// if (succ->right) //åñëè ýòîò ðåáåíîê ñóùåñòâóåò
// succ->right->parent = succ->parent; //åãî ðîäèòåëåì ñòàíîâèòñÿ "äåä"
//
// }
// else {//àíàëîãè÷íî åñëè succ - ïðàâsq ðåáåíîê
// succ->parent->right = succ->right;
// if (succ->right)
// succ->right->parent = succ->parent;
//
// }
// delete succ;
// }
//}
int min(tree*& tr)
{
if (node == NULL)
{
return INT_MIN;
}
if (tr->left != NULL)
{
return min(tr->left);
}
return tr->inf;
}
void remove(tree*& tr, int item)
{
if (tr == NULL)
{
return;
}
else if (item < tr->inf)
{
remove(tr->left, item);
}
else if (item > tr->inf)
{
remove(tr->right, item);
}
else // item == node->data
{
if (tr->right == NULL)
{
tr = tr->left;
}
else if (tr->left == NULL)
{
tr = tr->right;
}
else
{
int min_val = min(tr->right);
remove(tr->right, min_val);
tr->inf = min_val;
return;
}
}
}