-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.js
170 lines (140 loc) · 4.22 KB
/
TreeNode.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
class TreeNode {
constructor() {
this.parent = null;
this.value = null;
this.children = [];
}
addChild(n) {
n.parent = this;
this.children.push(n);
return n;
}
depth_descend(cb, _stack = []) {
cb(this, _stack);
this.children.forEach(c => {
_stack.push(this);
c.depth_descend(cb, _stack);
_stack.pop();
});
}
breadth_descend(cb, _queue = []) {
//not ideal, needs work
var stack = [];
this.ascend((n, _stack) => stack = _stack);
stack = stack.reverse();
stack.pop();
cb(this, stack);
_queue = _queue.concat(this.children);
if (_queue.length)
this.breadth_descend.call(_queue.shift(), cb, _queue);
}
ascend(cb, _stack = []) {
_stack.push(this);
cb(this, _stack);
if (this.isRoot() === false)
this.ascend.call(this.parent, cb, _stack);
}
get firstChild() {
return this.children[0];
}
isRoot() {
return (this.parent === null);
}
get siblings() {
return (this.isRoot()) ? [] : this.parent.children;
}
hasChildren() {
return (this.children.length > 0);
}
toUL(iterator = this.depth_descend) {
var last_genn = 0;
var last_li;
var ul = [document.createElement("ul")];
iterator.call(this, (n, ancestry) => {
var genn = ancestry.length;
if (genn !== 0) {
var li = document.createElement("li");
li.textContent = n.value;
if (genn > last_genn) {
add_child();
} else if (genn < last_genn) {
add_ancestor();
} else {
add_sibling();
}
}
function add_child() {
add_sibling();
}
function add_ancestor() {
while (last_genn !== genn) {
ul.pop();
last_genn--;
}
add_sibling();
}
/*
node.hasChildren() is insufficient.
It makes the assumption that
normal traversal is occuring.
The safest and most accurate way to
determine if a node has children,
regardless of the iterator,
is to lookahead on the current node
or lookbehind on the next input node.
In short, the iterator determines if a
node has children, not the node's children[].
A lookahead is a hassle, lookbehind it is.
*/
function add_sibling() {
var par = ul[ul.length-1];
par.appendChild(li);
/*
The *right* question is:
Am I a first child, not do I have children.
*/
if (last_li && genn > last_genn) {
par = document.createElement("ul");
par.appendChild(li);
ul.push(par);
last_li.appendChild(par);
}
}
last_genn = genn;
last_li = li;
});
return ul[0];
}
}
function obj2tree(o) {
var parent = new TreeNode();
if (o instanceof Array) {
o.forEach(el => parent.addChild(obj2tree(el)));
} else if (o instanceof Object) {
for (var p in o) {
var v = o[p];
var c = parent.addChild(obj2tree(v));
c.value = p;
if (v instanceof Array === false &&
v instanceof Object === false)
c.value += `: ${v}`;
}
} else {
parent.value = o.toString();
}
return parent;
}
var obj = {
name: { first: "rafael", last: "cepeda" },
age: 23,
books: ["foo", "bar", "fooby"],
school: { name: "UWF", state: "FL" },
food: { name: "oranges", genre: "fruits" },
beverage: "water"
};
var node = obj2tree(obj);
var ul;
ul = node.toUL(node.depth_descend);
document.getElementById("depth").appendChild(ul);
ul = node.toUL(node.breadth_descend);
document.getElementById("breadth").appendChild(ul);