C# OOP: Binary Search Tree

Exercise: write a C# program to create a Binary Search Tree. This Binary Search Tree is to store the integer values. Your program should display a menu of choices to operate the Binary Search Tree data structure. See the sample menu below:

=================================================

              Binary Search Tree Operations Menu

==================================================
1. Add items

2. Delete items

3. Show min node

5. Show max node

6. Find a node

7.Print all nodes

7. Exit


Enter your choice:1


In a Binary Search Tree data structure that stores the integer values, there is

root node. The nodes that are less than the root node will be put in the left

side and the nodes that are greater than the root node will be put in the right

side. The nodes that are in the higher positions are called the parent nodes

and the nodes that are in the lower positions are called child nodes. The

parent nodes may have more than one child. It also may not have a child.

The node that doesn't have a child is called leaf.

Binary Search Tree

Solution:

To keep this solution simple and easy to follow, we divide this solution in to different steps.

Step 1: Defining the binary search tree node

To implement the binary search tree in C#, Each node of the binary search tree should contain 4 components--data, left child link, right child link and parent link. The left child link points to the left child and the right child link points to the right child. The parent link will be used to store the parent node.

public class TreeNode{
TreeNode(int elem) { data = elem;left=null;right=null;parent=null; }
public int data; //node data
public TreeNode left;//left child link
public TreeNode right;//right child link
public TreeNode parent;//parent link

}

To operate the binary search tree, we also provide another abstract class
called Cls that will be inherited by the our main class: BTree.

abstract class Cls{

public TreeNode insert(TreeNode Tree, int Tar){

return null;

}

public void delete(TreeNode Tree){

}

public TreeNode findmin(TreeNode Tree){
return null;
}

public TreeNode find(TreeNode Tree,int Tar){
return null;
}

public void printall(TreeNode Tree){

}


}



Comments

a comment

 a

a


2016-12-07



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.