fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node{
  5. char data[50];
  6. struct Node* right;
  7. struct Node* left;
  8. };
  9.  
  10. typedef struct Node* NODE;
  11.  
  12. bool iterativeSearch(struct Node* root, char key[])
  13. {
  14. // Traverse untill root reaches to dead end
  15. while (root != NULL) {
  16. // pass right subtree as new tree
  17. if (key > root->data)
  18. root = root->right;
  19.  
  20. // pass left subtree as new tree
  21. else if (key < root->data)
  22. root = root->left;
  23. else
  24. return true; // if the key is found return 1
  25. }
  26. return false;
  27. }
  28.  
  29. NODE createNode(char data[]){
  30. NODE newNode = (NODE) malloc (sizeof(struct Node));
  31.  
  32. if(!newNode){
  33. cout<<"Not enough memory"<<endl;
  34. exit(-1);
  35. }
  36. newNode->left = NULL;
  37. newNode->right = NULL;
  38. strcpy(newNode->data,data);
  39. return (newNode);
  40. }
  41.  
  42. void insertNode(NODE* head,char data[]){
  43.  
  44. NODE newNode = createNode(data);
  45. NODE hold_the_head = *head;
  46. if(*head == NULL){
  47. *head = newNode;
  48. (*head)->right = NULL;
  49. (*head)->left = NULL;
  50. return;
  51. }
  52.  
  53. while(1){
  54. if((newNode->data>(*head)->data)&&((*head)->right== NULL)){
  55. (*head)->right = newNode;
  56. *head = hold_the_head;
  57. return;
  58. }
  59. else if( newNode->data > (*head)->data ){
  60. (*head) = (*head)->right;
  61. }
  62.  
  63. else if( (newNode->data < (*head)->data) && ( (*head)->left == NULL ) ){
  64. (*head)->left = newNode;
  65. *head = hold_the_head;
  66. return;
  67. }
  68. else if( newNode->data < (*head)->data ){
  69. (*head) = (*head)->left;
  70. }
  71. }
  72. }
  73.  
  74. void inOrderTraversal(NODE node){
  75.  
  76. if(node == NULL)
  77. return;
  78. inOrderTraversal(node->left);
  79. cout<<node->data<<"\t";
  80. inOrderTraversal(node->right);
  81. }
  82.  
  83. int main(){
  84.  
  85. NODE head = NULL;
  86. int t=5;
  87. cin>>t;
  88. while(t)
  89. {
  90. cout<<"Enter 1 to Add Customer\n 2 to Search Customer\n 3 to Display in Order Listing\n";
  91. int n=0;
  92. cin>>n;
  93. if(n==1)
  94. {
  95. cout<<"Enter The Name Of Customer to add\n";
  96. char str[50];
  97. cin>>str;
  98. insertNode(&head,str);
  99. }
  100. if(n==2)
  101. {
  102. cout<<"Enter a Customer Name to search\n";
  103. char chr[50];
  104. cin>>chr;
  105. int temp=iterativeSearch(head, chr);
  106. if(temp=1)
  107. cout<<"Customer Found\n";
  108. else
  109. cout<<"Customer Not Found\n";
  110. }
  111. if(n==3)
  112. {
  113. inOrderTraversal(head);
  114. }
  115.  
  116. --t;
  117. }
  118. /* insertNode(&head,"karan");
  119.   insertNode(&head,"sameer");
  120.   insertNode(&head,"palak");
  121.   insertNode(&head,"jagdish");
  122.   insertNode(&head,"naman");
  123.   insertNode(&head,"umang");
  124.   insertNode(&head,"chandu");
  125.  
  126.   inOrderTraversal(head);
  127.   cout<<endl;*/
  128. return 0;
  129. }
Success #stdin #stdout 0s 4480KB
stdin
5
1
Ram
1
Shyam
1
Abhimanyu
3
2
Ram
stdout
Enter 1 to Add Customer
 2 to Search Customer
 3 to Display in Order Listing
Enter The Name Of Customer to add
Enter 1 to Add Customer
 2 to Search Customer
 3 to Display in Order Listing
Enter The Name Of Customer to add
Enter 1 to Add Customer
 2 to Search Customer
 3 to Display in Order Listing
Enter The Name Of Customer to add
Enter 1 to Add Customer
 2 to Search Customer
 3 to Display in Order Listing
Ram	Shyam	Abhimanyu	Enter 1 to Add Customer
 2 to Search Customer
 3 to Display in Order Listing
Enter a Customer Name to search
Customer Found