0% found this document useful (0 votes)
3 views

Example of Tree

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Example of Tree

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Example of Tree

Adding & Printing


namespace ConsoleApp19
{
internal class Program
{
public class Node_tree
{
public int X;
public Node_tree LP, RP;
public Node_tree(int data)
{
X=data;
LP = RP = null;
}
}
public static Node_tree Insertinto(Node_tree Tr, int value)
{
if (Tr == null)
{
Tr = new Node_tree(value);
return Tr;
}
if (value <= Tr.X)
Tr.LP= Insertinto(Tr.LP, value);
else if(value>Tr.X)
Tr.RP= Insertinto(Tr.RP, value);
return Tr;
}
public static void PrintData(Node_tree Tr)
{
if(Tr!=null)
{
PrintData(Tr.LP);
Console.Write(Tr.X + " ");
PrintData(Tr.RP);
}
}
static void Main(string[] args)
{
Node_tree Root=null;
int V;
char C;
do
{
Console.WriteLine("Press + to add new element");
Console.WriteLine("Press % to print all elements");
Console.WriteLine("Press x to end");
C = Convert.ToChar(Console.ReadLine().ToLower());
switch (C)
{
case '+':
Console.Write("Please enter value:");
V = Convert.ToInt32(Console.ReadLine());
Root =Insertinto(Root,V);
break;
case '%':
PrintData(Root);
break;
default:
Console.WriteLine("Wrong Entry!");
break;
}
Console.Write("Press any key to continue...");
Console.ReadKey();
Console.WriteLine();
} while (C != 'x');
}
}
}

You might also like