仍然找不到二叉树中的最大数字

Still can't find largest number in binary tree

本文关键字:数字 找不到 二叉树      更新时间:2023-10-16

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

下面是我的全部代码:

#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();
    } 

,这里是应该找到最大数字的部分(最右边的子元素):

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;
}; 

Largest()的代码中有两个问题:

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

  • 您想在返回后执行计数打印,这是不可能的。

所以你应该把你的代码片段替换为:

    else {
        cout << theRoot->data;
        return theRoot->data;
    }
顺便说一句,不要让双分号(;;)留在你的代码中。在大多数情况下,它是无害的,但它总是坏的风格。

下面是一个修复的、稍微清理过的版本:

#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