-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path2-five.js
More file actions
47 lines (41 loc) · 906 Bytes
/
2-five.js
File metadata and controls
47 lines (41 loc) · 906 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
'use strict';
class Node {
constructor(parent, data) {
this.data = data;
this.parent = parent;
this.count = 0;
this.prev = null;
this.next = null;
this.first = null;
this.last = null;
if (parent) {
this.tree = parent.tree;
this.tree.count++;
if (!parent.count) {
parent.first = this;
}
if (parent.last) {
parent.last.next = this;
this.prev = parent.last;
}
parent.last = this;
parent.count++;
}
}
add(data) {
return new Node(this, data);
}
}
class Tree {
constructor(data) {
this.count = 1;
this.root = new Node(null, data);
this.root.tree = this;
}
}
// Usage
const tree = new Tree({ name: 'root' });
const n1 = tree.root.add({ name: 'n1' });
const n2 = tree.root.add({ name: 'n2' });
const n3 = tree.root.add({ name: 'n3' });
console.dir(tree, { depth: null });