# Finding Maximum and Minimum Values In Binary Search Tree . (Data Structures And Algorithms)

## Finding Maximum and Minimum Values

Incidentally, we should note how easy it is to find the maximum and minimum values in a binary search tree. In fact, this process is so easy we will show code for it. Still, understanding how it works is important. For the minimum, go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. This node is the minimum, as shown in Figure below:
Binary Trees Minimum 47 22 67 63 71 33 17 11 60 51 50 49 53 FIGURE Minimum value of a tree.

### Here’s some code that returns the node with the minimum key value:

Node* minimum()              // returns node with minimum key value
{
Node* current, last;
current = root;              // start at root
while(current != null)           // until the bottom
{
last = current;            // remember node
current = current.leftChild;               // go to left child }
return last;
}
We’ll need to know about finding the minimum value when we set about deleting a node. For the maximum value in the tree, follow the same procedure, but go from right child to right child until you find a node with no right child. This node is the maximum. The code is the same except that the last statement in the loop is

### C++ Code To Find Maximum Value In Binary Search Tree

public Node maximum()              // returns node with maximum key value
{
Node current, last;
current = root;              // start at root
while(current != null)           // until the bottom
{
last = current;            // remember node
current = current.rightChild;                // go to right child
}
return last;
}