Advertisement
N_DM

ParentArrayBin3

Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #define CAPACITY 100
  7.  
  8. template <class T = int>
  9. struct node
  10. {
  11.     T inf;
  12.     node<T> *left, *right;
  13. };
  14.  
  15. template <class T = int>
  16. class ParentBT
  17. {
  18. public:
  19.     ParentBT();
  20.     ~ParentBT();
  21.     ParentBT(const ParentBT<T>&);
  22.     ParentBT<T>& operator=(const ParentBT<T>&);
  23.  
  24.     bool empty() const;
  25.     T getRoot() const;
  26.     void create();
  27.     void createTree(node<T>*&, T&);
  28.     void print() const;
  29. private:
  30.     T *parent; //array of parents
  31.     node<T> *root;
  32.     int size;
  33.  
  34.     void deleteTree(node<T>* &);
  35.     void copy(node<T> * &, node<T>* const&) const;
  36.     void copyTree(ParentBT<T> const&);
  37.     void pr(const node<T>*) const;
  38. };
  39. template <class T>
  40. ParentBT<T>::ParentBT()
  41. {
  42.     root = NULL;
  43.     parent = new T[CAPACITY];
  44.     size = 0;
  45. }
  46. template <class T>
  47. ParentBT<T>::~ParentBT()
  48. {
  49.     deleteTree(root);
  50. }
  51. template <class T>
  52. ParentBT<T>::ParentBT(const ParentBT<T>& t)
  53. {
  54.     copyTree(t);
  55. }
  56. template <class T>
  57. ParentBT<T>& ParentBT<T>::operator=(const ParentBT<T>& t)
  58. {
  59.     if (this != &t)
  60.     {
  61.         deleteTree(root);
  62.         copyTree(t);
  63.     }
  64.     return *this;
  65. }
  66. template <class T>
  67. void ParentBT<T>::deleteTree(node<T>* &p)
  68. {
  69.     delete parent;
  70.     size = 0;
  71.     if (p)
  72.     {
  73.         deleteTree(p->left);
  74.         deleteTree(p->right);
  75.         delete p;
  76.         p = NULL;
  77.     }
  78. }
  79. template <class T>
  80. void ParentBT<T>::copyTree(ParentBT<T> const& t)
  81. {
  82.     copy(root, t.root);
  83.     for (int i = 0; i < size; i++)
  84.         parent[i] = t.parent[i];
  85. }
  86. template <class T>
  87. void ParentBT<T>::copy(node<T> * & p, node<T>* const & r) const
  88. {
  89.     p = NULL;
  90.     if (r)
  91.     {
  92.         p = new node<T>;
  93.         p->inf = r->inf;
  94.         copy(p->left, r->left);
  95.         copy(p->right, r->right);
  96.     }
  97. }
  98. template <class T>
  99. bool ParentBT<T>::empty() const
  100. {
  101.     return root == NULL;
  102. }
  103. template <class T>
  104. T ParentBT<T>::getRoot() const
  105. {
  106.     for (int i = 0; i < size; i++)
  107.         if (parent[i] == -1) return i;
  108. }
  109. template <class T>
  110. void ParentBT<T>::createTree(node<T>* & pos, T &x)
  111. {
  112.     pos = new node<T>;
  113.     pos->inf = x;
  114.     pos->left = NULL;
  115.     pos->right = NULL;
  116.     //cout << "root x: " << x << endl;
  117.     for (int i = 0; i < size; i++)
  118.     {
  119.         if (parent[i] == pos->inf && pos->left == NULL)
  120.         {
  121.             x = i;
  122.             //cout << "left x: " << x << endl;
  123.             createTree(pos->left, x);
  124.         }
  125.         else if (parent[i] == pos->inf && pos->left != NULL)
  126.         {
  127.             x = i;
  128.             //cout << "right x: " << x << endl;
  129.             createTree(pos->right, x);
  130.         }
  131.     }
  132. }
  133. template <class T>
  134. void ParentBT<T>::create()
  135. {
  136.     cout << "enter parents, max 2 equal parents (one parent can have only 2 children ): ";
  137.     char flag;
  138.     cout << "parent -> y/n"; cin >> flag;
  139.     while (flag == 'y')
  140.     {
  141.         cout << "parent: ";
  142.         cin >> parent[size];
  143.         if (parent[size] == parent[size - 1] && parent[size] == parent[size - 2] && size > 1)
  144.         {
  145.             cout << "there are allready two children wiht same parent\nparent -> y/n";
  146.             cin >> flag;
  147.             cout << "parent: ";
  148.             cin >> parent[size];
  149.         }
  150.         size++;
  151.         cout << "parent -> y/n";
  152.         cin >> flag;
  153.         if (size == CAPACITY - 1) break;
  154.     }
  155.     T x;
  156.     for (int i = 0; i < size; i++)
  157.         if (parent[i] == -1) x = i;
  158.     createTree(root,x);
  159. }
  160. template <class T>
  161. void ParentBT<T>::pr(const node<T>*p) const
  162. {
  163.     if (p)
  164.     {
  165.         pr(p->left);
  166.         cout << p->inf << " ";
  167.         pr(p->right);
  168.     }
  169. }
  170. template <class T>
  171. void ParentBT<T>::print() const
  172. {
  173.     pr(root);
  174. }
  175.  
  176.  
  177.  
  178. int main()
  179. {
  180.     ParentBT<int> binTree;
  181.     binTree.create();
  182.     cout << "root: " << binTree.getRoot() << endl;
  183.     binTree.print();
  184.  
  185.  
  186.     return 0;
  187.  
  188.     /*
  189.     1) we create an array of parents:
  190.         example:
  191.         parent[] = {1, 5, 5, 2, 2, -1, 3}
  192.           indexes-> #0,#1,#2,#3,#4,#5,#6
  193.           -1 -> root
  194.     2) create a tree(node) from the parent array:
  195.            5
  196.          /  \
  197.         1    2
  198.        /    / \
  199.       0    3   4
  200.           /
  201.          6
  202.     3) print tree(LRD): 0 1 5 6 3 2 4
  203.     */
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement