-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlFormula.js
More file actions
78 lines (78 loc) · 1.65 KB
/
DlFormula.js
File metadata and controls
78 lines (78 loc) · 1.65 KB
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
class DlFormula {
constructor(value = null, children = []) {
this._value = value;
this._children = children;
}
addChild(child) {
this._children.push(child);
}
getValue() {
return this._value;
}
setValue(value) {
this._value = value;
}
getChildren() {
return this._children;
}
setChildren(children) {
this._children = children;
}
size() {
let size = 1;
const recurse = (node) => {
if (node._children.length > 0) {
const numChildren = node._children.length;
size += numChildren;
for (let i = 0; i < numChildren; i++)
recurse(node._children[i]);
}
};
recurse(this);
return size;
}
height() {
let height = 0;
const recurse = (node, depth) => {
if (node._children.length === 0) {
if (depth > height)
height = depth;
} else
for (let i = 0; i < node._children.length; i++)
recurse(node._children[i], depth + 1);
};
recurse(this, 0);
return height;
}
leafCount() {
let leafCount = 0;
const recurse = (node) => {
if (node._children.length === 0)
leafCount++;
else
for (let i = 0; i < node._children.length; i++)
recurse(node._children[i]);
};
recurse(this);
return leafCount;
}
toString() {
const recurse = (node, indent = 0) => {
let str = '';
for (let i = 0; i < indent; i++)
str += '\t';
if (node._children.length === 0)
str += node._value.toString() + '\n';
else {
str += node._value.toString() + ' -> [\n';
for (let i = 0; i < node._children.length; i++)
str += recurse(node._children[i], indent + 1);
for (let i = 0; i < indent; i++)
str += '\t';
str += ']\n';
}
return str;
};
return recurse(this);
}
}