1 /* The Great Computer Language Shootout
2 http://shootout.alioth.debian.org/
3 contributed by Isaac Gouy */
5 function TreeNode(left,right,item){
11 TreeNode.prototype.itemCheck = function(){
12 if (this.left==null) return this.item;
13 else return this.item + this.left.itemCheck() - this.right.itemCheck();
16 function bottomUpTree(item,depth){
19 bottomUpTree(2*item-1, depth-1)
20 ,bottomUpTree(2*item, depth-1)
25 return new TreeNode(null,null,item);
31 for ( var n = 4; n <= 7; n += 1 ) {
33 var maxDepth = Math.max(minDepth + 2, n);
34 var stretchDepth = maxDepth + 1;
36 var check = bottomUpTree(0,stretchDepth).itemCheck();
38 var longLivedTree = bottomUpTree(0,maxDepth);
39 for (var depth=minDepth; depth<=maxDepth; depth+=2){
40 var iterations = 1 << (maxDepth - depth + minDepth);
43 for (var i=1; i<=iterations; i++){
44 check += bottomUpTree(i,depth).itemCheck();
45 check += bottomUpTree(-i,depth).itemCheck();
49 ret = longLivedTree.itemCheck();