12,510,006 members (52,828 online)
Rate this:
Please Sign up or sign in to vote.
See more:
```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
blertag366
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

## 2 solutions

Rate this:
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
Rate this:
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

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

Top Experts
Last 24hrsThis month
 OriginalGriff 261 Maciej Los 215 John Simmons / outlaw programmer 175 CPallini 140 David_Wimbley 140
 OriginalGriff 7,927 Maciej Los 4,091 ppolymorphe 4,064 David_Wimbley 3,326 Karthik Bangalore 3,161

Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
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