Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 仍然可以';在二叉树中找不到最大数_C++_Binary Tree - Fatal编程技术网

C++ 仍然可以';在二叉树中找不到最大数

C++ 仍然可以';在二叉树中找不到最大数,c++,binary-tree,C++,Binary Tree,我仍然无法在我的树中找到最大的数字,但现在我真的没有让它复杂化,我认为它应该可以工作,没有错误,但它只是不会在控制台中显示。有人有主意吗?我真的很沮丧 这是我的全部代码: #include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class BinaryTree { struct Node {

我仍然无法在我的树中找到最大的数字,但现在我真的没有让它复杂化,我认为它应该可以工作,没有错误,但它只是不会在控制台中显示。有人有主意吗?我真的很沮丧

这是我的全部代码:

#include <iostream>
#include <string>
#include <cstdlib> 


using namespace std;


template<class T>
class BinaryTree
{

struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = NULL;
            rChildptr = NULL;

        }
    };
private:
    Node* root; 

        void Insert(T newData, Node* &theRoot)
        {
            if(theRoot == NULL) 
            {
                theRoot = new Node(newData);
                return;
            }

            if(newData < theRoot->data)  
                Insert(newData, theRoot->lChildptr);
            else
                Insert(newData, theRoot->rChildptr);;
        }

        void PrintTree(Node* theRoot)
        {
            if(theRoot != NULL)
            {
                PrintTree(theRoot->lChildptr);
                cout<< theRoot->data<<" \n";;
                PrintTree(theRoot->rChildptr);
            }
        }
T Largest( Node* theRoot)
{
    if ( root == NULL ){
    cout<<"There is no tree";
    return -1;
    }

    if (theRoot->rChildptr != NULL)
        return Largest(theRoot->rChildptr);
    else
        return theRoot->data;
        cout<<theRoot->data;


}; 

    public:
        BinaryTree()
        {
            root = NULL;
        }

        void AddItem(T newData)
        {
            Insert(newData, root);
        }

        void PrintTree()
        {
            PrintTree(root);
        }
        T Largest()
        {
            return Largest(root);
        }
    };

    int main()
    {

        BinaryTree<int> *myBT = new BinaryTree<int>();
        myBT->AddItem(2);
        myBT->AddItem(5);
        myBT->AddItem(1);
        myBT->AddItem(10);
        myBT->AddItem(15);
        myBT->PrintTree();
        myBT->Largest();

    } 
