﻿ C++ exercises and solutions: Binary Search Tree-OOP

# C++ exercises and solutions: Binary Search Tree-OOP

Step 3: Finding the min and max nodes

In binary search tree, the smallest node is in the left side and the largest node is in the right side.

To find the smallest node, the process will check the parent node. In case that the parent node is not empty, if it doesn't a left child node, the smallest node is the parent node, otherwise the smallest node is its left child node.

//find min node
template <class Type>
Node<Type> *BSTree<Type>::findmin(Node<Type> *T)

{
if(T==NULL) return NULL;
else if(T->pLeft==NULL) return T;
else return findmin(T->pLeft);
}

To find the largest node, the process will check the parent node. In case that the parent node is not empty, if it doesn't a right child node, the largest node is the parent node, otherwise the largest node is its right child node.

//find max node
template <class Type>
Node<Type> *BSTree<Type>::findmax(Node<Type> *T)

{
if(T==NULL) return NULL;
else if(T->pRight==NULL) return T;
else return findmax(T->pRight);
}