-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAKtree.h
247 lines (202 loc) · 6.63 KB
/
AKtree.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
//
// AKtree.h
// Created by AlexKinder on 5/12/20.
//
#ifndef AKtree_h
#define AKtree_h
// advanced prototype for the AKtreeNode to use to declare a friend
template <typename T>
class AKtree;
// ---------------------- AKtreeNode Prototype --------------------------
template <typename T>
class AKtreeNode {
friend class AKtree<T>;
protected:
AKtreeNode *firstChild, *sib, *prev;
T data;
AKtreeNode *myRoot; // needed to test for certain error
public:
AKtreeNode(const T& d = T(),
AKtreeNode *sb = NULL, AKtreeNode *chld = NULL, AKtreeNode *prv = NULL)
: firstChild(chld), sib(sb), prev(prv), data(d), myRoot(NULL) { }
T GetData() const { return data; }
protected:
// for use only by AKtree
AKtreeNode(const T& d,
AKtreeNode *sb, AKtreeNode *chld, AKtreeNode *prv,
AKtreeNode *root)
: firstChild(chld), sib(sb), prev(prv), data(d), myRoot(root) { }
};
// ---------------------- AKtree Prototype --------------------------
template <typename T>
class AKtree {
protected:
int mSize;
AKtreeNode<T> *mRoot;
public:
AKtree() { mSize = 0; mRoot = NULL; }
AKtree(const AKtree &rhs) { mRoot = NULL; mSize = 0; *this = rhs; }
virtual ~AKtree() { clear(); }
bool empty() const { return (mSize == 0); }
int size() const { return mSize; }
void clear() { removeNode(mRoot); }
const AKtree & operator=(const AKtree &rhs);
AKtreeNode<T> *addChild(AKtreeNode<T> *treeNode, const T& x);
AKtreeNode<T> *find(const T& x) { return find(mRoot, x); }
AKtreeNode<T> *find(AKtreeNode<T> *root,
const T& x, int level = 0);
bool remove(const T& x) { return remove(mRoot, x); }
bool remove(AKtreeNode<T> *root, const T& x);
void removeNode(AKtreeNode<T> *nodeToDelete);
void display(AKtreeNode<T> *treeNode = NULL, int level = 0) const;
template <class Processor>
void traverse(Processor func, AKtreeNode<T> *treeNode = NULL) const;
protected:
AKtreeNode<T> *clone(AKtreeNode<T> *root) const;
void setMyRoots(AKtreeNode<T> *treeNode);
};
// public interface methods of AKtree ------------------------
template <typename T>
AKtreeNode<T>* AKtree<T>::find(AKtreeNode<T> *root,
const T& x, int level) {
AKtreeNode<T> *retval;
if (mSize == 0 || root == NULL)
return NULL;
if (root->data == x)
return root;
// otherwise, recurse. don't process sibs if this was the original call
if (level > 0 && (retval = find(root->sib, x, level)))
return retval;
return find(root->firstChild, x, ++level);
}
template <typename T>
bool AKtree<T>::remove(AKtreeNode<T> *root, const T& x) {
AKtreeNode<T> *tn = NULL;
if (mSize == 0 || root == NULL)
return false;
if ((tn = find(root, x)) != NULL)
{
removeNode(tn);
return true;
}
return false;
}
template <typename T>
const AKtree<T> &AKtree<T>::operator= (const AKtree &rhs) {
if (&rhs != this) {
clear();
mRoot = clone(rhs.mRoot);
mSize = rhs.mSize;
setMyRoots(mRoot);
}
return *this;
}
template <typename T>
void AKtree<T>::removeNode(AKtreeNode<T> *nodeToDelete) {
if (nodeToDelete == NULL || mRoot == NULL)
return;
if (nodeToDelete->myRoot != mRoot)
return; // silent error, node does not belong to this tree
// remove all the children of this node
while (nodeToDelete->firstChild)
removeNode(nodeToDelete->firstChild);
if (nodeToDelete->prev == NULL)
mRoot = NULL; // last node in tree
else if (nodeToDelete->prev->sib == nodeToDelete)
nodeToDelete->prev->sib = nodeToDelete->sib; // adjust left sibling
else
nodeToDelete->prev->firstChild = nodeToDelete->sib; // adjust parent
// adjust the successor sib's prev pointer
if (nodeToDelete->sib != NULL)
nodeToDelete->sib->prev = nodeToDelete->prev;
delete nodeToDelete;
--mSize;
}
template <typename T>
AKtreeNode<T> *AKtree<T>::addChild(
AKtreeNode<T> *treeNode, const T& x) {
// empty tree? - create a root node if user passes in NULL
if (mSize == 0) {
if (treeNode != NULL)
return NULL; // silent error something's fishy. treeNode can't right
mRoot = new AKtreeNode<T>(x, NULL, NULL, NULL);
mRoot->myRoot = mRoot;
mSize = 1;
return mRoot;
}
if (treeNode == NULL)
return NULL; // silent error inserting into a non_null tree with a null parent
if (treeNode->myRoot != mRoot)
return NULL; // silent error, node does not belong to this tree
// push this node into the head of the sibling list; adjust prev pointers
AKtreeNode<T> *newNode = new AKtreeNode<T>(x,
treeNode->firstChild, NULL, treeNode, mRoot); // sib, child, prev, root
treeNode->firstChild = newNode;
if (newNode->sib != NULL)
newNode->sib->prev = newNode;
++mSize;
return newNode;
}
template <typename T>
void AKtree<T>::display(AKtreeNode<T> *treeNode, int level) const {
AKtreeNode<T> *child;
// this will be static and so will be shared by all calls
static string blankString = " ";
string indent;
// stop runaway indentation/recursion
if (level > (int)blankString.length() - 1) {
cout << blankString << " ... " << endl;
return;
}
indent = blankString.substr(0, level);
if (mRoot == NULL)
return;
if (treeNode == NULL) {
display(mRoot);
return;
}
cout << indent << treeNode->data << endl;
for (child = treeNode->firstChild; child != NULL; child = child->sib)
display(child, level+1);
}
template <typename T>
template <class Processor>
void AKtree<T>::traverse(Processor func, AKtreeNode<T> *treeNode) const {
AKtreeNode<T> *child;
if (mRoot == NULL)
return;
if (treeNode == NULL) {
traverse(func, mRoot);
return;
}
func(treeNode->data);
for (child = treeNode->firstChild; child != NULL; child = child->sib)
traverse(func, child);
}
// FHsearchTree protected method definitions -----------------------------
template <typename T>
AKtreeNode<T> *AKtree<T>::clone(
AKtreeNode<T> *root) const {
AKtreeNode<T> *newNode;
if (root == NULL)
return NULL;
// does not set myRoot which must be done by caller
newNode = new AKtreeNode<T>(
root->data,
clone(root->sib), clone(root->firstChild));
if (newNode->sib)
newNode->sib->prev = newNode;
if (newNode->firstChild)
newNode->firstChild->prev = newNode;
return newNode;
}
template <typename T>
void AKtree<T>::setMyRoots(AKtreeNode<T> *treeNode) {
AKtreeNode<T> *child;
if (mRoot == NULL)
return;
treeNode->myRoot = mRoot;
for (child = treeNode->firstChild; child != NULL; child = child->sib)
setMyRoots(child);
}
#endif /* AKtree_h */