__http://en.wikipedia.org/wiki/Tree_traversal#Depth-first__[^])

(2) Track the minimum and maximum data value of each visited node

/ravi

```
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();
}
}
}
```

Comments

(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

(2) Track the minimum and maximum data value of each visited node

/ravi

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

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

—SA

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[^].

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

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

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.

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