如何显示二叉树的最大和最小元素? [英] How to display the largest and the smallest elements of binary tree?

查看:199
本文介绍了如何显示二叉树的最大和最小元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Node findmin(Node Root)
        {
            if (Root == null)
                return null;
            else if (Root.leftChild == null)
                return Root;
            else
                return findmin(Root.leftChild);
        }

        public Node findmax(Node Root)
        {
            if (Root == null)
                return null;
            else if (Root.rightChild == null)
                return Root;
            else
                return findmax(Root.rightChild);
        }
        
    }

    class Program
    {
            Console.WriteLine("Extreme elements");
            Console.WriteLine("----------------------------------------------------------------");
            Console.WriteLine();
            Console.WriteLine("The smallest element is:");
            //I don't know how to display the smallest or the largest element
             
            Console.WriteLine();
            Console.WriteLine("The largest element is:");

            
            Console.WriteLine();
            Console.WriteLine("----------------------------------------------------------------");

            Console.ReadLine();

        }

    }
}

推荐答案

(1)走树(深度优先遍历是一种简单的递归算法 - 请参阅 http://en.wikipedia。 org / wiki / Tree_traversal#Depth-first [ ^ ])

(2)跟踪每个访问节点的最小和最大数据值



/ ravi
(1) Walk the tree (a depth first traversal is a simple recursive algorithm - see http://en.wikipedia.org/wiki/Tree_traversal#Depth-first[^])
(2) Track the minimum and maximum data value of each visited node

/ravi


除了正确的解决方案1,还要考虑比尔的正确评论。



最高和最低只能是定义订单关系的一些集合的属性。由于我们总是处理有限集,因此总是存在最大值和最小值,但是,如果关系定义了不少于的概念,则自然地,不止一个元素可以表示最大值或最小值,因为两个不同的节点可以比较为相等 根据这个定义。这样的集可以是一组数值,但它几乎可以是任何东西。



比较的标准可以是任何东西,但它们应该满足一些公理,或这种关系的条件。例如:比较结果应该是常数(例如,不依赖于时间),有序元素链不能循环:A> B> C和C>不应该允许A. (我希望您理解这种关系与定义您树的父子关系无关。)



您只需要实现接口 System.IComparable< Node> ,类型为 Node 。你甚至没有显示这种类型,但它可以是 struct class 。请参阅:

http://msdn.microsoft.com/en-us /library/43hc6wht.aspx [ ^ ]。



-SA
In addition to the correct Solution 1, taking in consideration the correct comment by Bill.

Maximum and minimum can only be the attribute of some sets where order relationship is defined. As we always deal with finite sets, maximum and minimum always exist, but, if the relationship defines the notion of "no less", naturally, more than one element could represent maximum or minimum, because two different nodes can be compared as "equal" according this definition. Such set could be the set of numeric values, but it could be nearly anything.

The criteria for comparison can be anything, but they should satisfy some set of axioms, or condition for this relationship. For example: the result of comparison should be constant (not depend on time, for example), the chain of ordered elements can not cycle: A > B > C and C > A should not be allowed. (I hope you understand that this relationship has nothing to do with parent-child relationship which defines you tree.)

All you need is to implement the interface System.IComparable<Node> for the type Node. You did not even show this type, but it can be struct or class. Please see:
http://msdn.microsoft.com/en-us/library/43hc6wht.aspx[^].

—SA


这篇关于如何显示二叉树的最大和最小元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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