﻿ C exercises and solutions: Binary Search Tree

# C exercises and solutions: Binary Search Tree

## C pointer: 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. View all items

4. Show max item

5. Show min item

6. Find an item

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

Each node of the binary search tree contains 3 components--data and two links. The two links are pointers. One pointer points to the left child and another points to the right child. In this step, we also declare a root node pointer. It is a start node of our binary search tree.

typedef struct TreeNode{

int data; // the data
struct TreeNode *pLeft; //left child link
struct TreeNode *pRight; // right child link
}Node;

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.