Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 17-Jan-13 4:12am
blertag350
Edited 17-Jan-13 17:28pm
v3
Comments
BillWoodruff at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

(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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 503
1 OriginalGriff 384
2 George Jonsson 258
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100