freemandj 2021-03-21 16:33 采纳率: 100%
浏览 24
已结题

关于二叉排序树,没有错误却输不出任何结果,C++,求帮忙改改可以输出结果

     #include<iostream>
    #include<string>
    #include<vector>

    #include<algorithm>
    using namespace std;

    class TreeNode
    {
    public:
        int data;
        TreeNode *left, *right;
        TreeNode() {
        left = right = NULL;
        data=0;
         }
    };
    class BSTree
    {
    public:
        TreeNode *root;

        BSTree() { root = NULL; }
        void AddintoTree(int num)
        {
            TreeNode *t = root;
            while (true)
            {
                if (num < t->data)
                {
                    if (t->left == NULL)
                    {
                        t->left = new TreeNode;
                        t->left->data = num;
                        return;
                    }
                    else
                    {
                        t = t->left;
                    }
                }
                else
                {
                    if (t->right == NULL)
                    {
                        t->right = new TreeNode;
                        t->right->data = num;
                        return;
                    }
                    else
                    {
                        t = t->right;
                    }
                }
            }
        }
        void Preorder(TreeNode *root, vector<int> &ans) //递归前序
    {
        if (root == NULL)
            return;

        ans.push_back(root->data);
        Preorder(root->left, ans);
        Preorder(root->right, ans);
    }
    void Inorder(TreeNode *root, vector<int> &ans) //递归中序
    {
        if (root == NULL)
            return;

        Inorder(root->left, ans);
        ans.push_back(root->data);
        Inorder(root->right, ans);
    }

    void Postorder(TreeNode *root, vector<int> &ans) //递归后序
    {
        if (root == NULL)
            return;

        Postorder(root->left, ans);
        Postorder(root->right, ans);
        ans.push_back(root->data);
    }
    TreeNode *search(int value){
        TreeNode *p=root;
        while(p!=NULL||p->data==value){
        if(value<p->data){
            p=p->left;
        }    
        else{
            p=p->right;
        }    
        }
        return p;
    }
    TreeNode *findmax(){
        TreeNode *p=root;
        while(p!=NULL){
            p=p->left;
        }
        return p;
    }

    };
    int main(){
        BSTree bstree;
        bstree.AddintoTree(1);
         bstree.AddintoTree(2);
              bstree.AddintoTree(9);
                   bstree.AddintoTree(7);
                        bstree.AddintoTree(8);
                             bstree.AddintoTree(4);
                       
        vector<int> ans;
        bstree.Inorder(bstree.root,ans);
        vector<int>:: iterator it;
        for(it=ans.begin();it!=ans.end();it++){
            cout<<*it;
        }
        TreeNode *treenode;
        treenode=bstree.findmax();
        cout<<treenode->data;


        

    • 写回答

    1条回答 默认 最新

    • 幻灰龙 2021-03-22 09:03
      关注

      你可以增加一个打印二叉树的函数 Dump

      然后每个函数单独测试,例如调用  bstree.AddintoTree(1); 后调用 bstree.Dump(),其他代码先注释掉。

      这样逐个测试,看看哪里开始不对。

      本回答被题主选为最佳回答 , 对您是否有帮助呢?
      评论

    报告相同问题?

    问题事件

    • 系统已结题 9月14日
    • 已采纳回答 9月6日

    悬赏问题

    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应