C++ exercises and solutions: Tree menu

Binary Search Tree menu


Step 7: Display Binary Search 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.

void showmenu(){

  cout<<"=================================\n";
  cout<<"Binary Search Tree Operations Menu\n";
  cout<<"=================================\n";
  cout<<"1.Add a new node\n";
  cout<<"2.Delete an node\n";
  cout<<"3.Print all nodes\n";
  cout<<"4.Find min node\n";
  cout<<"5.Show max node\n";
  cout<<"6.Find a node\n";
  cout<<"7.Exit\n";


}

void select(){
  int val, ch;
  char yes='y';
  BSTree<int> *myTree=new BSTree<int>();
  while(yes=='y'){
    cout<<"Enter your choice:";cin>>ch;
    switch(ch){
      case 1:
         cout<<"Value:";cin>>val;
         myTree->Temp=myTree->Insert(&myTree->Root,val);
         break;

     case 2:
        cout<<"Value to delete:";cin>>val;
        myTree->Temp=myTree->Delete(&myTree->Root,val);
        break;

     case 3:
       cout<<"All items:\n";
       myTree->Printtree(myTree->Root);
       break;

   case 4:
       myTree->Temp=myTree->findmin(myTree->Root);
       if(myTree->Temp!=NULL) cout<<myTree->Temp->data<<endl;
       break;

   case 5:
      myTree->Temp=myTree->findmax(myTree->Root);
      if(myTree->Temp!=NULL) cout<<myTree->Temp->data<<endl;
     break;

   case 6:
      cout<<"Find what:";cin>>val;
      myTree->Temp=myTree->find(myTree->Root,val);
      if(myTree->Temp!=NULL) cout<<myTree->Temp->data<<endl;
      else cout<<"Not found!"<<endl;
      break;

   case 7: exit(0);

   default: cout<<"Invalid choice";

   }
    cout<<"Continue?y/n:";cin>>yes;
 }

  myTree->Temp=NULL;
}


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.