The Computer Language
Benchmarks Game

binary-trees Smalltalk VW program

source code

"* The Computer Language Shootout
    http://shootout.alioth.debian.org/
    contributed by Isaac Gouy
    modified by Eliot Miranda 
    *reset*
*"!


Object subclass: #TreeNode
   instanceVariableNames: 'left right'
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Shootout'!

!Tests class methodsFor: 'benchmarking'!
binarytrees: n to: output
   | minDepth maxDepth stretchDepth check longLivedTree iterations |
   minDepth := 4.
   maxDepth := minDepth + 2 max: n.
   stretchDepth := maxDepth + 1.

   check := (TreeNode bottomUpTree: stretchDepth) itemCheck.
   output
      nextPutAll: 'stretch tree of depth '; print: stretchDepth; tab;
      nextPutAll: ' check: '; print: check; nl.

   longLivedTree := TreeNode bottomUpTree: maxDepth.
   minDepth to: maxDepth by: 2 do: [:depth|
      iterations := 1 bitShift: maxDepth - depth + minDepth.

      check := 0.
      1 to: iterations do: [:i|
         check := check + (TreeNode bottomUpTree: depth) itemCheck
         ].
      output
         print: iterations; tab;
         nextPutAll: ' trees of depth '; print: depth; tab;
         nextPutAll: ' check: '; print: check; nl
      ].

   output
      nextPutAll: 'long lived tree of depth '; print: maxDepth; tab;
      nextPutAll: ' check: '; print: longLivedTree itemCheck; nl! !

!Tests class methodsFor: 'benchmark scripts'!
binarytrees
   self binarytrees: self arg to: self stdout.
   ^''! !


!TreeNode methodsFor: 'initialize-release'!
left: leftChild right: rightChild
   left := leftChild.
   right := rightChild! !

!TreeNode methodsFor: 'accessing'!
itemCheck
   ^left isNil 
      ifTrue: [1] ifFalse: [1 + left itemCheck + right itemCheck]! !


!TreeNode class methodsFor: 'instance creation'!
bottomUpTree: anInteger
   ^(anInteger > 0) 
      ifTrue: [
         self 
            left: (self bottomUpTree: anInteger - 1) 
            right: (self bottomUpTree: anInteger - 1)  
         ]
      ifFalse: [self left: nil right: nil]! !

!TreeNode class methodsFor: 'instance creation'!
left: leftChild right: rightChild      
   ^(super new) left: leftChild right: rightChild! !

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
VisualWorks® Personal Use Edition Release 8.1.1 of March 10, 2016


Sun, 26 Mar 2017 17:54:46 GMT

COMMAND LINE:
/usr/local/src/vw8.1.1pul/bin/linuxx86_64/vwlinuxx86_64 /usr/local/src/vw8.1.1pul/image/benchmarks.im -nogui -evaluate "Tests binarytrees" -a 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303