Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include<iostream>

using namespace std;

class Node
{
public:
Node * Left;
Node * Right;
int Data;
};

class BST
{
Node *Head;
public:
BST()
{
Head = NULL;
}

void insert(int Value)


{
Node *temp = new Node;
temp->Data = Value;
temp->Left = NULL;
temp->Right = NULL;

if (Head == NULL)
{
Head = temp;
}
else
{
Node *curr = Head;
Node *Parent = NULL;
while (curr != NULL)
{
Parent = curr;
if (curr->Data < temp->Data)
{
curr = curr->Right;
}
else
curr = curr->Left;
}

if (Parent->Data > temp->Data)


{
Parent->Left = temp;
}
else
{
Parent->Right = temp;
}
}
}
void PrintOrder()
{
Inorder(Head);

}
void Inorder(Node *temp)
{
if (temp == NULL)
return;
else

{
Inorder(temp->Left);
cout << temp->Data << "\n";
Inorder(temp->Right);
}
}
bool Search(int val)
{
Node *temp = Head;
if (temp->Data == val)
return true;
else if (val > temp->Data)
{
cout << "Value Dismissed" << endl;
temp = temp->Right;
while (temp != NULL)
{
if (val == temp->Data)
{
cout << "VALUE FOUND " << endl;
return true;
}
else if (val > temp->Data)
temp = temp->Right;
else if (val < temp->Data)
temp = temp->Left;
}
}
else if (val < temp->Data)
{
temp = temp->Left;
while (temp != NULL)
{
if (val == temp->Data)
{
cout << "VALUE FOUND " << endl;
return true;
}
else if (val > temp->Data)
temp = temp->Right;
else if (val < temp->Data)
temp = temp->Left;
}
}
}
};

void main()
{
BST BsT;
int option;
int val;

do
{
cout << "Press 1 to input value " << endl;
cout << "Press 2 to output Display " << endl;
cout << " Press 3 to Search input value " << endl;

cin >> option;

switch (option)
{
case 1:
cout << "Enter Value \n";
cin >> val;
BsT.insert(val);
break;
case 2:
BsT.PrintOrder();
break;
case 3:
cout << "nter value \n";
cin >> val;
BsT.Search(val);
}
} while (option != 99 || option != -1);
system("pause");
}

You might also like