-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.h
241 lines (196 loc) · 6.5 KB
/
tree.h
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
#ifndef TREE_H
#define TREE_H
#include "treenode.h"
template<class T>
class BinarySearchTree {
private:
int mod(int num) {
return num>0?num:-num;
}
TreeNode<T>* rightRotate(TreeNode<T>* y)
{
TreeNode<T>* x = y->leftChild.get();
TreeNode<T>* T2 = x->rightChild.get();
// Perform rotation
x->rightChild.release();
x->rightChild.reset(y);
TreeNode<T>* temp = y->parent;
y->parent = x;
y->leftChild.release();
y->leftChild.reset(T2);
if(T2)
T2->parent = y;
x->parent = temp;
// Return new root
return x;
}
// A utility function to left rotate subtree rooted with x
TreeNode<T>* leftRotate(TreeNode<T>* x)
{
TreeNode<T>* y = x->rightChild.get();
TreeNode<T>* T2 = y->leftChild.get();
// Perform rotation
y->leftChild.release();
y->leftChild.reset(x);
TreeNode<T>* temp = x->parent;
x->parent = y;
x->rightChild.release();
(x->rightChild).reset(T2);
// cout<<x->data<<endl;
if(T2)
T2->parent = x;
y->parent = temp;
// Return new root
// cout<<x->data<<endl<<y->data<<endl<<y;
return y;
}
TreeNode<T>* checkAndBalance(TreeNode<T>* root, T data ,int flag) {
int balance = root->leftChild->maxDepth() - root->rightChild->maxDepth();
if(mod(balance)<=1)
return NULL;
else {
if(flag==0) {
if(data<root->data) {
TreeNode<T>* temp = leftRotate(root->leftChild.get());
root->leftChild.release();
root->leftChild.reset(temp);
return rightRotate(root);
}
else {
// left rotation
TreeNode<T>* temp = root->parent;
if(temp==NULL)
return leftRotate(root);
if(temp->leftChild.get()==root) {
TreeNode<T>* temp2 = leftRotate(root);
temp->leftChild.release();
temp->leftChild.reset(temp2);
return temp2;
}
else {
TreeNode<T>* temp2 = leftRotate(root);
temp->rightChild.release();
temp->rightChild.reset(temp2);
return temp2;
}
// return temp;
}
}
else {
if(data<root->data) {
//right rotation
TreeNode<T>* temp = root->parent;
if(temp==NULL)
return rightRotate(root);
if(temp->leftChild.get()==root) {
TreeNode<T>* temp2 = rightRotate(root);
temp->leftChild.release();
temp->leftChild.reset(temp2);
return temp2;
}
else {
TreeNode<T>* temp2 = leftRotate(root);
temp->rightChild.release();
temp->rightChild.reset(temp2);
return temp2;
}
}
else {
// right left rotation
TreeNode<T>* temp = rightRotate(root->rightChild.get());
root->rightChild.release();
root->rightChild.reset(temp);
return leftRotate(root);
}
}
}
}
TreeNode<T> *insert(unique_ptr<TreeNode<T>> &ptr, T data) {
// cout<< "data add"<<data;
if (ptr.get() == NULL) {
return NULL;
}
if (ptr->data < data){
if(ptr->rightChild.get()==NULL) {
TreeNode<T>* insertedNode = new TreeNode<T>(data);
ptr->setRightChild(insertedNode);
if(ptr->parent) {
checkAndBalance(ptr->parent, data, 0);
// if(temp)
// return temp;
}
return insertedNode;
}
return insert(ptr->rightChild, data);
}
else{
if(ptr->leftChild.get()==NULL) {
TreeNode<T>* insertedNode = new TreeNode<T>(data);
ptr->setLeftChild(insertedNode);
if(ptr->parent) {
checkAndBalance(ptr->parent, data, 1);
// if(temp)
// return temp;
}
return insertedNode;
}
return insert(ptr->leftChild, data);
}
}
TreeNode<T> *find(unique_ptr<TreeNode<T>> &ptr, T data) {
if (ptr.get() == nullptr)
return nullptr;
if (ptr->data < data)
return find(ptr->rightChild, data);
else if (data < ptr->data)
return find(ptr->leftChild, data);
else
return ptr.get();
}
public:
unique_ptr<TreeNode<T>> root;
void write(ostream &output) const {
root->write(output);
}
BinarySearchTree() {
this->root = nullptr;
}
TreeNode<T> *insert(T data) {
if(this->root==nullptr){
root.reset(new TreeNode<T>(data));
return root.get();
}
TreeNode<T>* temp = insert(this->root, data);
while(this->root->parent!=NULL){
TreeNode<T>* temp2 = this->root->parent;
this->root.release();
this->root.reset(temp2);
}
// cout<<root->data<<"end ";
return temp;
}
TreeNode<T> *find(T data) {
return find(this->root, data);
}
int maxDepth() {
if(root==nullptr) return 0;
//cout<<root->leftChild->data;
return root->maxDepth();
}
// typename TreeNode<T>::TreeNodeIterator
typename TreeNode<T>::TreeNodeIterator begin() {
if(root==nullptr)
return typename TreeNode<T>::TreeNodeIterator(NULL);
TreeNode<T>* temp = root.get();
//temp.reset(root);
while(temp->leftChild!=nullptr)
temp = (temp->leftChild).get();
return typename TreeNode<T>::TreeNodeIterator(temp);
}
//typename TreeNode<T>::TreeNodeIterator
typename TreeNode<T>::TreeNodeIterator end() {
return typename TreeNode<T>::TreeNodeIterator(NULL);
}
};
// do not edit below this line
#endif