-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.cpp
203 lines (172 loc) · 3.55 KB
/
BST.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
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include<utility>
using namespace std;
template <typename T>
class BST;
long long int composition(int k, int n) {
//can be done in one loop but the precision might not be accurate
long long int up = 1;
long long int down = 1;
for (int i = n - k + 1; i <= n; i++)
up *= i;
for (int i = 1; i <= k; i++) {
down *= i;
}
return up / down;
}
template<typename T>
class Node {
friend class BST<T>;
public :
Node() = default;
Node(T data,Node<T>* parent=nullptr,Node* left=nullptr,Node* right=nullptr) {
this->parent = parent;
this->data = data;
this->right = right;
this->left = left;
}
T getData() { return this->data; }
private:
Node* right;
Node* left;
Node* parent;
T data;
};
template<typename T>
class BST {
public:
BST(T root_data) {
root = new Node<T>(root_data);
}
BST() {
root = nullptr;
}
BST(const vector<T>& sequence) {
for (const auto& i : sequence) {
this->push(i);
}
}
void push(T data) {
if (!root) {
root = new Node<T>(data);
return;
}
Node<T>* temp = root;
while (true) {
if (data >= temp->data) {
if (temp->right ) {
temp = temp->right;
}
else {
temp->right = new Node<T>(data,temp);
break;
}
}
else {
if (temp->left) {
temp = temp->left;
}
else {
temp->left = new Node<T>(data, temp);
break;
}
}
}
}
void BFSPrint() {
int nodes_in_same_height=1;
int nodes_in_next_same_height=0;
queue<Node<T>*> nodes;
Node<T>* prev_parent = nullptr;
if (root != nullptr)
nodes.push(root);
while (!nodes.empty()) {
Node<T>* node = nodes.front();
nodes.pop();
if (node->left) {
nodes.push(node->left);
nodes_in_next_same_height++;
}
if (node->right ) {
nodes.push(node->right);
nodes_in_next_same_height++;
}
if (prev_parent != node->parent) {
cout <<" " <<node->parent->data << ": ";
prev_parent = node->parent;
}
cout << node->data << ' ';
nodes_in_same_height--;
if (!nodes_in_same_height) {
nodes_in_same_height = nodes_in_next_same_height;
nodes_in_next_same_height = 0;
prev_parent = node->parent;
cout << endl;
}
}
}
Node<T>* getByString(string rl) {
if (rl.empty())
return root;
if (!root)
throw "out of range";
int index = 0;
Node<T>* temp = root;
while (index != rl.size()) {
if (rl[index] == 'r') {
if (!(temp-> right))
throw "out of range";
temp = temp->right;
}
if (rl[index] == 'l') {
if (!(temp-> left))
throw "out of range";
temp = temp->left;
}
index++;
}
return temp;
}
void Delete(Node<T>* node) {
if (!node->right)
transplant(node, node->left);
else if(!node->left)
transplant(node, node->right);
else {
//finding tree minimum;
Node<T>* temp = node;
while (temp->left) {
temp = temp->left;
}
//temp is tree minimum
if (temp->parent != node) {
transplant(temp, temp->right);
temp->right = node->right;
temp->right->parent = temp;
}
transplant(node, temp);
temp->left = node->left;
temp->left->parent = temp;
}
}
void DeleteByString(string s) {
this->Delete(getByString(s));
}
private:
void transplant(Node<T>* original, Node<T>* replacement) {
if (!original->parent)
this->root = replacement;
else if (original == original->parent->right)
original->parent->right = replacement;
else
original->parent->left = replacement;
if (replacement)
replacement->parent = original->parent;
}
Node<T>* root;
};
int main() {
}