-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseBranch.hpp
263 lines (220 loc) · 6.68 KB
/
baseBranch.hpp
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
#ifndef BASEBRANCH_HPP
#define BASEBRANCH_HPP
#include <iostream>
#include <string>
#include <stdio.h>
template <typename Type>
class baseBranch
{
protected:
Type data;
baseBranch<Type> *right;
baseBranch<Type> *left;
public:
baseBranch() {
this->data = 0;
this->right = this->left = NULL;
}
baseBranch(Type e) {
this->data = e;
this->right = this->left = NULL;
}
baseBranch(const baseBranch &b) {
this->data = b.data;
this->right = b.right;
this->left = b.left;
}
Type Data() const { return this->data; }
void operator=(const baseBranch &b) {
this->data = b.data;
this->right = b.right;
this->left = b.left;
}
void operator=(const Type t) {
this->data = t;
}
template <typename branch>
void rotateAntiClockwise(branch **thisAddrPtr) {
#ifdef TESTING
if (*thisAddrPtr != this || not this->right)
throw "Wrong node selected for rotating anti clockwise";
#endif
branch *k = (branch *)((branch *)this->right)->left;
this->right->left = (branch *)this;
*thisAddrPtr = (branch *)this->right;
this->right = k;
}
template <typename branch>
void rotateClockwise(branch **thisAddrPtr) {
#ifdef TESTING
if (*thisAddrPtr != this || not this->left)
throw "Wrong node selected for rotating anti clockwise";
#endif
branch *k = (branch *)((branch *)this->left)->right;
this->left->right = (branch *)this;
*thisAddrPtr = (branch *)this->left;
this->left = k;
}
bool isLeaf() const { return !this->right && !this->left; }
bool isOnlyRightLinked() const { return this->right && !this->left; }
bool isOnlyLeftLinked() const { return !this->right && this->left; }
bool isRightLinked() const { return this->right; }
bool isLeftLinked() const { return this->left; }
void printAll() const {
std::cout << this->data << " : " << this << "\n\t";
std::cout << "Left : " << this->left << "\n\t";
std::cout << "Right : " << this->right << "\n";
}
void print() const {
std::cout << this->data;
}
void print(std::string s) const {
std::cout << this->data << s;
}
void print_() const {
std::cout << this->data << " ";
}
void println() const {
this->print("\n");
}
template <typename branch>
void removeAll(branch **node) {
#ifdef TESTING
if (*node != this)
throw "Bad memory location";
#endif // TESTING
if (this->left) ((branch *)this->left)->removeAll((branch **)&this->left);
if (this->right) ((branch *)this->right)->removeAll((branch **)&this->right);
delete *node;
*node = NULL;
}
int display_(int is_left, int offset, int depth, char s[50][255]) const ;
void display() const ;
void preOrder (void func(void)) const;
void postOrder (void func(void)) const;
void inOrderInc(void func(void)) const;
void inOrderDec(void func(void)) const;
void preOrder (void func(const Type), const Type) const;
void postOrder(void func(const Type), const Type) const;
void inOrderInc(void func(const Type), const Type) const;
void inOrderDec(void func(const Type), const Type) const;
void copyToArrIncOrder(Type [], size_t &) const;
void copyToArrDecOrder(Type [], size_t &) const;
};
template <typename Type>
void baseBranch<Type>::copyToArrIncOrder(Type A[], size_t &s) const
{
if (this->left) this->left->copyToArrIncOrder(A, s);
A[s++] = this->data;
if (this->right) this->right->copyToArrIncOrder(A, s);
}
template <typename Type>
void baseBranch<Type>::copyToArrDecOrder(Type A[], size_t &size) const
{
if (this->right) this->right->copyToArrDecOrder(A, size);
A[size++] = this->data;
if (this->left) this->left->copyToArrDecOrder(A, size);
}
template <typename Type>
void baseBranch<Type>::preOrder(void func(void)) const
{
func();
if (this->left) this->left->preOrder(func);
if (this->right) this->right->preOrder(func);
}
template <typename Type>
void baseBranch<Type>::postOrder(void func(void)) const
{
if (this->left) this->left->postOrder(func);
if (this->right) this->right->postOrder(func);
func();
}
template <typename Type>
void baseBranch<Type>::inOrderInc(void func(void)) const
{
if (this->left) this->left->inOrderInc(func);
func();
if (this->right) this->right->inOrderInc(func);
}
template <typename Type>
void baseBranch<Type>::inOrderDec(void func(void)) const
{
if (this->right) this->right->inOrderDec(func);
func();
if (this->left) this->left->inOrderDec(func);
}
template <typename Type>
void baseBranch<Type>::preOrder(void func(const Type), const Type x) const
{
func(x);
if (this->left) this->left->preOrder(func, x);
if (this->right) this->right->preOrder(func, x);
}
template <typename Type>
void baseBranch<Type>::postOrder(void func(const Type), const Type x) const
{
if (this->left) this->left->postOrder(func, x);
if (this->right) this->right->postOrder(func, x);
func(x);
}
template <typename Type>
void baseBranch<Type>::inOrderInc(void func(const Type), const Type x) const
{
if (this->left) this->left->inOrderInc(func, x);
func(x);
if (this->right) this->right->inOrderInc(func, x);
}
template <typename Type>
void baseBranch<Type>::inOrderDec(void func(const Type), const Type x) const
{
if (this->right) this->right->inOrderDec(func, x);
func(x);
if (this->left) this->left->inOrderDec(func, x);
}
template <typename Type>
int baseBranch<Type>::display_(int is_left, int offset, int depth, char s[50][255]) const {
char b[20];
int width = 1;
sprintf(b, "%d", this->data);
int left = this->left ? this->left->display_(1, offset, depth + 1, s) : 0;
int right = this->right ? this->right->display_(0, offset + left + width, depth + 1, s) : 0;
#define COMPACT
#ifdef COMPACT
for (int i = 0; i < width; i++)
s[depth][offset + left + i] = b[i];
if (depth && is_left) {
for (int i = 0; i < width + right; i++)
s[depth - 1][offset + left + width/2 + i] = '-';
s[depth - 1][offset + left + width/2] = '.';
} else if (depth && !is_left) {
for (int i = 0; i < left + width; i++)
s[depth - 1][offset - width/2 + i] = '-';
s[depth - 1][offset + left + width/2] = '.';
}
#else
for (int i = 0; i < width; i++)
s[2 * depth][offset + left + i] = b[i];
if (depth && is_left) {
for (int i = 0; i < width + right; i++)
s[2 * depth - 1][offset + left + width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset + left + width + right + width/2] = '+';
} else if (depth && !is_left) {
for (int i = 0; i < left + width; i++)
s[2 * depth - 1][offset - width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset - width/2 - 1] = '+';
}
#endif
return left + width + right;
}
template <typename Type>
void baseBranch<Type>::display() const {
char s[50][255];
for (int i = 0; i < 10; i++)
sprintf(s[i], "%80s", " ");
this->display_(0, 0, 0, s);
for (int i = 0; i < 10; i++)
printf("%s\n", s[i]);
}
#endif