#包括
#包括
#包括
使用名称空间std;
样板
类二叉树
{
结构体类型
{
T数据;
节点*lChildptr;
节点*rChildptr;
节点(T dataNew)
{
数据=新数据;
lChildptr=NULL;
rChildptr=NULL;
}
};
私人:
节点*根;
无效插入(T新数据、节点*&根)
{
if(theRoot==NULL)
{
theRoot=新节点(新数据);
回来
}
如果(新数据数据)
插入(新数据,根->lChildptr);
其他的
插入(newData,theRoot->rChildptr);;
}
无效打印树(节点*根)
{
if(theRoot!=NULL)
{
打印树(theRoot->lChildptr);
coutdatadata;
库塔迪坦(2);
myBT->AddItem(5);
myBT->AddItem(1);
myBT->AddItem(10);
myBT->AddItem(15);
myBT->printree();
myBT->max();
} 
这里是应该找到最大数字的部分(最下面的孩子):

T最大(节点*根)
{
if(root==NULL){
coutrChildptr);
其他的
返回根->数据;

cout在
max()中,您的代码中有两个问题:

  • 看起来您想在else子句中执行两条语句,但没有使用大括号

  • 您想在返回后执行cout打印,这是不可能的。请切换顺序

因此,您应该将代码片段替换为:

    else {
        cout << theRoot->data;
        return theRoot->data;
    }
else{
cout数据;
返回根->数据;
}

顺便说一句,不要让双分号(
)留在代码中。在大多数情况下,它是无害的,但它总是不好的样式。

这里有一个固定的、稍微清理过的版本,可以工作:

#include <iostream>
#include <string>
#include <cstdlib> 

using namespace std;

template<class T>
class BinaryTree
{
    struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = 0;
            rChildptr = 0;
        }
    };

private:

    Node* root;

    void Insert(T newData, Node** theRoot)
    {
        if (*theRoot == 0)
        {
            *theRoot = new Node(newData);
        }
        else if (newData < (*theRoot)->data)
            Insert(newData, &((*theRoot)->lChildptr));
        else
            Insert(newData, &((*theRoot)->rChildptr));
    }

    void PrintTree(Node* theRoot)
    {
        if (theRoot != 0)
        {
            PrintTree(theRoot->lChildptr);
            cout << theRoot->data << " " << endl;
            PrintTree(theRoot->rChildptr);
        }
    }

    T Largest(Node* theRoot)
    {
        if (root == 0)
        {
          throw "There is no tree";
        }

        if (theRoot->rChildptr != 0)
            return Largest(theRoot->rChildptr);
        else
            return theRoot->data;
    }

public:

    BinaryTree()
    {
        root = 0;
    }

    void AddItem(T newData)
    {
        Insert(newData, &root);
    }

    void PrintTree()
    {
        PrintTree(root);
    }

    T Largest()
    {
        return Largest(root);
    }
};

int main()
{
    BinaryTree<int> *myBT = new BinaryTree<int>();

    cout << "The tree is empty. Trying to find the largest element." << endl;

    try
    {
      cout << "Largest element: " << myBT->Largest() << endl;
    }
    catch (const char* e)
    {
      cout << "Exception caught: " << e << endl;
    }

    myBT->AddItem(15);
    myBT->AddItem(2);
    myBT->AddItem(5);
    myBT->AddItem(1);
    myBT->AddItem(10);

    cout << "Tree:" << endl;
    myBT->PrintTree();

    cout << "Largest element: " << myBT->Largest() << endl;
} 

你在调试器中一步一步地检查代码了吗?不确定,如何使用调试器,我应该在哪里设置断点?你有什么问题?有什么错误?没什么,现在很好,答案在下面。谢谢大家。我现在有大括号,但仍然没有,删除了双分号。我只是回来说我终于找到了pr问题是我只是想切换cout和return,然后我看到你写了它,想了很多,你能解释一下为什么在切换后现在可以这样做吗,我不确定….:因为return的意思是“立即退出这个函数,并将值返回给调用者”。在某些语言中,如Java,在返回后放置任何语句都是一个严重错误,因此您的错误可能很容易被发现。谢谢,是的,它看起来更干净,有很多东西需要学习,我的风格需要改进,即使我认为我还没有风格。
#include <iostream>
#include <string>
#include <cstdlib> 

using namespace std;

template<class T>
class BinaryTree
{
    struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = 0;
            rChildptr = 0;
        }
    };

private:

    Node* root;

    void Insert(T newData, Node** theRoot)
    {
        if (*theRoot == 0)
        {
            *theRoot = new Node(newData);
        }
        else if (newData < (*theRoot)->data)
            Insert(newData, &((*theRoot)->lChildptr));
        else
            Insert(newData, &((*theRoot)->rChildptr));
    }

    void PrintTree(Node* theRoot)
    {
        if (theRoot != 0)
        {
            PrintTree(theRoot->lChildptr);
            cout << theRoot->data << " " << endl;
            PrintTree(theRoot->rChildptr);
        }
    }

    T Largest(Node* theRoot)
    {
        if (root == 0)
        {
          throw "There is no tree";
        }

        if (theRoot->rChildptr != 0)
            return Largest(theRoot->rChildptr);
        else
            return theRoot->data;
    }

public:

    BinaryTree()
    {
        root = 0;
    }

    void AddItem(T newData)
    {
        Insert(newData, &root);
    }

    void PrintTree()
    {
        PrintTree(root);
    }

    T Largest()
    {
        return Largest(root);
    }
};

int main()
{
    BinaryTree<int> *myBT = new BinaryTree<int>();

    cout << "The tree is empty. Trying to find the largest element." << endl;

    try
    {
      cout << "Largest element: " << myBT->Largest() << endl;
    }
    catch (const char* e)
    {
      cout << "Exception caught: " << e << endl;
    }

    myBT->AddItem(15);
    myBT->AddItem(2);
    myBT->AddItem(5);
    myBT->AddItem(1);
    myBT->AddItem(10);

    cout << "Tree:" << endl;
    myBT->PrintTree();

    cout << "Largest element: " << myBT->Largest() << endl;
} 
The tree is empty. Trying to find the largest element.
Exception caught: There is no tree
Tree:
1
2
5
10
15
Largest element: 15