namespace BinaryTree
{
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.WriteLine(Data+" ");
}
}
public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node Current = root;
Node Parent;
while (true)
{
Parent = Current;
if (i < Parent.Data)
{
Current = Current.Left;
if (Current == null)
{
Parent.Left = newNode;
break;
}
}
else
{
Current = Current.Right;
if (Current == null)
{
Parent.Right = newNode;
break;
}
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
Random rd = new Random();
for (int i = 0; i < 10; i++)
{
BST.Insert(rd.Next(30));
}
}
}
}
Monday, October 20, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment