Advertisement
Guest User

Untitled

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