-
Notifications
You must be signed in to change notification settings - Fork 11
/
tree.js
272 lines (231 loc) · 6.79 KB
/
tree.js
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
export class Tree {
constructor() {
this.rootNode = new TreeNode(null);
this.currentNode = this.rootNode;
this[Symbol.iterator] = function () {
return this;
};
}
add(value) {
console.log("adding", value, "to the tree");
// dont add empty/null
if (!value) throw "adding a falsy value to the tree.";
// if value is not an array, make it one.
if (!Array.isArray(value)) {
value = [value];
}
// filter out any falsy values...
value = value.filter((x) => x);
if (value.length == 0) throw "cannot add an empty or falsy array to the tree";
value.forEach((x) => {
let child = new TreeNode(x);
child.setParent(this.currentNode);
this.currentNode.addChild(child);
});
//console.log(this.currentNode.children, this.rootNode.children);
}
hasNext() {
return !!this.nextNode.value;
}
isFirst() {
return this.currentNode.parent === this.rootNode;
}
clear() {
this.rootNode.clearChildren();
this.currentNode = this.rootNode;
}
// ask the current TreeNode what is the next value...
next() {
let tmp = this.currentNode.next();
if (!tmp.done) {
this.currentNode = tmp.value;
this.ptree();
}
return tmp;
}
// ask the TreeNode what is your previous value....
// if you cannot go back, the returned element
// is undefined!
previous() {
let tmp = this.currentNode.previous();
if (!tmp.done) {
this.currentNode = tmp.value;
this.currentNode.clearChildren();
//this.currentNode.children.forEach((child) => child.clearChildren());
console.log(" ================ PREV ================ ");
this.ptree();
}
return tmp;
}
// remove the current element
// (this will remove ALL the children element of the current element)
prune() {
this.currentNode.removeFromParent();
this.previous();
}
// you can ONLY pop a node that has no
// children.
pop() {
if (this.currentNode.children.length > 0) return;
let tmp = this.currentNode;
this.previous();
tmp.removeFromParent();
}
isEmpty() {
return this.rootNode.children.length == 0;
}
toVanillaObject() {
function nodeJSON(child) {
let value = child.value;
let kidsValue = child.children.map((x) => nodeJSON(x));
return { value: value, children: kidsValue };
}
let obj = {}
obj.rootNode = nodeJSON(this.rootNode);
obj.currentNode = this.currentNode.value;
return obj
}
loadFromVanillaObject(object) {
// reset the root's children
if (!this.rootNode.children) throw "children is null?";
this.rootNode.children.length = 0;
// we lose "this" reference in the mapping...
let thisObj = this;
function addKids(node, kidsArray) {
if (kidsArray.length == 0) return;
kidsArray.forEach((kid) => {
let kidNode = new TreeNode(kid.value);
if (object.currentNode == kidNode.value) {
thisObj.currentNode = kidNode;
}
node.addChild(kidNode);
addKids(kidNode, kid.children);
});
}
addKids(this.rootNode, object.rootNode.children);
}
static fromJSON(json) {
let object = JSON.parse(json);
let newTree = new Tree();
newTree.loadFromVanillaObject(object);
return newTree;
}
loadFromJSON(json) {
this.clear()
let object = JSON.parse(json);
this.loadFromVanillaObject(object);
}
toJSON() {
let json = JSON.stringify(this.toVanillaObject());
return json;
}
ptree() {
console.log(" ============ TREE ===========");
let node = this.rootNode.next();
while (!node.done) {
console.log(node.value.value, node.value.value == this.currentNode.value ? " <=== currentNode" : "");
node = node.value.next();
}
console.log(" ============================= ");
}
}
class TreeNode {
constructor(value) {
this.value = value;
this.parent = null;
this.children = [];
}
setParent(parent) {
this.parent = parent;
}
addChild(child) {
child.parent = this;
this.children.push(child);
}
lookForNext(child) {
// child asked for the next node ...
// lets find his index....
let childIndex = this.children.indexOf(child);
// not sure how the index could not be found...
// unless misused...
if (childIndex == -1) {
return { done: true, value: undefined };
}
// get the next index and if
// it is still a valid index
if (++childIndex < this.children.length) {
//return this.children[childIndex];
return { done: false, value: this.children[childIndex] };
}
// child was the last element of the array,
// so ask our parent for the next element...
// but if we are the root.. return null...
if (this.parent == null) {
return { done: true, value: undefined };
}
return this.parent.lookForNext(this);
}
next() {
if (this.children.length > 0) {
return { done: false, value: this.children[0] };
}
if (this.parent == null) return { done: true, value: undefined };
if (this.parent.value == null) return { done: true };
let myNext = this.parent.lookForNext(this);
if (myNext.done) {
return { done: true, value: undefined };
}
return myNext;
}
lookForPreviousNode(child) {
// my child asked my to look for his previous sibling...
// first get the index of my child
let childIndex = this.children.indexOf(child);
// not sure how the index could not be found...
// unless misused... Should I throw an exception?
if (childIndex == -1) {
return { done: true, value: undefined };
}
if (childIndex > 0) {
let lastChild = this.children[childIndex - 1].getLastChild();
return { done: false, value: lastChild };
}
// Mu first child is calling, return me...
// unless I am the root, we never return the root.
if (this.parent == null) {
return { done: true, value: undefined };
}
return { done: false, value: this };
}
getLastChild() {
// I have been asked to by my parent to get the last child
// if I dont have any children.. return me.
if (this.children.length > 0) {
return this.children[this.children.length - 1].getLastChild();
}
return this;
}
previous() {
// ask my parent for the previous sibling (or if I am the first, it's my parent)
if (this.parent) {
return this.parent.lookForPreviousNode(this);
}
// if you are at the root, you wont have a parent
// and you cannot go back...
return { done: true, value: undefined };
}
iterator() {
return new Tree(this);
}
clearChildren() {
this.children = [];
}
removeChild(child) {
let childIndex = this.children.indexOf(child);
if (childIndex == -1) return;
this.children.splice(childIndex, 1);
}
removeFromParent() {
this.parent.removeChild(this);
}
}