-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtree-generic.js
211 lines (156 loc) · 4.18 KB
/
tree-generic.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
const uniqueId = (() => {
function* uniqueIdGenerator() {
let id = Date.now();
while(true) {
yield id++;
}
}
const gen = uniqueIdGenerator();
return () => gen.next().value;
})()
class Tree {
#children = new Map();
#parent = null;
#id = uniqueId();
#name;
constructor(name) {
if(!name || typeof name !== 'string' || !name.trim().length) {
throw new Error('Name must be a non-empty String');
}
this.#name = name;
}
get name() {
return this.#name;
}
set name(newName) {
if(!newName || typeof newName !== 'string' || !newName.trim().length) {
throw new Error('Cannot change name.Name must be a non-empty String');
}
this.#name = newName;
}
get identifier() {
return this.#id;
}
get children() {
return Array.from(this.#children.values());
}
get parentNode() {
return this.#parent;
}
set parentNode(newParent) {
if(newParent !== this.parentNode && (newParent === null || newParent instanceof Tree)) {
if(this.#parent) {
this.#parent.removeChildNode(this);
}
this.#parent = newParent;
if(newParent) {
newParent.appendChildNode(this);
}
}
}
get childrenCount() {
return this.#children.size;
}
createChildNode(name) {
const newNode = new Tree(name);
this.#children.set(newNode.identifier, newNode);
newNode.parentNode = this;
return newNode;
}
hasChildNode(needle) {
if(needle instanceof Tree) {
return this.#children.has(needle.identifier);
}
for(let child of this.children) {
if(child.name === needle || this.identifier === needle) {
return true
}
}
return false;
}
getChildNode(nameOrId) {
for(let child of this.children) {
if(child.name === nameOrId || this.identifier === nameOrId) {
return child;
}
}
return null;
}
appendChildNode(node) {
if(!(node instanceof Tree) || this.hasChildNode(node)) return;
if(node === this) throw new Error('Node cannot contain itself');
let parent = this.parentNode;
while(parent !== null) {
if(parent === node) throw new Error('Node cannot contain one of its ancestors');
parent = parent.parentNode;
}
this.#children.set(node.identifier, node);
node.parentNode = this;
}
removeChildNode(needle) {
if(!this.hasChildNode(needle)) return;
let removedNode;
if(needle instanceof Tree) {
this.#children.delete(needle.identifier);
removedNode = needle;
} else {
for(let child of this.children) {
if(child.name === needle || child.identifier === needle) {
this.#children.delete(child.identifier);
removedNode = child;
break;
}
}
}
if(removedNode) {
removedNode.parentNode = null;
}
}
appendChildNode(node) {
if(!(node instanceof Tree) || this.hasChildNode(node)) return;
if(node === this) throw new Error('Node cannot contain itself');
let parent = this.parentNode;
while(parent !== null) {
if(parent === node) throw new Error('Node cannot contain one of its ancestors');
parent = parent.parentNode;
}
this.#children.set(node.identifier, node);
node.parentNode = this;
}
#getTreeString = (node, spaceCount = 0) => {
let str = "\n";
node.children.forEach((child) => {
str += `${" ".repeat(spaceCount)}${child.name}${this.#getTreeString(child, spaceCount + 2)}`
})
return str;
}
print() {
console.log(`\n${this.name}${this.#getTreeString(this, 2)}`);
}
traverse(cb) {
for(let child of this.children) {
if(cb(child) === true || child.traverse(cb) === true) {
return true;
}
}
}
findNodeByName(name) {
let foundNode = null;
this.traverse(node => {
if(node.name === name) {
foundNode = node;
return true;
}
})
return foundNode;
}
findAllNodesByName(name) {
const children = [];
this.traverse(node => {
if(node.name === name ) {
children.push(node);
}
})
return children;
}
}