MUstar

IoT C++ 09/23 - Binary Search Tree Example (Interger/InCPT)

Sep 24th, 2017
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstdlib>
  3. #define NULL __null
  4.  
  5. using namespace std;
  6.  
  7. typedef struct treenode {
  8.     int data;
  9.     struct treenode* left;
  10.     struct treenode* right;
  11. } treenode;
  12.  
  13. treenode* input(treenode* node, int data)
  14. {
  15.     if(node == NULL){
  16.         treenode* num;
  17.         //cout<<"[node]"<<endl;
  18.         num = new treenode[100];
  19.         num->data = data;
  20.         num->left = NULL;
  21.         num->right = NULL;
  22.         return num;
  23.     }
  24.     else if (data < node->data){
  25.         //cout<<"[left]"<<endl;
  26.         node->left = input(node->left,data);
  27.     }
  28.     else if(data > node->data){
  29.         //cout<<"[right]"<<endl;
  30.         node->right = input(node->right,data);
  31.     }
  32.     else{
  33.         cout<<"ERROR>Already R0egistered"<<endl;
  34.         return node;
  35.     }
  36. }
  37.  
  38. void preorder(treenode* node)
  39. {
  40.     cout<<node->data<<" ";
  41.     if (node->left) preorder(node->left);
  42.     if (node->right)    preorder(node->right);
  43. }
  44.  
  45. void ctrorder(treenode* node)
  46. {
  47.     if (node->left) ctrorder(node->left);
  48.     cout<<node->data<<" ";
  49.     if (node->right)    ctrorder(node->right);
  50. }
  51.  
  52. void lstorder(treenode* node)
  53. {
  54.     if (node->left) lstorder(node->left);
  55.     if (node->right)    lstorder(node->right);
  56.     cout<<node->data<<" ";
  57. }
  58.  
  59. void search(treenode* node , int data)
  60. {
  61.     if(node == NULL){
  62.         cout<<"ERROR>NONE TREENODE!"<<endl;
  63.         return;
  64.     }
  65.     else if(data == node->data) {
  66.         cout<<"CENTRE NUM>"<<node->data<<endl;
  67.         if(node->left!=NULL)cout<<"LEFT NUM>"<<node->left->data<<endl;
  68.         else cout<<"LEFT NUM>"<<"None"<<endl;
  69.         if(node->right!=NULL)cout<<"RIGHT NUM>"<<node->right->data<<endl;
  70.         else cout<<"RIGHT NUM>"<<"None"<<endl;
  71.         return;
  72.     }
  73.     else if (data < node->data) {
  74.         search(node->left,data);
  75.     }
  76.     else if (data > node->data) {
  77.         search(node->right,data);
  78.     }
  79.     else {
  80.      cout<<"ERROR>SEARCH ERROR!"<<endl;
  81.     }
  82. }
  83. void delnum(treenode* node , int data)
  84. {
  85.     if(data == node->data) {
  86.         if(data==5){
  87.             cout<<"ERROR>NO! DEL 1STCTR NUMBER"<<endl;
  88.         }
  89.         else{
  90.             node->data = (int)NULL;
  91.             if(node->left!=NULL) node->left = NULL;
  92.             if(node->right!=NULL) node->right = NULL;
  93.         }
  94.     }
  95.     else if (data < node->data) {
  96.         delnum(node->left,data);
  97.     }
  98.     else if (data > node->data) {
  99.         delnum(node->right,data);
  100.     }
  101.     else {
  102.      cout<<"ERROR>DELETE ERROR!"<<endl;
  103.     }
  104. }
  105.  
  106.  
  107. int main(void)
  108. {
  109.     int num, select;
  110.     treenode* node;
  111.     node=NULL;
  112.     cout<<"1STCTR>"; cin>>num;
  113.     node = input(node,num);
  114.     while(1)
  115.     {
  116.         cout<<"Input(OUT:-1)>"; cin>>num;
  117.         if(num==-1)
  118.             break;
  119.         input(node,num);
  120.     }
  121.     while(2)
  122.     {
  123.         cout<<"Menu> 1)ALLVIEW , 2)SELECT_VIEW, 3)DELETE 4)EXIT"<<endl;
  124.         cout<<"Select>"; cin>>select;
  125.         if(select==4) break;
  126.         switch(select)
  127.         {
  128.             case 1:
  129.             {
  130.                 cout<<"pre>"; preorder(node); cout<<endl;
  131.                 cout<<"ctr>"; ctrorder(node); cout<<endl;
  132.                 cout<<"lst>"; lstorder(node); cout<<endl;
  133.                 break;
  134.             }
  135.             case 2:
  136.                 cout<<"Msg>ENTER U WANT VIEW NUMBER"<<endl;
  137.                 cout<<"Input>"; cin>>select;
  138.                 search(node , select);
  139.                 break;
  140.             case 3:
  141.                 cout<<"Msg>ENTER U WANT DEL NUMBER"<<endl;
  142.                 cout<<"Input>"; cin>>select;
  143.                 delnum(node , select);
  144.                 break;
  145.             default:
  146.                 cout<<"Invalid Select"<<endl; break;
  147.         }
  148.         cout<<endl;
  149.     }
  150.     delete[] node; node=NULL;
  151.     cout<<"ByeBye"<<endl;
  152.     return 0;
  153. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×