# binary-trees C# .NET #2 program

## source code

```﻿// The Computer Language Benchmarks Game
// https://benchmarksgame-team.pages.debian.net/benchmarksgame/
//
// based Jarkko Miettinen Java #2 and Anthony Lloyd C#
// contributed by Isaac Gouy

using System;

class BinaryTrees
{
const int MinDepth = 4;

public static void Main(string[] args)
{
int maxDepth = args.Length == 0 ? 10
: Math.Max(MinDepth + 2, int.Parse(args[0]));

Console.WriteLine(string.Concat("stretch tree of depth ", maxDepth + 1,
"\t check: ", (TreeNode.bottomUpTree(maxDepth + 1)).itemCheck()));

var longLivedTree = TreeNode.bottomUpTree(maxDepth);

var results = new string[(maxDepth - MinDepth) / 2 + 1];

for (int i = 0; i < results.Length; i++)
{
int depth = i * 2 + MinDepth;
int n = (1 << maxDepth - depth + MinDepth) / NoTasks;
for (int t = 0; t < tasks.Length; t++)
{
{
var check = 0;
for (int i = n; i > 0; i--)
check += (TreeNode.bottomUpTree(depth)).itemCheck();
return check;
});
}
for (int t = 1; t < tasks.Length; t++)
results[i] = string.Concat(n * NoTasks, "\t trees of depth ",
depth, "\t check: ", check);
}
for (int i = 0; i < results.Length; i++)
Console.WriteLine(results[i]);

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

private class TreeNode
{

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

internal TreeNode(TreeNode left, TreeNode right)
{
this.left = left;
this.right = right;
}

internal int itemCheck()
{
if (left == null) return 1;
else return 1 + left.itemCheck() + right.itemCheck();
}
}
}
```

## notes, command-line, and program output

```NOTES:
.NET SDK 8.0.301
Host Version: 8.0.6
Commit: 3b8b000a0e

<OutputType>Exe
<TargetFramework>net8.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false
<OptimizationPreference>Speed
<IlcInstructionSet>native

Wed, 29 May 2024 21:13:09 GMT

MAKE:
cp binarytrees.csharpcore-2.csharpcore Program.cs
cp Include/csharpcore/program.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
~/dotnet/dotnet build -c Release --use-current-runtime
Determining projects to restore...
Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj (in 816 ms).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(66,37): warning CS8625: Cannot convert null literal to non-nullable reference type. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(66,42): warning CS8625: Cannot convert null literal to non-nullable reference type. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj]
program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/bin/Release/net8.0/linux-x64/program.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(66,37): warning CS8625: Cannot convert null literal to non-nullable reference type. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(66,42): warning CS8625: Cannot convert null literal to non-nullable reference type. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj]
2 Warning(s)
0 Error(s)

Time Elapsed 00:00:05.25

6.93s to complete and log all make actions

COMMAND LINE:
./bin/Release/net8.0/linux-x64/program 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
```