The Computer Language
Benchmarks Game

binary-trees C# .NET Core #2 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/ 

   contributed by Marek Safar 
   *reset* 
*/

using System;

class BinaryTrees
{
   const int minDepth = 4;

   public static void Main(String[] args)
   {
      int n = 0;
      if (args.Length > 0) n = Int32.Parse(args[0]);

      int maxDepth = Math.Max(minDepth + 2, n);
      int stretchDepth = maxDepth + 1;

      int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
      Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);

      TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);

      for (int depth=minDepth; depth<=maxDepth; depth+=2){
         int iterations = 1 << (maxDepth - depth + minDepth);

         check = 0;
         for (int i=1; i<=iterations; i++)
         {
            check += (TreeNode.bottomUpTree(depth)).itemCheck();
         }

         Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
            iterations, depth, check);
      }

      Console.WriteLine("long lived tree of depth {0}\t check: {1}",
         maxDepth, longLivedTree.itemCheck());
   }


   struct TreeNode
   {
      class Next
   	  {
	      public TreeNode left, right;
      }
   	
      private Next next;

      internal static TreeNode bottomUpTree(int depth){
         if (depth>0){
            return new TreeNode(
                 bottomUpTree(depth-1)
               , bottomUpTree(depth-1)
               );
         }
         else {
            return new TreeNode();
         }
      }

      TreeNode(TreeNode left, TreeNode right){
      	 this.next = new Next ();
         this.next.left = left;
         this.next.right = right;
      }

      internal int itemCheck(){
         // if necessary deallocate here
         if (next==null) return 1;
         else return 1 + next.left.itemCheck() + next.right.itemCheck();
      }
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
dotnet 1.0.1 005db40cd1
"System.GC.Server": true


Sun, 16 Apr 2017 15:21:55 GMT

MAKE:
cp binarytrees.csharpcore-2.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/bin/Release/netcoreapp1.1/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.86
7.60s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp1.1/tmp.dll 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303