Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, so i made a binary tree in a template. Now i would like to find the largest number in case element, in my tree. I think i got the right way to find it but, there is something wring, when i run my code, i got 6 times written There is no tree. I think i got to much confused by the passing data, and cout or there is no tree to search on?

Here is my code:(anyhow the tree is working just fine, the PrintTree function shows it in the console)

#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 ( theRoot == NULL )
			{
			cout<<"There is no tree";
			return -1;
			}else{
		

			T left = Largest(theRoot->lChildptr);
			T right = Largest ( theRoot->rChildptr);
			if( theRoot->data > left && theRoot->data > right ){
					return theRoot->data;
					cout<<theRoot->data;
			}
			   else if (left < right)
			   {
					return right;
					cout<<right;
				}
			   else
			   {
					return left;
					cout<<left;
			   }
			}

	
		};

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

</int></int></class></cstdlib></string></iostream>
Posted

I think you're missing a check to see if the node actually has children before recursively calling largest on it, so eventually it will be passed NULL and say there is no tree. Also, you should only need to check one of the children recursively if I remember by binary trees correctly, only one child could possibly be larger than the current node.


Try something like this:
C++
T Largest( Node* theRoot)
{
    if (theRoot->rChildptr != NULL)
        return Largest(theRoot->rChildptr);
    else
        return theRoot->data;
}
 
Share this answer
 
v2
first i was thinking of something like this:
C#
#include<stdio.h>
#include<conio.h>

struct node
{
  int data;
  struct node *left,*right;
}*tree;

struct node* MAX(struct node* q)
{
    struct node* temp;
 while(q->right!=NULL)
    {
       temp=q;
        q=q->right;
     }
 return temp;
}


cause the right child is always bigger, so i would just go straight down to the right bottom child, but not sure how to implement into my code..
 
Share this answer
 
Comments
lewax00 18-Nov-11 18:39pm    
Just recursively check the right child until the current node's right child is NULL, then return the current node's value, I'll add some code to my answer.
i did this after your code:
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;


}; 


but still wont show me the largest number only the ordered list...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900