13,096,399 members (56,132 online)
Rate this:
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("----------------------------------------------------------------");

}

}
}```
Posted 17-Jan-13 4:12am
blertag366
Updated 17-Jan-13 17:28pm
v3
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

Rate this:

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

## 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 148 Graeme_Grant 140 Jochen Arndt 120 Richard MacCutchan 80 Mohibur Rashid 59
 OriginalGriff 4,164 Graeme_Grant 2,277 ProgramFOX 2,067 Jochen Arndt 1,805 ppolymorphe 1,735

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 17 Jan 2013