-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst_lib.cpp
More file actions
243 lines (216 loc) · 4.57 KB
/
bst_lib.cpp
File metadata and controls
243 lines (216 loc) · 4.57 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
#include <iostream>
#include "bst_lib.h"
using namespace std;
Node::Node(int val)
:data{ val }, lchild{ nullptr }, rchild{ nullptr }, weight{ 1 } {};
Node::Node() {};
Node* Node::getLeft() {
return lchild;
}
Node* Node::getRight(){
return lchild;
}
void Node::increaseWeight() {
weight++;
}
void Node::preOrder() {
if (this == nullptr)
return;
cout << this->data << " ";
this->lchild->preOrder();
this->rchild->preOrder();
}
void Node::inOrder() {
if (this == nullptr)
return;
this->lchild->inOrder();
cout << this->data << " ";
this->rchild->inOrder();
}
void Node::postOrder() {
if (this == nullptr)
return;
this->lchild->postOrder();
this->rchild->postOrder();
cout << this->data << " ";
}
int Node::height() {
if (this == nullptr)
return -1;
int left = this->lchild->height();
int right = this->rchild->height();
return max(left, right) + 1;
}
bool Node::isBST() {
if (this == nullptr)
return true;
if (this->lchild != nullptr && this->lchild->data > this->data) {
cout << "Questo Binary tree non corrisponde a un tipo BST " << endl;
return false;
}
if (this->rchild != nullptr && this->rchild->data < this->data) {
cout << "Questo Binary tree non corrisponde a un tipo BST " << endl;
return false;
}
return this->lchild->isBST() && this->rchild->isBST();
}
Node* Node::insertR(int k) {
if (this == nullptr) {
return new Node(k);
}
if (k == this->data) {
increaseWeight();
return this;
}
if (k < this->data) {
this->lchild = this->lchild->insertR(k);
}
else {
this->rchild = this->rchild->insertR(k);
}
return this;
}
bool check{ true };
Node* Node::searchR(int k) {
if (this == nullptr) {
check = false;
return nullptr;
}
else {
if (k == this->data) {
check = true;
return this;
}
else {
if (k < this->data) {
this->lchild->searchR(k);
if (check == true)
return this;
else
return nullptr;
}
if (k > this->data) {
this->rchild->searchR(k);
if (check == true)
return this;
else
return nullptr;
}
return this;
}
}
}
Node* Node::deleteNode(int k) {
Node* temp{ nullptr };
if (this == nullptr) {
return this;
}
if (k < this->data) {
this->lchild = this->lchild->deleteNode(k);
}
else if (k > this->data) {
this->rchild = this->rchild->deleteNode(k);
}
else {
if (this->lchild == nullptr && this->rchild == nullptr) {
delete this;
return nullptr;
}
if (this->lchild == nullptr) {
temp = this->rchild;
delete this;
return temp;
}
else if (this->rchild == nullptr) {
temp = this->lchild;
delete this;
return temp;
}
temp = this->rchild;
while (temp->lchild != nullptr) {
temp = temp->lchild;
}
this->data = temp->data;
this->rchild = this->rchild->deleteNode(temp->data);
}
return this;
}
Node* Node::insertI(int k) {
if (this == nullptr) {
return new Node(k);
}
Node* current = this;
Node* father = nullptr;
while (current != nullptr) {
if (k < current->data) {
father = current;
current = current->lchild;
}
else {
if (k == current->data) {
current->weight++;
return this;
}
father = current;
current = current->rchild;
}
}
if (k < father->data) {
father->lchild = new Node(k);
}
if (k > father->data) {
father->rchild = new Node(k);
}
return this;
}
Node* Node::searchI(int k) {
Node* current{ this };
if (this == nullptr) {
return nullptr;
}
if (k < this->data) {
while (current != nullptr) {
if (k == current->data) {
return current;
}
current = current->lchild;
}
}
else if (k > this->data) {
while (current != nullptr) {
if (k == current->data) {
return current;
}
current = current->rchild;
}
}
return nullptr;
}
ostream& operator<<(ostream& os, Node* &r) {
if (r == nullptr) {
os << "Nodo Nullo" << endl;
return os;
}
os << "Key - Nodo Corrente: " << r->data << endl;
os << "Peso - Nodo Corrente: " << r->weight << endl;
if (r->lchild == nullptr) {
os << "Nodo Sinistro Nullo" << endl;
}
else {
os << "Key - Nodo Sinistro: " << r->lchild->data << endl;
}
if (r->rchild == nullptr) {
os << "Nodo Destro Nullo" << endl;
}
else {
os << "Key - Nodo Destro: " << r->rchild->data << endl;
}
return os;
}
istream& operator>>(istream& is, Node &r) {
cout << "Immetti il valore che deve avere la key del Nodo: ";
is >> r.data;
r.lchild = nullptr;
r.rchild = nullptr;
return is;
}