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.util.Convert;
10 int main(string[] args)
12 int N = args.length > 1 ? to!(int)(args[1]) : 1;
15 int maxDepth = (minDepth + 2) > N ? minDepth + 2 : N;
16 int stretchDepth = maxDepth + 1;
18 TreeNode stretchTree = TreeNode.BottomUpTree(0, stretchDepth);
20 TreeNode longLivedTree = TreeNode.BottomUpTree(0, maxDepth);
23 for(depth = minDepth; depth <= maxDepth; depth += 2)
25 int check, iterations = 1 << (maxDepth - depth + minDepth);
27 for(int i = 1; i <= iterations; i++)
29 auto tempTree = TreeNode.BottomUpTree(i, depth);
30 check += tempTree.ItemCheck;
33 tempTree = TreeNode.BottomUpTree(-i, depth);
34 check += tempTree.ItemCheck;
46 this(int item, TreeNode left = null, TreeNode right = null)
53 static TreeNode BottomUpTree(int item, int depth)
56 return new TreeNode(item
57 ,BottomUpTree(2 * item - 1, depth - 1)
58 ,BottomUpTree(2 * item, depth - 1));
59 return new TreeNode(item);
65 return item + left.ItemCheck() - right.ItemCheck();