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

时间:2011-11-19 00:11:21

标签: c++ binary-tree

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

这是我的整个代码:

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

}; 

2 个答案:

答案 0 :(得分:2)

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

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

  • 你想在返回后执行cout打印,这是不可能的。切换订单。

所以你应该用这个替换你的代码片段:

    else {
        cout << theRoot->data;
        return theRoot->data;
    }

顺便说一句,不要让双分号(;;)保留在您的代码中。在大多数情况下它都是无害的,但它总是很糟糕。

答案 1 :(得分:0)

这是一个固定且稍微清理过的版本:

#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