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
4 // (ported to Tango, fixed some stylistic issues)
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 int check = TreeNode.BottomUpTree(0, stretchDepth).ItemCheck;
19 TreeNode longLivedTree = TreeNode.BottomUpTree(0, maxDepth);
21 for (int depth = minDepth; depth <= maxDepth; depth += 2) {
22 int iterations = 1 << (maxDepth - depth + minDepth);
25 for (int i = 1; i <= iterations; i++) {
26 check += TreeNode.BottomUpTree(i, depth).ItemCheck;
27 check += TreeNode.BottomUpTree(-i, depth).ItemCheck;
40 this(int item, TreeNode left = null, TreeNode right = null)
47 static TreeNode BottomUpTree(int item, int depth)
50 return new TreeNode(item,
51 BottomUpTree(2 * item - 1, depth - 1),
52 BottomUpTree(2 * item, depth - 1));
53 return new TreeNode(item);
59 return item + left.ItemCheck() - right.ItemCheck();