C ++二进制搜索树切换语句问题

时间:2015-03-27 16:37:14

标签: c++ menu switch-statement binary-search-tree

您好我试图实现二进制搜索树但是我遇到了switch语句的问题。值-1应该用于访问菜单并且它可以工作但是同时它输入到树的顶部。一旦我进入菜单,我输入的任何数字也会在树中结束,下面是代码,它没有给我任何错误,但我无法理解开关案例声明,任何帮助都将受到赞赏。< / p>

&#13;
&#13;
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <stdio.h>

using namespace std;
struct node
{
   int data;
   node *left;
   node *right;
};


void insert(node **tree, int value)
{
   if (*tree == nullptr)
   {
      *tree = new node;
      
      (*tree)->data = value;
      (*tree)->left = nullptr;
      (*tree)->right = nullptr;
   }
   else
   {
      if(value < (*tree)->data)
      {
         // Adding & because (*tree) is a type: node*
         // & references it in the form of a node**
         // Which is what the function expects
         insert( &((*tree)->left) , value);/**/
      }
      else if(value > (*tree)->data)
      {
         insert( &((*tree)->right) , value);
      } 
      else
      {
         return;
      }
   }
}



void inorder(node *tree)
{
   if (tree != nullptr)
   {
      inorder(tree->left);
      cout << tree->data << ", ";
      inorder(tree->right);
   }
}

void preOrder(node *tree)
{
   if (tree != nullptr)
   {
      cout << tree->data << ", ";
      preOrder(tree->left);
      preOrder(tree->right);
   }
}

void postOrder(node *tree)
{
   if (tree != nullptr)
   {
      postOrder(tree->left);
      postOrder(tree->right);
      cout << tree->data << ", ";
   }
}

void delete_tree(node *tree)
{
   if(tree != NULL)
  {
    delete_tree(tree->left);
    delete_tree(tree->right);
    delete tree;
    cout << "value deleted: " << tree->data << endl;
  }   
}

int main(int argc, char **argv)
{
   node *tree = nullptr;


   while (true)
   {
      int num;
     
      cout << "\n\nEnter a value into the tree(type -1 for options)\n";
      cin >> num;
      insert(&tree, num);

      while(num == -1)
      {
         cout<<endl<<endl;
         cout<<" Binary Search Tree Operations "<<endl;
         cout<<" ----------------------------- "<<endl;
         cout<<" 0. Print in order "<<endl;
         cout<<" 1. Pre-Order Traversal "<<endl;
         cout<<" 2. Post-Order Traversal "<<endl;
         cout<<" 3. Insert number "<<endl;
         cout<<" 4. Delete tree "<<endl;
         cout<<" Enter your choice : ";
         cin>>num;
      }
      switch(num)
      {
         case 0 : 
            cout<<endl;
            cout<<" Printing in Order "<<endl;
            cout<<" -------------------"<<endl;
            inorder(tree);
         break;
         
         case 1 : 
            cout<<" Pre-Order Traversal "<<endl;
            cout<<" -------------------"<<endl;
            preOrder(tree);
         break;
         
         case 2 : 
            cout<<" Post-Order Traversal "<<endl;
            cout<<" -------------------"<<endl;
            postOrder(tree);
         break;

         case 3 : 
            cout<<" Insert number into the tree "<<endl;
            cout<<" -------------------"<<endl;
            insert(&tree, num);
         break;

         case 4 : 
            cout<<" delete the tree "<<endl;
            cout<<" -------------------"<<endl;
            delete_tree(tree);
         break;
      }
   }
   return 0;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

之后你有insert(&tree, num);
cout << "\n\nEnter a value into the tree(type -1 for options)\n";
cin >> num;

所以即使它是-1,你总是会添加一个数字。在调用insert之前,需要检查-1。