#include <bits/stdc++.h>
using namespace std;
struct Node{
char data[50];
struct Node* right;
struct Node* left;
};
typedef struct Node* NODE;
bool iterativeSearch(struct Node* root, char key[])
{
// Traverse untill root reaches to dead end
while (root != NULL) {
// pass right subtree as new tree
if (key > root->data)
root = root->right;
// pass left subtree as new tree
else if (key < root->data)
root = root->left;
else
return true; // if the key is found return 1
}
return false;
}
NODE createNode(char data[]){
NODE newNode = (NODE) malloc (sizeof(struct Node));
if(!newNode){
cout<<"Not enough memory"<<endl;
exit(-1);
}
newNode->left = NULL;
newNode->right = NULL;
strcpy(newNode->data,data);
return (newNode);
}
void insertNode(NODE* head,char data[]){
NODE newNode = createNode(data);
NODE hold_the_head = *head;
if(*head == NULL){
*head = newNode;
(*head)->right = NULL;
(*head)->left = NULL;
return;
}
while(1){
if((newNode->data>(*head)->data)&&((*head)->right== NULL)){
(*head)->right = newNode;
*head = hold_the_head;
return;
}
else if( newNode->data > (*head)->data ){
(*head) = (*head)->right;
}
else if( (newNode->data < (*head)->data) && ( (*head)->left == NULL ) ){
(*head)->left = newNode;
*head = hold_the_head;
return;
}
else if( newNode->data < (*head)->data ){
(*head) = (*head)->left;
}
}
}
void inOrderTraversal(NODE node){
if(node == NULL)
return;
inOrderTraversal(node->left);
cout<<node->data<<"\t";
inOrderTraversal(node->right);
}
int main(){
NODE head = NULL;
int t=5;
cin>>t;
while(t)
{
cout<<"Enter 1 to Add Customer\n 2 to Search Customer\n 3 to Display in Order Listing\n";
int n=0;
cin>>n;
if(n==1)
{
cout<<"Enter The Name Of Customer to add\n";
char str[50];
cin>>str;
insertNode(&head,str);
}
if(n==2)
{
cout<<"Enter a Customer Name to search\n";
char chr[50];
cin>>chr;
int temp=iterativeSearch(head, chr);
if(temp=1)
cout<<"Customer Found\n";
else
cout<<"Customer Not Found\n";
}
if(n==3)
{
inOrderTraversal(head);
}
--t;
}
/* insertNode(&head,"karan");
insertNode(&head,"sameer");
insertNode(&head,"palak");
insertNode(&head,"jagdish");
insertNode(&head,"naman");
insertNode(&head,"umang");
insertNode(&head,"chandu");
inOrderTraversal(head);
cout<<endl;*/
return 0;
}