-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
54 lines (44 loc) · 936 Bytes
/
tree.js
File metadata and controls
54 lines (44 loc) · 936 Bytes
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
class TreeNode {
constructor(name, type, img) {
this.name = name;
this.type = type;
this.imagePath = img;
this.children = [];
this.parent = null;
}
addChild(node) {
if (!(node instanceof TreeNode)) {
throw new Error('Invalid node. Node must be an instance of TreeNode.');
}
node.parent = this;
this.children.push(node);
}
removeChild(node) {
const index = this.children.indexOf(node);
if (index !== -1) {
this.children.splice(index, 1);
node.parent = null;
}
}
getChild(index) {
if (index < 0 || index >= this.children.length) {
throw new Error('Index out of bounds.');
}
return this.children[index];
}
getChildren() {
return this.children;
}
getParent() {
return this.parent;
}
getName() {
return this.name;
}
getType() {
return this.type;
}
getImagePath() {
return this.imagePath;
}
}