回 帖 发 新 帖 刷新版面

主题:有谁能写出一个主程序吗?关于二叉树的.谢谢!!

我谁能帮我写一个关于二叉树的主程序吗?要求有选择功能!
非常感谢!!

回复列表 (共3个回复)

沙发


void main()
{ BT bt;
      int choice;
      bool t=true;

    while(t)
    {
        cout<<"1.生成二叉树\n";
        cout<<"2.先序遍历\n";
             cout<<"3.中序遍历\n";
        cout<<"4.后序遍历\n"; 
            cout<<"5.层次遍历\n";
        cout<<"6.计算二叉树结点数目 \n"; 
        cout<<"7.计算二叉树高度\n"; 
        cout<<"8.删除指定元素\n";
        cout<<"9.给出二叉树的前序和中序,输出它的后序\n";
        cout<<"请选择操作"<<endl;
        cin>>choice;
       switch(choice)
        {
        case 1:{bt.CreateBT(&bt.root);
               break;}
        case 2:{    
            if (bt.root ==NULL)
               cout<<"二叉树为空,请先创建/n";
          else
            bt.preorder(bt.root);
            break;
               }
        case 3:{
            if (bt.root ==NULL)
               cout<<"二叉树为空,请先创建/n";
          else
            bt.inorder(bt.root);
            break;
               }
        case 4:{
            if (bt.root ==NULL)
               cout<<"二叉树为空,请先创建/n";
          else
            bt.postorder(bt.root);
            break;
               }
        case 5:{bt.translever(bt.root);
            break;}
        case 6:{
            bt.jiediannumber(bt.root);
            cout<<"结点数为:"<<count<<endl;
                        count=0;
            break;}
        case 7:{cout<<"高度度为"<<bt.depth(bt.root)<<endl;
            break;}
        case 8:{cout<<"你创建的二叉树按照前序遍历为:\n";
               bt.preorder (bt.root );
                cout<<"\n";
            cout<<"请输入你要删除的节点的数值"<<endl;
                int d;
                cin>>d;
                bt.Delete(&bt.root,d);
                if(find==false) cout<<"没有你要删除的节点 ";
                else{  
                find=false;
                 cout<<"你删除节点后的二叉树按照前序遍历为:\n";
                 bt.preorder (bt.root );
                }
                break;}
        case 9:
            {cout<<"请输入二叉树的节点数,节点数大于零\n";
            int num;
            cin>>num;

                int *a=new int[num];
                int *b=new int[num];            
              cout<<"请输入二叉树的前序遍历:\n";
              for(int i=0;i<num;i++)
              cin>>a[i];
              cout<<"请输入二叉树的中序遍历:\n";
              for(int j=0;j<num;j++)
              cin>>b[j];
                BT *t=new BT;
                t->creatpost(&t->root,a,0,num-1,b,0,num-1);
                cout<<"这是你创建的二叉树的后序遍历:\n";
              t->postorder(t->root);
            }
       }
      int goon;      
cout<<"你还想试试其它操作吗?"<<"\n"<<"若是,则输入1,否则输入0"<<endl;
      cin>>goon;
      if(goon==0) t=false;
        }
}

板凳

非常谢谢你的回复!

    正和我意.
   
      谢谢!!

3 楼

不错,写得很好喔.......

我来回复

您尚未登录,请登录后再回复。点此登录或注册