view binary_trees.rhope @ 155:d59611dcec71

Small fix to binary trees benchmark
author Mike Pavone <pavone@retrodev.com>
date Tue, 21 Dec 2010 04:12:11 +0000
parents f4fd8962c385
children
line wrap: on
line source



Blueprint Tree
{
	Left
	Right
	Item
}

Blueprint Empty
{

}

Make Tree[item,depth:out]
{
	If[depth]
	{
		dbl <- [item]+[item]
		ndepth <- [depth]-[1]
		out <- [[[Build[Tree()]]Left <<[ Make Tree[[dbl]-[1], ndepth] ]]Right <<[ Make Tree[dbl, ndepth] ]]Item <<[item]
	}{
		out <- [[[Build[Tree()]]Left <<[Build[Empty()]]]Right <<[Build[Empty()]]]Item <<[item]
	}
}

Check Tree@Tree[node:out]
{
	out <- [[[node]Item >>]+[Check Tree[[node]Left >>]]] - [Check Tree[[node]Right >>]]
}

Check Tree@Empty[node:out]
{
	out <- 0
}

Step[num, depth: out]
{
	out <- [Check Tree[Make Tree[num, depth]]] + [Check Tree[Make Tree[[0]-[num], depth]]]
}

Test Size[sum, current,iterations,depth:out]
{
	nsum <- [sum]+[Step[current, depth]]
	If[[current]=[iterations]]
	{
		out <- Val[nsum]
	}{
		out <- Test Size[nsum, [current]+[1], iterations, depth]
	}
}

Test Sizes[current,max,iterations:out]
{
	sum <- Test Size[0, 1, iterations, current]	
	{	
		Print[
			[[[[String[[iterations]*[2]]
			]Append["\t trees of depth "]
			]Append[String[current]]
			]Append["\t check: "]
			]Append[String[sum]]]
		{
			If[[[current]+[2]]>[max]]
			{
				out <- 1
			}{
				out <- Test Sizes[[current]+[2], max, [iterations]/[4]]
			}
		}
	}
}

Main[args]
{
	[args]Index[1]
	{
		min depth <- 4
		max depth <- Max[[min depth]+[2], Int32[~]]
		Print[
			[[["stretch tree of depth "
			]Append[String[[max depth]+[1]]]
			]Append["\t check: "]
			]Append[String[Check Tree[Make Tree[0, [max depth]+[1]]]]]]
		{ long lived <- Make Tree[0, max depth]
		{ Test Sizes[min depth, max depth, [1]LShift[max depth]]
		{ Print[
			[[["long lived tree of depth "
			]Append[String[max depth]]
			]Append["\t check: "]
			]Append[String[Check Tree[long lived]]]] }}}
	}{
		Print["you must provide a max depth param"]
	}
}