二叉树找到最大的数字 [英] Binary Tree Find Largest Number

查看:96
本文介绍了二叉树找到最大的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在模板中制作了一棵二叉树.现在,我想在我的树中找到最大的case元素数.我认为我找到了正确的方法,但是有些麻烦,当我运行我的代码时,我写了6次,没有树.我想我对传递的数据感到非常困惑,但是cout还是没有树可以搜索?

这是我的代码:(无论如何树都工作正常,PrintTree函数在控制台中显示它)

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

解决方案

我认为您在递归调用最大节点之前错过了检查该节点是否确实有子节点的检查,因此最终将通过该节点NULL并说没有树.另外,如果我正确地记住了二叉树,则只需递归检查其中一个子代,只有一个子代可能大于当前节点.


尝试这样的事情:

 T最大(节点* theRoot)
{
    如果(theRoot-> rChildptr!= NULL)
        返回最大(theRoot-&r; rChildptr);
    其他
        返回 theRoot->数据;
} 


首先,我想到的是这样的东西:

#include< stdio.h>
#include< conio.h>

结构节点
{
   int 数据;
   struct 节点* left,* right;
}*树;

结构节点* MAX(结构节点* q)
{
    结构节点* temp;
 同时(q->正确!= NULL)
    {
       temp = q;
        q = q->对;
     }
 返回温度;
} 



因为合适的孩子总是更大,所以我会直接找最右边的孩子,但不确定如何在我的代码中实现.

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;


}; 



但仍然不会显示仅排序列表中最大的数字...


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>

解决方案

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:

T Largest( Node* theRoot)
{
    if (theRoot->rChildptr != NULL)
        return Largest(theRoot->rChildptr);
    else
        return theRoot->data;
}


first i was thinking of something like this:

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


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...


这篇关于二叉树找到最大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