C# OOP: Binary Search Tree

BS Tree menu of choices


Step 7: Display BS Tree operations menu

We define a showmenu() function to display a list of choices. We also need another function called select() that allows the user to choose each of the options and decides whether he/he wants to exit the program.

public static void showmenu(){

Console.WriteLine("=================================");
Console.WriteLine("Binary Tree Operations Menu");
Console.WriteLine("=================================");
Console.WriteLine("1.Add a new item");
Console.WriteLine("2.Delete an item");
Console.WriteLine("3.Show min node");
Console.WriteLine("4.Show max node");
Console.WriteLine("5.Find a node");
Console.WriteLine("6.Print all nodes");

Console.WriteLine("7.Exit");

}

public static void select(){

int val, ch,yes=0;
BTree mylist=new BTree();
TreeNode temp=null;
showmenu();

try{
while(yes==0){
Console.Write("Enter your choice:");
ch=int.Parse(Console.ReadLine()) ;
switch(ch){

case 1:
Console.Write("Value:");
val = int.Parse(Console.ReadLine());
mylist.Root=mylist.insert(mylist.Root,val);

break;

case 2:
Console.Write("Value to be deleted:");
val = int.Parse(Console.ReadLine());
mylist.delete(mylist.Root,val);
break;
case 3:
temp=mylist.findmin(mylist.Root);
Console.WriteLine("The min node is:"+temp.data);
break;

case 4:
temp=mylist.findmax(mylist.Root);
Console.WriteLine("The max node is:"+temp.data);
break;

case 5:
Console.Write("Value:");
val = int.Parse(Console.ReadLine());
temp=mylist.find(mylist.Root,val);
if(temp!=null)
Console.WriteLine("The found node is:"+temp.data);
else Console.WriteLine("Not found");
break;
case 6:
Console.WriteLine("All items:");
mylist.printall(mylist.Root);
break;

case 7: Environment.Exit(0); break;

default: Console.WriteLine("Invalid choice!"); break;

}


Console.Write("\nContinue? Press 0 to continue:");
yes = int.Parse(Console.ReadLine());

}

}catch(Exception e){}

}



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.