-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.h
317 lines (265 loc) · 8.32 KB
/
BinaryTree.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) Kamaledin Ghiasi-Shirazi, Ferdowsi Univerity of Mashhad, 2016 (1395 Hijri Shamsi)
//
// Author: Kamaledin Ghiasi-Shirazi
#pragma once
#include <string>
using namespace std;
template <class T, class IBTN>
class BinaryTree {
public:
class BinaryTreeNode {
public:
virtual ~BinaryTreeNode(void) {}
virtual T& getData() { return mActualNode->getData(); }
virtual void setData(const T& data) { return mActualNode->setData(data); }
virtual bool hasLeftChild() const { return mActualNode->getLeftChild() != 0; }
virtual BinaryTreeNode getLeftChild()const { return BinaryTreeNode(mActualNode->getLeftChild()); }
virtual bool hasRightChild() const { return mActualNode->getRightChild() != 0; }
virtual BinaryTreeNode getRightChild() const { return BinaryTreeNode(mActualNode->getRightChild()); }
template<typename S, typename R>
friend class AvlBinaryTree;
template<typename S, typename R>
friend class IndexedAvlBinaryTree;
template<class T>
friend class InternalBinaryAvlTreeNode;
private:
template <class T, class R>
friend class BinaryTree;
BinaryTreeNode(IBTN* node) { mActualNode = node; }
IBTN *mActualNode;
};
protected:
/*
ReverseInorderEnd
----------------------|---------------------
| |
ReversePostorderEnd InorderEnd/ReversePreorderOEnd
-------------------|-------------------
| |
ActualTree PreorderEnd/PostorderEnd
mRevInEnd
----------------------|---------------------
| |
mRevPostEnd mInEnd=mRevPreEnd
-------------------|-------------------
| |
ActualTree mPreEnd=mPostEnd
*/
IBTN* mRevInEnd;
IBTN* mRevPostEnd;
IBTN* mInEnd;
IBTN* mRevPreEnd;
IBTN* mPreEnd;
IBTN* mPostEnd;
int mNodeDisplayWidth;
int mSize;
public:
BinaryTree(void) {
mNodeDisplayWidth = 4; // Don't change this value
mSize = 0;
mRevInEnd = new IBTN();
mRevPostEnd = new IBTN();
mInEnd = new IBTN();
mRevPreEnd = mInEnd;
mPreEnd = new IBTN();
mPostEnd = mPreEnd;
mRevInEnd->mLeftChild = mRevPostEnd;
mRevPostEnd->mParent = mRevInEnd;
mRevInEnd->mRightChild = mInEnd;
mInEnd->mParent = mRevInEnd;
mInEnd->mRightChild = mPreEnd;
mPreEnd->mParent = mInEnd;
}
// caution: don't interpret virtual destrucor as an ordinary virtual function!
virtual ~BinaryTree(void) {
DeleteSubtree(mRevInEnd);
}
int size() { return mSize; }
void DeleteSubtree(IBTN* node) {
if (node->mLeftChild)
DeleteSubtree(node->mLeftChild);
if (node->mRightChild)
DeleteSubtree(node->mRightChild);
delete node;
}
bool isEmpty() {
return mInEnd->mLeftChild == 0;
}
virtual void insertRootNode(T data) {
IBTN* root;
if (mInEnd->mLeftChild)
throw ("Error: Root already exists.");
root = new IBTN();
root->mParent = mInEnd;
*root->mData = data;
root->mLeftChild = 0;
root->mRightChild = 0;
mInEnd->mLeftChild = root;
mSize = 1;
}
virtual BinaryTreeNode getRootNode() { return BinaryTreeNode(mInEnd->mLeftChild); }
virtual BinaryTreeNode getHeaderRootNode() { return BinaryTreeNode(mRevInEnd); }
// error if a left child already exists.
virtual void insertLeftChild(const BinaryTreeNode& parentNode, T data) {
if (parentNode.mActualNode->mLeftChild)
throw ("Error: left child already exists.");
IBTN* newnode = new IBTN();
newnode->setData(data);
parentNode.mActualNode->mLeftChild = newnode;
newnode->mParent = parentNode.mActualNode;
mSize++;
}
// error if a right child already exists.
virtual void insertRightChild(const BinaryTreeNode& parentNode, T data) {
if (parentNode.mActualNode->mRightChild)
throw ("Error: right child already exists.");
IBTN* newnode = new IBTN();
newnode->setData(data);
parentNode.mActualNode->mRightChild = newnode;
newnode->mParent = parentNode.mActualNode;
mSize++;
}
// Only leaf nodes and nodes with degree 1 can be deleted.
// If a degree-1 node is deleted, it is replaced by its subtree.
virtual void deleteNode(const BinaryTreeNode& node) {
//std::cout << node.mActualNode->toString();
if (node.mActualNode->mRightChild) {
if (node.mActualNode->mLeftChild) {
throw "Error: the degree is 2";//comment this to allow delete
//BinaryTreeNode* itnode = &node.getLeftChild();
//while (itnode->hasRightChild()) {
// itnode = &itnode->getRightChild();
//}
//node.mActualNode->mData = itnode->mActualNode->mData;
//deleteNode(*itnode);
//return;
}
//here only have right child D
if (node.mActualNode->mParent->mLeftChild == node.mActualNode) {
node.mActualNode->mParent->mLeftChild = node.mActualNode->mRightChild;
node.mActualNode->mRightChild->mParent = node.mActualNode->mParent;
}
else if (node.mActualNode->mParent->mRightChild == node.mActualNode) {
node.mActualNode->mParent->mRightChild = node.mActualNode->mRightChild;
node.mActualNode->mRightChild->mParent = node.mActualNode->mParent;
}
}
else if (node.mActualNode->mLeftChild) {
//only have left child
if (node.mActualNode->mParent->mRightChild == node.mActualNode) {
node.mActualNode->mParent->mRightChild = node.mActualNode->mLeftChild;
node.mActualNode->mLeftChild->mParent = node.mActualNode->mParent;
}
else if (node.mActualNode->mParent->mLeftChild == node.mActualNode) {
node.mActualNode->mParent->mLeftChild = node.mActualNode->mLeftChild;
node.mActualNode->mLeftChild->mParent = node.mActualNode->mParent; // debugr :)
}
}
else {
if (node.mActualNode->mParent->mRightChild == node.mActualNode)
node.mActualNode->mParent->mRightChild = 0;
else
node.mActualNode->mParent->mLeftChild = 0;
}
delete node.mActualNode;
mSize--;
}
void setNodeDisplayWidth(int width) {
mNodeDisplayWidth = width;
}
// This function is solely written to work on small binary trees.
// The code has many other known limitations.
void draw(std::ostream& out) {
if (mSize == 0)
return;
int maxDepth = 9;
int depth = depthCalc(getRootNode().mActualNode);
if (depth > maxDepth) {
out << "Can't draw, the height of the tree is greater than " << (maxDepth + 1) / 2 << "\n";
return;
}
depth = depth * 2 - 1;
char **map = new char *[depth];
for (int i = 0; i < depth; i++) {
map[i] = new char[160];
for (int j = 0; j < 160; j++)
map[i][j] = 0;
}
recursiveDraw(getRootNode().mActualNode, map, 80, 0);
for (int i = 0; i < depth; i++)
{
for (int j = 0; j < 160; j++)
out << map[i][j];
out << endl;
}
for (int i = 0; i < depth; i++)
delete[] map[i];
delete[] map;
}
int depthCalc(IBTN* root) {
int res = 1;
if (root->mRightChild) {
int rightDepth = depthCalc(root->mRightChild) + 1;
res = rightDepth;
}
if (root->mLeftChild) {
int leftDepth = depthCalc(root->mLeftChild) + 1;
res = (res > leftDepth) ? res : leftDepth;
}
return res;
}
void recursiveDraw(IBTN* root, char** lines, int x, int y) {
int des = 1;
for (int i = 0; i < y / 2 + 2; i++)
des *= 2;
des = 160 / des;
//root:
string s = root->toString();
//s += "";
//s += root->mParent->toString();
int m = s.length() < mNodeDisplayWidth ? s.length() : mNodeDisplayWidth;
for (int i = 0; i < m; i++)
lines[y][x + i - mNodeDisplayWidth / 2] = s[i];
//left child:
if (root->mLeftChild) {
for (int i = 0; i < des; i++)
lines[y + 1][x - i] = '-';
lines[y + 1][x] = '|';
recursiveDraw(root->mLeftChild, lines, x - des, y + 2);
}
//right child:
if (root->mRightChild) {
for (int i = 0; i < des; i++)
lines[y + 1][x + i] = '-';
lines[y + 1][x] = '|';
recursiveDraw(root->mRightChild, lines, x + des, y + 2);
}
}
virtual string toString() {
string str;
if (mSize == 0)
return str;
int maxDepth = 20;
int depth = depthCalc(getActualNode(getRootNode()));
if (depth > maxDepth) {
return str;
}
depth = depth * 2 - 1;
char **map = new char *[depth];
for (int i = 0; i < depth; i++) {
map[i] = new char[160];
for (int j = 0; j < 160; j++)
map[i][j] = ' ';
}
recursiveDraw(getActualNode(getRootNode()), map, 80, 0);
for (int i = 0; i < depth; i++)
for (int j = 0; j < 160; j++)
str += map[i][j];
for (int i = 0; i < depth; i++)
delete[] map[i];
delete[] map;
return str;
}
protected:
virtual IBTN* getActualNode(const BinaryTreeNode& node) { return node.mActualNode; }
};