Advertisement
Guest User

Binary Search Tree

a guest
Nov 18th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include<map>
  4. #include<stdlib.h>
  5. using namespace std;
  6.  
  7. #define YES 1
  8. #define NO 0
  9.  
  10. class tree
  11. {
  12.     private:
  13.         struct leaf
  14.         {
  15.             int data;
  16.             leaf *l;
  17.             leaf *r;
  18.         };
  19.         struct leaf *p;
  20.  
  21.     public:
  22.         tree();
  23.         ~tree();
  24.         void destruct(leaf *q);
  25.         tree(tree& a);
  26.         void findparent(int n,int &found,leaf* &parent);
  27.         void findfordel(int n,int &found,leaf *&parent,leaf* &x);
  28.         void add(int n);
  29.         void transverse();
  30.         void in(leaf *q);
  31.         void pre(leaf *q);
  32.         void post(leaf *q);
  33.         void del(int n);
  34. };
  35.        
  36. tree::tree()
  37. {
  38.     p=NULL;
  39. }
  40.  
  41. tree::~tree()
  42. {
  43.     destruct(p);
  44. }
  45.  
  46. void tree::destruct(leaf *q)
  47. {
  48.     if(q!=NULL)
  49.     {
  50.         destruct(q->l);
  51.         del(q->data);
  52.         destruct(q->r);
  53.     }
  54. }
  55. void tree::findparent(int n,int &found,leaf *&parent)
  56. {
  57.     leaf *q;
  58.     found=NO;
  59.     parent=NULL;
  60.  
  61.     if(p==NULL)
  62.         return;
  63.  
  64.     q=p;
  65.     while(q!=NULL)
  66.     {
  67.         if(q->data==n)
  68.         {
  69.             found=YES;
  70.             return;
  71.         }
  72.         if(q->data>n)
  73.         {
  74.             parent=q;
  75.             q=q->l;
  76.         }
  77.         else
  78.         {
  79.             parent=q;
  80.             q=q->r;
  81.         }
  82.     }
  83. }
  84.  
  85. void tree::add(int n)
  86. {
  87.     int found;
  88.     leaf *t,*parent;
  89.     findparent(n,found,parent);
  90.     if(found==YES)
  91.         cout<<"\nSuch a Node Exists";
  92.     else
  93.     {
  94.         t=new leaf;
  95.         t->data=n;
  96.         t->l=NULL;
  97.         t->r=NULL;
  98.  
  99.         if(parent==NULL)
  100.             p=t;
  101.         else
  102.             parent->data > n ? parent->l=t : parent->r=t;
  103.     }
  104. }
  105.  
  106. void tree::transverse()
  107. {
  108.     int c;
  109.     cout<<"\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
  110.     cin>>c;
  111.     switch (c)
  112.     {
  113.         case 1:
  114.             in(p);
  115.             break;
  116.  
  117.         case 2:
  118.             pre(p);
  119.             break;
  120.  
  121.         case 3:
  122.             post(p);
  123.             break;
  124.     }
  125. }
  126.  
  127. void tree::in(leaf *q)
  128. {
  129.     if(q!=NULL)
  130.     {
  131.         in(q->l);
  132.         cout<<"\t"<<q->data<<endl;
  133.         in(q->r);
  134.     }
  135.    
  136. }
  137.  
  138. void tree::pre(leaf *q)
  139. {
  140.     if(q!=NULL)
  141.     {
  142.         cout<<"\t"<<q->data<<endl;
  143.         pre(q->l);
  144.         pre(q->r);
  145.     }
  146.    
  147. }
  148.  
  149. void tree::post(leaf *q)
  150. {
  151.     if(q!=NULL)
  152.     {
  153.         post(q->l);
  154.         post(q->r);
  155.         cout<<"\t"<<q->data<<endl;
  156.     }
  157.    
  158. }
  159.  
  160. void tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)
  161. {
  162.     leaf *q;
  163.     found=0;
  164.     parent=NULL;
  165.     if(p==NULL)
  166.         return;
  167.  
  168.     q=p;
  169.     while(q!=NULL)
  170.     {
  171.         if(q->data==n)
  172.         {
  173.             found=1;
  174.             x=q;
  175.             return;
  176.         }
  177.         if(q->data>n)
  178.         {
  179.             parent=q;
  180.             q=q->l;
  181.         }
  182.         else
  183.         {
  184.             parent=q;
  185.             q=q->r;
  186.         }
  187.     }
  188. }
  189.  
  190. void tree::del(int num)
  191. {
  192.     leaf *parent,*x,*xsucc;
  193.     int found;
  194.  
  195.     // If EMPTY TREE
  196.     if(p==NULL)
  197.     {
  198.         cout<<"\nTree is Empty";
  199.         return;
  200.     }
  201.     parent=x=NULL;
  202.     findfordel(num,found,parent,x);
  203.     if(found==0)
  204.     {
  205.         cout<<"\nNode to be deleted NOT FOUND";
  206.         return;
  207.     }
  208.  
  209.     // If the node to be deleted has 2 leaves
  210.     if(x->l != NULL && x->r != NULL)
  211.     {
  212.         parent=x;
  213.         xsucc=x->r;
  214.  
  215.         while(xsucc->l != NULL)
  216.         {
  217.             parent=xsucc;
  218.             xsucc=xsucc->l;
  219.         }
  220.         x->data=xsucc->data;
  221.         x=xsucc;
  222.     }
  223.  
  224.     // if the node to be deleted has no child
  225.     if(x->l == NULL && x->r == NULL)
  226.     {
  227.         if(parent->r == x)
  228.             parent->r=NULL;
  229.         else
  230.             parent->l=NULL;
  231.  
  232.         delete x;
  233.         return;
  234.     }
  235.  
  236.     // if node has only right leaf
  237.     if(x->l == NULL && x->r != NULL )
  238.     {
  239.         if(parent->l == x)
  240.             parent->l=x->r;
  241.         else
  242.             parent->r=x->r;
  243.  
  244.         delete x;
  245.         return;
  246.     }
  247.  
  248.     // if node to be deleted has only left child
  249.     if(x->l != NULL && x->r == NULL)
  250.     {
  251.         if(parent->l == x)
  252.             parent->l=x->l;
  253.         else
  254.             parent->r=x->l;
  255.  
  256.         delete x;
  257.         return;
  258.     }
  259. }
  260.  
  261. string value(istream &input)
  262. {
  263.     char text;
  264.     string answer="";
  265.     text=input.get();
  266.     while(!isdigit(text) && !input.eof())
  267.     {
  268.      text=input.get();
  269.     }
  270.    
  271.    while(isdigit(text))
  272.    {
  273.    answer.push_back((text));
  274.    text = input.get();
  275.    }
  276.     return answer;
  277. }
  278.  
  279. int main()
  280. {
  281.     tree t;
  282.     map<int,int> word;
  283.     map<int,int>::iterator count;
  284.     string str;
  285.     int num;
  286.     string space ="";
  287.     while((str=value(cin))!=space )
  288.     {
  289.         num = atoi(str.c_str());
  290.             ++word[num];
  291.     }
  292.     int size = (int) word.size();
  293.     int * data = new int[size];;
  294.     int x = 0;
  295.         for(count = word.begin(); count!=word.end(); ++count){
  296.         data[x] = (*count).first;
  297.         x = x+1;
  298.         }
  299.     for(int iter=0 ; iter < 4 ; iter++)
  300.         t.add(data[iter]);
  301.  
  302.     t.transverse();
  303.     return 0;
  304. }
  305.  
  306.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement