CS计算机代考程序代写 using System;

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 Left { get; set; } // Left subtree
public Node Right { get; set; } // Right subtree

// Node constructor
public Node(T item, Node L, Node R)
{
Item = item;
Left = L;
Right = R;
}
}

public class RandomBinaryTree
{
private Node root; // Reference to the root of the binary tree
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 RandomBuild(int n)
{
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(default(T), RandomBuild(left), RandomBuild(n – left – 1));
}
}

// 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 root, int indent)
{
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 = new RandomBinaryTree(n);
BT.Print();
n = Convert.ToInt32(Console.ReadLine());
}
}
}
}