Barta Tamás
Barta Tamás

Reputation: 899

Largest number in binary tree

So i try to find the largest number in my binary tree so i can delete it, but it wont run, insert the part where i am searching for the largest number, the tree works just fine, without this part.

Here is the code i got for now:

#include <iostream>
#include <string>


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);
            }
        }
        void Largest( Node* theRoot, T max)
        {
            if ( theRoot == null )
            return -1;

            int left = Largest(theRoot->lChildptr);
            int right = Largest ( theRoot->rChildptr);
        if( theRoot->data > left && theRoot->data > right )
        return theRoot->data;
             else
       return max ( left, right );
};

    public:
        BinaryTree()
        {
            root = NULL;
        }

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

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

    int main()
    {
        BinaryTree<int> *myBT = new BinaryTree<int>();
        myBT->AddItem(2);
        myBT->AddItem(5);
        myBT->AddItem(1);
        myBT->AddItem(10);
        myBT->AddItem(8);
        myBT->PrintTree();
        myBT->Largest();
    } 

Upvotes: 0

Views: 1364

Answers (3)

Minh
Minh

Reputation: 21

Since a binary search tree means the left child is smaller than node, right child is larger or equal to the node.

I think you would only need to find the right most child to find the largest node.

Anyways, you have 2 versions BinaryTree::Largest, one that takes 0 arguments, and one that takes 2 arguments.

In void BinaryTree::Largest(), you call Largest(root), but there's no Largest that takes only one argument. Perhaps you intended on passing one of the template objects in?

The other problem I see is that the other Largest function returns void and not a number, and uses the template object (max) like it was a function when it's not (it's an int in your example). Later you return the template, when the function declared it was returning void.

I'd recommend you change Largest to return the template instead of void.

T Largest( Node* theRoot)//<-- Notice returning of the template, not void
{
    if ( theRoot == null )
        return -1;

    T left = Largest(theRoot->lChildptr);
    T right = Largest ( theRoot->rChildptr);
    if( theRoot->data > left && theRoot->data > right )
        return theRoot->data;
    else if (left < right)//<-- max was not a function, manual compare
        return right;
    else
        return left;
}//<--You were missing a "}" here too

//...

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

Upvotes: 0

Vlad
Vlad

Reputation: 18633

It seems you lied about deleting the largest right child.

Aside from return types and arities, you're making it a bit too complicated. Wikipedia says:

The left subtree of a node contains only nodes with keys less than the node's key.

Upvotes: 0

Sean Nilan
Sean Nilan

Reputation: 1745

The largest number in your tree will be the right-most node in the tree. So keep going right until you can't any longer.

// removed code since this is probably a homework question

Upvotes: 1

Related Questions