Binary tree

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

//...




    public:
        BinaryTree()
        {
            root = NULL;
        }

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

        void PrintTree()
        {
            PrintTree(root);
        }


        
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(8);
        myBT->PrintTree();
        myBT->Largest();
    }

 

posted @ 2011-11-19 14:09  Thomas Hwang  阅读(180)  评论(0编辑  收藏  举报