forked from ssitu001/toyProblemPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeDFSelect.js
123 lines (110 loc) · 2.82 KB
/
treeDFSelect.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
/**
*
* Implement a `DFSelect` method on this Tree class.
*
* DFSelect accepts a filter function, calls that function on each of the nodes
* in Depth First order, and returns a flat array of node values of the tree
* for which the filter returns true.
*
* Example:
* var root1 = new Tree(1);
* var branch2 = root1.addChild(2);
* var branch3 = root1.addChild(3);
* var leaf4 = branch2.addChild(4);
* var leaf5 = branch2.addChild(5);
* var leaf6 = branch3.addChild(6);
* var leaf7 = branch3.addChild(7);
* root1.DFSelect(function (value, depth) {
* return value % 2;
* })
* // [1, 5, 3, 7]
*
* root1.DFSelect(function (value, depth) {
* return depth === 1;
* })
* // [2, 3]
*
*/
/*
* Basic tree that stores a value.
*/
var Tree = function(value){
this.value = value;
this.children = [];
};
Tree.prototype.DFSelect = function(filter) {
var results = [];
function depthFirst(nodes, depth) {
if(filter(nodes.value, depth)) {
results.push(nodes.value);
}
if(nodes.children) {
for(var i = 0; i < nodes.children.length; i++) {
depthFirst(nodes.children[i], depth+1);
}
}
}
depthFirst(this, 0);
return results;
};
//
/**
* You shouldn't need to change anything below here, but feel free to look.
*/
/**
* add an immediate child
* (wrap values in Tree nodes if they're not already)
*/
Tree.prototype.addChild = function(child){
if (!child || !(child instanceof Tree)){
child = new Tree(child);
}
if(!this.isDescendant(child)){
this.children.push(child);
}else {
throw new Error("That child is already a child of this tree");
}
// return the new child node for convenience
return child;
};
/**
* check to see if the provided tree is already a child of this
* tree __or any of its sub trees__
*/
Tree.prototype.isDescendant = function(child){
if(this.children.indexOf(child) !== -1){
// `child` is an immediate child of this tree
return true;
}else{
for(var i = 0; i < this.children.length; i++){
if(this.children[i].isDescendant(child)){
// `child` is descendant of this tree
return true;
}
}
return false;
}
};
/**
* remove an immediate child
*/
Tree.prototype.removeChild = function(child){
var index = this.children.indexOf(child);
if(index !== -1){
// remove the child
this.children.splice(index,1);
}else{
throw new Error("That node is not an immediate child of this tree");
}
};
var root1 = new Tree(1);
var branch2 = root1.addChild(2);
var branch3 = root1.addChild(3);
var leaf4 = branch2.addChild(4);
var leaf5 = branch2.addChild(5);
var leaf6 = branch3.addChild(6);
var leaf7 = branch3.addChild(7);
console.log(root1.DFSelect(function (value, depth) {
return value % 2;
}));
// [1, 5, 3, 7]