C# OOP: Binary Search Tree

Max and Min nodes of BS tree

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 the min node

public TreeNode findmin(TreeNode Tree){
if(Tree==null) return null;
else if(Tree.left==null) return Tree;
else return findmin(Tree.left);
}

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 the max node

public TreeNode findmax(TreeNode Tree){
if(Tree==null) return null;
else if(Tree.right==null) return Tree;
else return findmax(Tree.right);
}




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.