﻿ C# exercises and solutions- C# OOP Binary Search Tree

## 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:

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

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

2. Delete items

3. Show min node

5. Show max node

6. Find a node

7.Print all nodes

7. Exit

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.

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

}

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){

}

}