-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinarySearchTree.h
288 lines (257 loc) · 5.27 KB
/
BinarySearchTree.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* BinarySearchTree.h
*
* Created on: 2011-11-28
* Author: morin
*/
#ifndef BINARYSEARCHTREE_H_
#define BINARYSEARCHTREE_H_
#include <climits>
#include <cmath>
#include "BinaryTree.h"
#include "utils.h"
namespace ods {
template<class Node, class T>
class BSTNode : public BTNode<Node> {
public:
T x;
};
/**
* A binary search tree class. The Node parameter should be a subclass
* of BSTNode<T> (or match it's interface)
*/
template<class Node, class T>
class BinarySearchTree : public BinaryTree<Node> {
protected:
using BinaryTree<Node>::r;
using BinaryTree<Node>::nil;
int n;
T null;
virtual Node *findLast(T x);
virtual bool addChild(Node *p, Node *u);
virtual void splice(Node *u);
virtual void remove(Node *u);
virtual void rotateRight(Node *u);
virtual void rotateLeft(Node *u);
virtual bool add(Node *u);
public:
BinarySearchTree();
BinarySearchTree(T null);
virtual ~BinarySearchTree();
virtual bool add(T x);
virtual bool remove(T x);
virtual T find(T x);
virtual T findEQ(T x);
virtual int size();
virtual void clear();
};
template<class T>
class BSTNode1 : public BSTNode<BSTNode1<T>, T> { };
template<class T>
class BinarySearchTree1 : public BinarySearchTree<BSTNode1<T>, T> {
public:
BinarySearchTree1();
};
/*
* FIXME: Why doesn't this work?
template<class Node>
BinarySearchTree<Node,int>::BinarySearchTree() {
this->null = INT_MIN;
n = 0;
}
*/
template<class Node, class T>
BinarySearchTree<Node,T>::BinarySearchTree() {
this->null = T(); // won't work for non-primitive types
n = 0;
}
template<class Node, class T>
BinarySearchTree<Node,T>::BinarySearchTree(T null) {
this->null = null;
n = 0;
}
template<class Node, class T>
Node* BinarySearchTree<Node,T>::findLast(T x) {
Node *w = r, *prev = nil;
while (w != nil) {
prev = w;
int comp = compare(x, w->x);
if (comp < 0) {
w = w->left;
} else if (comp > 0) {
w = w->right;
} else {
return w;
}
}
return prev;
}
template<class Node, class T>
T BinarySearchTree<Node,T>::findEQ(T x) {
Node *w = r;
while (w != nil) {
int comp = compare(x, w->x);
if (comp < 0) {
w = w->left;
} else if (comp > 0) {
w = w->right;
} else {
return w->x;
}
}
return null;
}
template<class Node, class T>
T BinarySearchTree<Node,T>::find(T x) {
Node *w = r, *z = nil;
while (w != nil) {
int comp = compare(x, w->x);
if (comp < 0) {
z = w;
w = w->left;
} else if (comp > 0) {
w = w->right;
} else {
return w->x;
}
}
return z == nil ? null : z->x;
}
template<class Node, class T>
BinarySearchTree<Node,T>::~BinarySearchTree() {
// nothing to do - BinaryTree destructor does cleanup
}
template<class Node, class T>
bool BinarySearchTree<Node, T>::addChild(Node *p, Node *u) {
if (p == nil) {
r = u; // inserting into empty tree
} else {
int comp = compare(u->x, p->x);
if (comp < 0) {
p->left = u;
} else if (comp > 0) {
p->right = u;
} else {
return false; // u.x is already in the tree
}
u->parent = p;
}
n++;
return true;
}
template<class Node, class T>
bool BinarySearchTree<Node, T>::add(T x) {
Node *p = findLast(x);
Node *u = new Node;
u->x = x;
return addChild(p, u);
}
template<class Node, class T>
bool BinarySearchTree<Node, T>::add(Node *u) {
Node *p = findLast(u->x);
return addChild(p, u);
}
template<class Node, class T>
void BinarySearchTree<Node, T>::splice(Node *u) {
Node *s, *p;
if (u->left != nil) {
s = u->left;
} else {
s = u->right;
}
if (u == r) {
r = s;
p = nil;
} else {
p = u->parent;
if (p->left == u) {
p->left = s;
} else {
p->right = s;
}
}
if (s != nil) {
s->parent = p;
}
n--;
}
template<class Node, class T>
void BinarySearchTree<Node, T>::remove(Node *u) {
if (u->left == nil || u->right == nil) {
splice(u);
delete u;
} else {
Node *w = u->right;
while (w->left != nil)
w = w->left;
u->x = w->x;
splice(w);
delete w;
}
}
template<class Node, class T>
bool BinarySearchTree<Node, T>::remove(T x) {
Node *u = findLast(x);
if (u != nil && compare(x, u->x) == 0) {
remove(u);
return true;
}
return false;
}
template<class Node, class T> inline
int BinarySearchTree<Node, T>::size() {
return n;
}
template<class Node, class T> inline
void BinarySearchTree<Node, T>::clear() {
BinaryTree<Node>::clear();
n = 0;
}
template<class Node, class T>
void BinarySearchTree<Node, T>::rotateLeft(Node *u) {
Node *w = u->right;
w->parent = u->parent;
if (w->parent != nil) {
if (w->parent->left == u) {
w->parent->left = w;
} else {
w->parent->right = w;
}
}
u->right = w->left;
if (u->right != nil) {
u->right->parent = u;
}
u->parent = w;
w->left = u;
if (u == r) { r = w; r->parent = nil; }
}
template<class Node, class T>
void BinarySearchTree<Node, T>::rotateRight(Node *u) {
Node *w = u->left;
w->parent = u->parent;
if (w->parent != nil) {
if (w->parent->left == u) {
w->parent->left = w;
} else {
w->parent->right = w;
}
}
u->left = w->right;
if (u->left != nil) {
u->left->parent = u;
}
u->parent = w;
w->right = u;
if (u == r) { r = w; r->parent = nil; }
}
/*
template<class T>
BinarySearchTree1<T*>::BinarySearchTree1() : BinarySearchTree<BSTNode1<T*>, T*>(NULL) {
}
*/
template<class T>
BinarySearchTree1<T>::BinarySearchTree1() {
}
} /* namespace ods */
#endif /* BINARYSEARCHTREE_H_ */