-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyBST.cpp
267 lines (223 loc) · 7.12 KB
/
MyBST.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// MyBST.cpp
// bst_transform
//
#include "MyBST.h"
#include <queue>
/**
* Computes how to transform this MyBST into the target MyBST using rotations.
* Returns a vector of rotations indicating which rotations around which nodes
* must be performed to transform this into the target.
*
* IMPORTANT: We want to transform T1 into T2 so the call should look like
* rotations = t1.transform(t2)
*/
MyBST::MyBST() : BST()
{
}
MyBST::MyBST(vector<int>& nums) : BST(nums)
{
}
Node* MyBST::traverse(int index) //level order traversal
{
queue<Node*> q;
q.push(this->root);
int count = 0;
while(!q.empty())
{
Node* temp_node = q.front();
q.pop();
count++;
//Check if we found the node the index is referring to
if (count == index)
return temp_node;
//check the left of the temp_node
if (temp_node->left)
{
q.push(temp_node->left);
}
//check the right of the temp_node
if (temp_node->right)
{
q.push(temp_node->right);
}
}
//if you get here, the index is too large for the tree
return nullptr;
}
Node* MyBST::find(int value)
{
//finds the node is V1 with the same value as the Node in V2
//returns the node
queue<Node*> q;
q.push(this->root);
while(!q.empty())
{
Node* temp_node = q.front();
q.pop();
if (temp_node->key == value)
{
return temp_node;
}
if (temp_node->left)
{
//temp_node = temp_node->right;
q.push(temp_node->left);
}
if (temp_node->right)
{
//temp_node = temp_node->left;
q.push(temp_node->right);
}
}
return nullptr;
}
int MyBST::getLevel(Node* node, Node* ptr, int height)
{
if (node == nullptr)
{
return 0;
}
if (node->key == ptr->key)
{
return height;
}
int level = getLevel(node->left, ptr, height+1);
if (level != 0)
{
return level;
}
return getLevel(node->right, ptr, height+1);
}
Node* MyBST::findParent(Node* root, int value)
{
if ((root->left == nullptr) && (root->right == nullptr))
{
return nullptr;
}
if ((root->left != nullptr && root->left->key == value) || (root->right != nullptr && root->right->key == value))
{
return root;
}
else if (root->key > value)
{
return findParent(root->left, value);
}
else
{
return findParent(root->right, value);
}
}
vector<Rotation> MyBST::transform(MyBST target) {
//T1.transform(T2);
/*** This is what you have to do ***/
Node* V2 = target.root;
int index2 = 1;
vector<Rotation> instructions;
cout << "at beginning" << endl;
while(V2)
{
cout << "at beginning of while" << endl;
//go through and increment index and everything
Node* V1 = this->find(V2->key);
cout << "findd" << endl;
bool set = false; //only change set to true when the levels are the same
int L2 = target.getLevel(target.root, V2, 1); // L2 is level of V2 in T2
cout << "get level" << endl;
while(!set)
{
int L1 = this->getLevel(this->root, V1, 1); //L1 is level of V2 in T1
cout << "before the set" << endl;
if (L1 == L2) //if level of v1 and level of v2 are equal
{
cout << "at the set" << endl;
set = true;
}
else
{
cout << "about to find parent" << endl;
Node* parent_V1 = this->findParent(this->root, V1->key); //parent_V1 is the parent Node of V1
cout << "found parent" << endl;
if (parent_V1 == nullptr)
{
cout << "uh oh " << endl;
}
cout << "Checking node " << V1->key << " at level " << L1 << " in T1, parent " << parent_V1->key << endl;
if (V1->key < parent_V1->key)
{
//Zig on parent_V1
cout << "if v1<pV1" << endl;
Rotation rot(parent_V1->key, ZIG);
instructions.push_back(rot);
if (parent_V1->key == this->root->key) // if the parent of V1 is the root node fo the BST
{
this->root = rotateRight(parent_V1);
cout << "here at ZIG on parent" << endl;
}
else
{
Node* grandParent_V1 = this->findParent(this->root, parent_V1->key); //grandparent of V1
if (parent_V1->key < grandParent_V1->key) //if parent of V1 is on the left of grandparent of V1
{
grandParent_V1->left = this->rotateRight(parent_V1);
cout << "here at grandparent if ZIG" << endl;
}
else
{
grandParent_V1->right = this->rotateRight(parent_V1);
cout << "here at grandparent else ZIG" << endl;
}
}
}
else if (V1->key > parent_V1->key) // V1 is on the right of its parent
{
//ZAG on parent_V1
cout << "v1>pv1" << endl;
Rotation rot(parent_V1->key, ZAG);
instructions.push_back(rot);
cout << "after instructions" << endl;
if (parent_V1->key == this->root->key)
{
this->root = rotateLeft(parent_V1);
cout << "here at ZAG on parent" << endl;
}
else
{
cout << "about to find grandparent" << endl;
Node* grandParent_V1 = this->findParent(this->root, parent_V1->key);
cout << "found grandparent" << endl;
if (parent_V1->key < grandParent_V1->key) //if parent of V1 is on the left of grandparent of V1
{
grandParent_V1->left = this->rotateLeft(parent_V1);
cout << "here at grandparent if ZAG" << endl;
}
else
{
grandParent_V1->right = this->rotateLeft(parent_V1);
cout << "here at grandparent else ZAG" << endl;
}
}
}
}
}
index2++;
cout << "at traver" << endl;
V2 = target.traverse(index2);
cout << "after traverse" << endl;
}
return instructions;
}
Node* MyBST::rotateRight(Node* Q)
{
Node* P = Q->left;
Q->left = P->right;
P->right = Q;
return P;
}
Node* MyBST::rotateLeft(Node* P)
{
Node* Q = P->right;
P->right = Q->left;
Q->left = P;
return Q;
}