C++ tutorial: structure-Binary Search Tree

C++ Structure-Binary Search Tree


By using structure, write a C++ program to create a Binary Search Tree. This Binary Search Tree is to store the integer values. The 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

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



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.

struct ListElem{

  int data; // the element data
  ListElem *pLeft; // pLeft links to the left sub-tree
  ListElem *pRight; // pRight links to the right sub-tree

  ListElem *Root; //Declaring a pointer to root node

For the next steps of our solution, please see their links on the menu at the

left side of the page. 


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.