1 // Written by Andrey Khropov <andkhropov_nosp@m_mtu-net.ru>
2 // Found at http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=43991
3 // Modified by Leandro Lucarella
6 import tango.io.Stdout;
7 import tango.util.Convert;
11 int main(string[] args)
13 int N = args.length > 1 ? to!(int)(args[1]) : 1;
16 int maxDepth = (minDepth + 2) > N ? minDepth + 2 : N;
17 int stretchDepth = maxDepth + 1;
19 TreeNode stretchTree = TreeNode.BottomUpTree(0, stretchDepth);
20 //Stdout("stretch tree of depth ")(stretchDepth)("\t check: ")
21 // (stretchTree.ItemCheck);
23 TreeNode longLivedTree = TreeNode.BottomUpTree(0, maxDepth);
26 for(depth = minDepth; depth <= maxDepth; depth += 2)
28 int check, iterations = 1 << (maxDepth - depth + minDepth);
30 for(int i = 1; i <= iterations; i++)
32 auto tempTree = TreeNode.BottomUpTree(i, depth);
33 check += tempTree.ItemCheck;
36 tempTree = TreeNode.BottomUpTree(-i, depth);
37 check += tempTree.ItemCheck;
41 //Stdout(iterations * 2)("\t trees of depth ")(depth)
42 // ("\t check: ")(check);
45 //Stdout("long lived tree of depth ")(maxDepth)("\t check: ")
46 // (longLivedTree.ItemCheck);
54 this(int item, TreeNode left = null, TreeNode right = null)
61 static TreeNode BottomUpTree(int item, int depth)
64 return new TreeNode(item
65 ,BottomUpTree(2 * item - 1, depth - 1)
66 ,BottomUpTree(2 * item, depth - 1));
67 return new TreeNode(item);
73 return item + left.ItemCheck() - right.ItemCheck();