Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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();

        }

    }
}
Posted
Updated 17-Jan-13 17:28pm
v3
Comments
BillWoodruff 17-Jan-13 10:44am    
Exactly what do you mean here by "largest," and "smallest" elements ? What exactly does an "element" contain: a number ?

Obviously, the code you post here is doing no internal comparisons of any elements, and, you are going to have implement comparisons.

"Binary Trees" could be sorted, or unsorted, based on some criterion: you can search through them breadth first, or depth first. Tell us something about the structure of the binary tree you are working with now.
Sergey Alexandrovich Kryukov 17-Jan-13 12:24pm    
You are absolutely right, and this is the key, in addition to the pretty trivial problem of tree traversing (explained in Solution 1).

This is not so trivial thing. The general theory describing the relationships related to maximum and minimum of final sets is the fundamental Lattice Theory:
http://en.wikipedia.org/wiki/Lattice_%28order%29

In my answer, I explained how the comparison should be implemented. Please see Solution 2.
Of course, your comment is credited.

Cheers,
—SA

(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
 
Share this answer
 
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
 
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