2 <!-- MOZ_INSERT_CONTENT_HOOK -->
3 <script src = runner.js
></script>
5 var onlyName
= 'Binary Trees', onlyNum
= 2;
8 /* The Great Computer Language Shootout
9 http://shootout.alioth.debian.org/
10 contributed by Isaac Gouy */
12 function TreeNode(left
,right
,item
){
18 TreeNode
.prototype.itemCheck = function(){
19 if (this.left
==null) return this.item
;
20 else return this.item
+ this.left
.itemCheck() - this.right
.itemCheck();
23 function bottomUpTree(item
,depth
){
26 bottomUpTree(2*item
-1, depth
-1)
27 ,bottomUpTree(2*item
, depth
-1)
32 return new TreeNode(null,null,item
);
36 startTest("real-binary-trees");
40 for ( var n
= 2; n
<= 8; n
*= 2 ) (function(n
){
41 test( "Binary Trees", n
, function(){
43 var maxDepth
= Math
.max(minDepth
+ 2, n
);
44 var stretchDepth
= maxDepth
+ 1;
46 var check
= bottomUpTree(0,stretchDepth
).itemCheck();
48 var longLivedTree
= bottomUpTree(0,maxDepth
);
49 for (var depth
=minDepth
; depth
<=maxDepth
; depth
+=2){
50 var iterations
= 1 << (maxDepth
- depth
+ minDepth
);
53 for (var i
=1; i
<=iterations
; i
++){
54 check
+= bottomUpTree(i
,depth
).itemCheck();
55 check
+= bottomUpTree(-i
,depth
).itemCheck();
59 ret
= longLivedTree
.itemCheck();
66 <body onload=
"thisTest()"></body></html>