-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathtree.es6.js
167 lines (154 loc) · 3.96 KB
/
tree.es6.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
function Node(data) {
this.data = data;
this.children = [];
}
class Tree {
constructor() {
this.root = null;
}
add(data, toNodeData) {
const node = new Node(data);
const parent = toNodeData ? this.findBFS(toNodeData) : null;
if (parent) {
parent.children.push(node);
} else {
if (this.root) {
throw new Error('Root node is already assigned');
}
this.root = node;
}
}
remove(data) {
if(this.root.data === data) {
this.root = null;
}
const queue = [this.root];
while(queue.length) {
const node = queue.shift();
for (let [index, child] of node.children.entries()) {
if (child.data === data) {
node.children.splice(index, 1);
} else {
queue.push(child);
}
}
}
}
contains(data) {
return !!this.findBFS(data);
}
findBFS(data) {
const queue = [this.root];
while(queue.length) {
const node = queue.shift();
if (node.data === data) {
return node;
}
for (const child of node.children) {
queue.push(child);
}
}
return null;
}
_preOrder(node, fn) {
if (node) {
if (fn) {
fn(node);
}
for(const child of node.children) {
this._preOrder(child, fn);
}
}
}
_postOrder(node, fn) {
if (node) {
for (const child of node.children) {
this._postOrder(child, fn);
}
if (fn) {
fn(node);
}
}
}
traverseDFS(fn, method) {
const current = this.root;
if (method) {
this[`_${method}`](current, fn);
} else {
this._preOrder(current, fn);
}
}
traverseBFS(fn) {
const queue = [this.root];
while (queue.length) {
const node = queue.shift();
if (fn) {
fn(node);
}
for (const child of node.children) {
queue.push(child);
}
}
}
print() {
if (!this.root) {
return console.log('No root node found');
}
const newline = new Node('|');
const queue = [this.root, newline];
let string = '';
while (queue.length) {
const node = queue.shift();
string += `${node.data.toString()} `;
if (node === newline && queue.length) {
queue.push(newline);
}
for (const child of node.children) {
queue.push(child);
}
}
console.log(string.slice(0, -2).trim());
}
printByLevel() {
if(!this.root) {
return console.log('No root node found');
}
const newline = new Node('\n');
const queue = [this.root, newline];
let string = '';
while(queue.length) {
const node = queue.shift();
string += node.data.toString() + (node.data !== '\n' ? ' ' : '');
if(node === newline && queue.length) {
queue.push(newline);
}
for(const child of node.children) {
queue.push(child);
}
}
console.log(string.trim());
}
}
const tree = new Tree();
tree.add('ceo');
tree.add('cto', 'ceo');
tree.add('dev1', 'cto');
tree.add('dev2', 'cto');
tree.add('dev3', 'cto');
tree.add('cfo', 'ceo');
tree.add('accountant', 'cfo');
tree.add('cmo', 'ceo');
tree.print(); // => ceo | cto cfo cmo | dev1 dev2 dev3 accountant
tree.printByLevel(); // => ceo \n cto cfo cmo \n dev1 dev2 dev3 accountant
console.log('tree contains dev1 is true:', tree.contains('dev1')); // => true
console.log('tree contains dev4 is false:', tree.contains('dev4')); // => false
console.log('--- BFS');
tree.traverseBFS(node => { console.log(node.data); }); // => ceo cto cfo cmo dev1 dev2 dev3 accountant
console.log('--- DFS preOrder');
tree.traverseDFS(node => { console.log(node.data); }, 'preOrder'); // => ceo cto dev1 dev2 dev3 cfo accountant cmo
console.log('--- DFS postOrder');
tree.traverseDFS(node => { console.log(node.data); }, 'postOrder'); // => dev1 dev2 dev3 cto accountant cfo cmo ceo
tree.remove('cmo');
tree.print(); // => ceo | cto cfo | dev1 dev2 dev3 accountant
tree.remove('cfo');
tree.print(); // => ceo | cto | dev1 dev2 dev3