#MonthOfCode - Day 19: tree

My entry for the 19th day of the month of code. The theme for today is: tree.

Here’s a stupid tree implementation where the nodes are not stored explicitly but are instead captured by closures. Only leaves have a value.

Code after the break.

tree.jsview raw
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
(function () {

function tree() {
var children = arguments;
return function (i) {
return children[i];
};
}

var root = tree(
tree(
1,
2
),
3,
tree(
tree(
tree(
4,
5,
6
),
7
),
tree(
8,
9
)
)
);

console.log(root(2)(0)(0)(1)); // 5

})();

Stupid, isn’t it?