1 USING: kernel math accessors prettyprint io locals sequences
2 math.ranges math.order ;
3 IN: benchmark.binary-trees
5 TUPLE: tree-node item left right ;
7 C: <tree-node> tree-node
9 : bottom-up-tree ( item depth -- tree )
13 [ [ 2 * 1 - ] dip bottom-up-tree ]
14 [ [ 2 * ] dip bottom-up-tree ] 2tri
17 ] if <tree-node> ; inline recursive
19 GENERIC: item-check ( node -- n )
21 M: tree-node item-check
22 [ item>> ] [ left>> ] [ right>> ] tri [ item-check ] bi@ - + ;
24 M: f item-check drop 0 ;
26 : min-depth 4 ; inline
28 : stretch-tree ( max-depth -- )
29 1 + 0 over bottom-up-tree item-check
30 [ "stretch tree of depth " write pprint ]
31 [ "\t check: " write . ] bi* ; inline
33 :: long-lived-tree ( max-depth -- )
34 0 max-depth bottom-up-tree
36 min-depth max-depth 2 <range> [| depth |
37 max-depth depth - min-depth + 2^ [
40 [ depth bottom-up-tree item-check + ] bi@
44 pprint "\t trees of depth " write depth pprint
48 "long lived tree of depth " write max-depth pprint
49 "\t check: " write item-check . ; inline
51 : binary-trees ( n -- )
52 min-depth 2 + max [ stretch-tree ] [ long-lived-tree ] bi ; inline
54 : binary-trees-main ( -- )
57 MAIN: binary-trees-main