using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RandomlyBuiltBinaryTree
{
public class Node
{
public T Item { get; set; } // Item stored in the Node
public Node
public Node
// Node constructor
public Node(T item, Node
{
Item = item;
Left = L;
Right = R;
}
}
public class RandomBinaryTree
{
private Node
private Random r; // For building the random binary tree
// RandomBinaryTree constructor
// Builds a random binary tree on n nodes
public RandomBinaryTree(int n)
{
r = new Random(); // Creates a random variable r
root = RandomBuild(n);
}
// RandomBuild (in a preorder fashion)
// Creates and returns a randomly built binary tree on n nodes
// Devroye and Kruszewski, Botanical beauty of random binary trees, Springer, 1996
public Node
{
int left;
if (n == 0)
return null;
else
{
// Randomly determine number of nodes in the left subtree
left = (int)(n * r.NextDouble());
// Recursively build tree
return new Node
}
}
// Public Print (Inorder)
// Outputs the binary tree in a 2-D format without edges and rotated 90 degrees
public void Print()
{
Print(root, 0);
}
// Private Print
// Recursively implements the public Print
private void Print(Node
{
if (root != null)
{
Print(root.Right, indent + 3);
Console.WriteLine(new String(‘ ‘, indent) + “*”);
Print(root.Left, indent + 3);
}
}
}
class Program
{
static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
while (n != 0 )
{
RandomBinaryTree
BT.Print();
n = Convert.ToInt32(Console.ReadLine());
}
}
}
}