-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.js
More file actions
33 lines (28 loc) · 780 Bytes
/
Copy pathTree.js
File metadata and controls
33 lines (28 loc) · 780 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
/*
Someone wrote a tree implementation, but they forgot to finish writing the definitions for addChild and contains.
Help this person finish their tree by adding in the missing code for these methods.
*/
var treeMaker = function(value) {
var newTree = Object.create(treeMaker.methods);
newTree.value = value;
newTree.children = [];
return newTree;
};
treeMaker.methods = {};
treeMaker.methods.addChild = function(value) {
var node = treeMaker(value);
this.children.push(node)
}
treeMaker.methods.contains = function(value) {
var result = false;
var search = function(value, node) {
if (node.value === value) {
result = true;
}
for (var x = 0; x < node.children.length; x++) {
search(value, node.children[x])
}
}
search(value, this)
return result;
};