daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Dec 17th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include <math.h>
  3.  
  4. template<typename T>
  5. class ChainTree
  6. {
  7. public:
  8.     ChainTree();
  9.     ChainTree(int size);
  10.     ~ChainTree();
  11.     ChainTree(ChainTree<T> const&);
  12.     ChainTree& operator =(ChainTree<T> const&);
  13.  
  14.     //prints left root right
  15.     void pr(int i = 0);
  16.     //prints left tree
  17.     void prLeft();
  18.     //prints right tree
  19.     void prRight();
  20.  
  21.     T GetRoot();
  22. private:
  23.     int size;
  24.     T* a;
  25.     int* b;
  26.     int* c;
  27.     int rootIndex;
  28.     void CreateChainTree(int i);
  29.     void DeleteTree();
  30.     void CopyTree(ChainTree<T> const&);
  31. };
  32.  
  33. int main()
  34. {
  35.     int size;
  36.     std::cout << "Vyvedete visochinata na durvoto: " << std::endl;
  37.     std::cin >> size;
  38.     ChainTree<int> ct(size);
  39.     ChainTree<int>ct1;
  40.     ct1 = ct;
  41.     ct1.pr();
  42.     std::cout << std::endl;
  43.     ct1.prLeft();
  44.     std::cout << std::endl;
  45.     ct1.prRight();
  46.     system("pause");
  47.     return 0;
  48. }
  49.  
  50. template<typename T>
  51. ChainTree<T>::ChainTree()
  52. {
  53. }
  54.  
  55. template<typename T>
  56. inline ChainTree<T>::ChainTree(int size)
  57. {
  58.     this->size = (int)pow(2, size);
  59.     a = new T[this->size];
  60.     b = new int[this->size];
  61.     c = new int[this->size];
  62.     for (size_t i = 0; i < this->size; i++)
  63.     {
  64.         a[i] = T();
  65.         b[i] = -1;
  66.         c[i] = -1;
  67.     }
  68.     int i = 0;
  69.     CreateChainTree(i);
  70. }
  71.  
  72. template<typename T>
  73. ChainTree<T>::~ChainTree()
  74. {
  75.     DeleteTree();
  76. }
  77.  
  78. template<typename T>
  79. ChainTree<T>::ChainTree(ChainTree<T> const & other)
  80. {
  81.     CopyTree(other);
  82. }
  83.  
  84. template<typename T>
  85. ChainTree<T> & ChainTree<T>::operator=(ChainTree<T> const & other)
  86. {
  87.     if (this != &other)
  88.     {
  89.         DeleteTree();
  90.         CopyTree(other);
  91.     }
  92.     return *this;
  93. }
  94.  
  95. template<typename T>
  96. void ChainTree<T>::pr(int i = 0)
  97. {
  98.     if (i > size)
  99.     {
  100.         return;
  101.     }
  102.     if (b[i] != -1)
  103.     {
  104.         pr(2 * i + 1);
  105.     }
  106.     else
  107.     {
  108.         std::cout << "none ";
  109.     }
  110.     if (i == rootIndex)
  111.     {
  112.         std::cout << a[rootIndex] << " ";
  113.     }
  114.     else
  115.     {
  116.         std::cout << a[i] << " ";
  117.     }
  118.     if (c[i] != -1)
  119.     {
  120.         pr(2 * i + 2);
  121.     }
  122.     else
  123.     {
  124.         std::cout << "none ";
  125.     }
  126. }
  127.  
  128. template<typename T>
  129. void ChainTree<T>::prLeft()
  130. {
  131.     if (b[rootIndex] == -1)
  132.     {
  133.         std::cout << "none";
  134.         return;
  135.     }
  136.     pr(2 * rootIndex + 1);
  137. }
  138.  
  139. template<typename T>
  140. void ChainTree<T>::prRight()
  141. {
  142.     if (c[rootIndex] == -1)
  143.     {
  144.         std::cout << "none";
  145.         return;
  146.     }
  147.     pr(2 * rootIndex + 2);
  148. }
  149.  
  150. template<typename T>
  151. T ChainTree<T>::GetRoot()
  152. {
  153.     return a[rootIndex];
  154. }
  155.  
  156. template<typename T>
  157. void ChainTree<T>::CreateChainTree(int i)
  158. {
  159.     if (i > size)
  160.     {
  161.         return;
  162.     }
  163.     T x; char ch;
  164.     std::cout << "root: " << std::endl;
  165.     std::cin >> x;
  166.  
  167.     if (i == 0)
  168.         rootIndex = i;
  169.     a[i] = x;
  170.  
  171.     std::cout << "left BinTree of: " << x << " y/n? ";
  172.     std::cin >> ch;
  173.     if (ch == 'y')
  174.     {
  175.         b[i] = 2*i+1;
  176.         CreateChainTree(2 * i + 1);
  177.     }
  178.     else
  179.     {
  180.         b[i] = -1;
  181.     }
  182.  
  183.     std::cout << "right BinTree of: " << x << " y/n? ";
  184.     std::cin >> ch;
  185.     if (ch == 'y')
  186.     {
  187.         c[i] = 2 * i + 2;
  188.         CreateChainTree(2 * i + 2);
  189.     }
  190.     else
  191.     {
  192.         c[i] = -1;
  193.     }
  194. }
  195.  
  196. template<typename T>
  197. void ChainTree<T>::DeleteTree()
  198. {
  199.     delete a;
  200.     delete b;
  201.     delete c;
  202. }
  203.  
  204. template<typename T>
  205. void ChainTree<T>::CopyTree(ChainTree<T> const & other)
  206. {
  207.     this->rootIndex = other.rootIndex;
  208.     this->size = other.size;
  209.     this->a = new T[other.size];
  210.     this->b = new int[other.size];
  211.     this->c = new int[other.size];
  212.     for (size_t i = 0; i < other.size; i++)
  213.     {
  214.         this->a[i] = other.a[i];
  215.         this->b[i] = other.b[i];
  216.         this->c[i] = other.c[i];
  217.     }
  218. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top