Wednesday, 3 August 2011

Binary Search Tree - Code


Binary Search Tree
The following C++ code will explains the implementation of Binary Search Tree.
The following implementation is in object oriented using the Tree class and tree object
The implemented ADT functions of Binary Search Tree are insert and search.


In Tree.h
#pragma once

class Tree
{
public:
struct Node
{
int data;
Node *left;
Node *right;
Node *previous;
}*root, *current, *p;
int SearchSign, sign, level;

public:
Tree(); // Constructor
~Tree(); // Destructor

void InsertInBinaryTree(int _numberToInsert);
void SearchInBinaryTree(int _numberToSearch);
};

In Tree.cpp
#include "Tree.h"
#include "iostream"
using namespace std;

Tree::Tree()
{
root = new Node;
root->previous = NULL;
root->data = 0;
root->left = NULL;
root->right = NULL;
current = root;
p = root;
SearchSign = 0;
sign = 0;
level = 0;
}

Tree::~Tree()
{
delete root;
}

void Tree::InsertInBinaryTree(int _numberToInsert)
{
if (_numberToInsert > current->data) // Right Subtree
sign = 2;
else if (_numberToInsert < current->data) // Left Subtree
sign = 1;
else // Equal to the root
sign = 0;

switch (sign)
{
case 2: if (current->right == NULL)
{
current= new Node;
current->data = _numberToInsert;
current->previous = p;
cout << "The number " << _numberToInsert << " goes on right subtree with parent : " << current->previous->data;
p->right = current;
current->left = NULL;
current->right = NULL;
p = current;
}
else
{
p = p->right;
current = p;
Tree::InsertInBinaryTree(_numberToInsert);
}
break;
case 1: if (current->left == NULL)
{
current = new Node;
current->data = _numberToInsert;
current->previous = p;
cout << "The number " << _numberToInsert << " goes on left subtree with parent : " << current->previous->data;
p->left = current;
current->left = NULL;
current->right = NULL;
p = current;
}
else
{
p = p->left;
current = p;
Tree::InsertInBinaryTree(_numberToInsert);
}
break;
case 0: cout << "Number " << _numberToInsert << " is already with root: " << p->previous->data;
break;
}
sign = 3;
}
void Tree::SearchInBinaryTree(int _numberToSearch)
{
if (_numberToSearch > current->data) // Right Subtree
SearchSign = sign = 2;
else if (_numberToSearch < current->data) // Left Subtree
SearchSign = sign = 1;
else // Equal to the root
sign = 0;

switch (sign)
{
case 2:
p = p->right;
current = p;
++level;
Tree::SearchInBinaryTree(_numberToSearch);
break;
case 1:
p = p->left;
current = p;
++level;
Tree::SearchInBinaryTree(_numberToSearch);
break;
case 0:if (SearchSign == 2)
cout << "The number " << _numberToSearch << " is on right node with parent : " << current->previous->data << " and level : " << level;
else if (SearchSign == 1)
cout << "The number " << _numberToSearch << " is on left node with parent : " << current->previous->data << " and level : " << level;
if (root->data == _numberToSearch)
cout << "The number " << _numberToSearch << " is the root of your tree and level : " << level;
break;
}
sign = 3;
}

In Program.h
#pragma once

#include "iostream"
#include "Tree.h"
using namespace std;

class Program
{
public:
static int Main()
{
Tree tree;
cout << "Enter a number to make the root: ";
cin >> tree.root->data;

char inputCharacter = 'y';
//------------Insert values--------------------
while ((inputCharacter == 'y') || (inputCharacter == 'Y'))
{
int inputNumber;
cout << "\nEnter a number to make the node of your tree: ";
cin >> inputNumber;
tree.InsertInBinaryTree(inputNumber);

tree.current = tree.root;
tree.p = tree.root;
tree.sign = 3;

cout << "\n\nDo you want to enter more values in your Tree: (y/n) : ";
cin >> inputCharacter;

}

//------------Search values--------------------
inputCharacter = 'y';
while ((inputCharacter == 'y') || (inputCharacter == 'Y'))
{
tree.current = tree.root;
tree.p = tree.root;
tree.sign = 3;
tree.SearchSign = 3;
tree.level = 0;

int searchNumber;
cout << "\n\n\nEnter a number to search in your tree: ";
cin >> searchNumber;
tree.SearchInBinaryTree(searchNumber);

cout << "\n\nDo you want to enter more values in your Tree: (y/n) : ";
cin >> inputCharacter;

}

return 0;
}
};

In main.cpp
#include "Program.h"

int main() {
return Program::Main();;
}



For the concept of Binary Search Tree see : Binary Search Tree  Feel free to comment with your questions and suggestions regarding the post content...!

No comments:

Post a Comment

Convey your thoughts to authors.