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



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.