Advertisement
ilnazEPTA

Untitled

May 12th, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Tree_node
  6. {
  7. int val;
  8. Tree_node* left_child;
  9. Tree_node* right_child;
  10. Tree_node(int val)
  11. {
  12. this->val = val;
  13. left_child = right_child = nullptr;
  14. }
  15. Tree_node(){}
  16. };
  17.  
  18.  
  19. class Binary_tree
  20. {
  21. private:
  22. Tree_node* root;
  23. int Size;
  24. int height(Tree_node* currRoot)
  25. {
  26. if (currRoot == nullptr)
  27. return 0;
  28. int lh = height(currRoot->left_child);
  29. int rh = height(currRoot->right_child);
  30. return 1 + (lh > rh ? lh : rh);
  31. }
  32.  
  33. void print(Tree_node* currRoot,int a)
  34. {
  35. if (currRoot == nullptr)
  36. {
  37. for (int i = 0; i < a + 1; i++) cout << " ";
  38.  
  39. cout << "@" << endl;
  40.  
  41. return;
  42. }
  43. print(currRoot->right_child, a + 4);
  44.  
  45. for (int i = 0; i < a; i++)
  46. cout << " ";
  47. cout << (currRoot->val) << endl;
  48.  
  49. print(currRoot->left_child, a + 4);
  50.  
  51. }
  52.  
  53. void print_preorder(Tree_node* currRoot)
  54. {
  55. if (currRoot == 0)
  56. return;
  57. cout << currRoot->val << ' ';
  58. print_preorder(currRoot->left_child);
  59. print_preorder(currRoot->right_child);
  60. }
  61.  
  62. void print_inorder(Tree_node* currRoot)
  63. {
  64. if (currRoot == 0)
  65. return;
  66. print_inorder(currRoot->left_child);
  67. cout << currRoot->val << ' ';
  68. print_inorder(currRoot->right_child);
  69. }
  70.  
  71. void print_postorder(Tree_node* currRoot)
  72. {
  73. if (currRoot == 0)
  74. return;
  75. print_postorder(currRoot->right_child);
  76. print_postorder(currRoot->left_child);
  77. cout << currRoot->val << ' ';
  78. }
  79.  
  80. void remove(Tree_node*& currRoot, int val)
  81. {
  82. if (currRoot == nullptr)
  83. return;
  84. if (currRoot->val != val)
  85. {
  86. if (currRoot->val > val)
  87. remove(currRoot->left_child, val);
  88. else
  89. remove(currRoot->right_child, val);
  90. }
  91. else
  92. {
  93. Tree_node* help = currRoot;
  94. if (currRoot->left_child == nullptr)
  95. {
  96. currRoot = currRoot->right_child;
  97. delete help;
  98. }
  99. else
  100. {
  101. if (currRoot->right_child == nullptr)
  102. {
  103. currRoot = currRoot->left_child;
  104. delete help;
  105. }
  106. else
  107. {
  108. Tree_node* right = currRoot->right_child;
  109. if (right->left_child == nullptr)
  110. {
  111. right->left_child = currRoot->left_child;
  112. currRoot = right;
  113. delete help;
  114. }
  115. else
  116. {
  117. Tree_node* prev = right, *act = prev->left_child;
  118. while (act->left_child != nullptr)
  119. {
  120. prev = prev->left_child;
  121. act = act->left_child;
  122. }
  123. currRoot->val = act->val;
  124. prev->left_child = act->right_child;
  125. delete act;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. public:
  132. Binary_tree()
  133. {
  134. root = nullptr;
  135. Size = 0;
  136. }
  137.  
  138. Binary_tree(int val)
  139. {
  140. root->val = val;
  141. root->left_child = root->right_child = nullptr;
  142. Size = 1;
  143. }
  144.  
  145. ~Binary_tree()
  146. {
  147. destroy(root);
  148. }
  149.  
  150. void destroy(Tree_node* currRoot)
  151. {
  152. if (currRoot == nullptr)
  153. return;
  154. destroy(currRoot->left_child);
  155. destroy(currRoot->right_child);
  156. delete currRoot;
  157. }
  158.  
  159. bool add(int val)
  160. {
  161. if (root == nullptr)
  162. {
  163. root = new Tree_node(val);
  164. Size++;
  165. return true;
  166. }
  167. Tree_node* curr = root;
  168. while (true)
  169. {
  170. if (curr->val == val)
  171. return false;
  172. else
  173. if (curr->val > val)
  174. {
  175. if (curr->left_child == nullptr)
  176. {
  177. curr->left_child = new Tree_node(val);
  178. Size++;
  179. return true;
  180. }
  181. else
  182. curr = curr->left_child;
  183. }
  184. else
  185. if (curr->right_child == nullptr)
  186. {
  187. curr->right_child = new Tree_node(val);
  188. Size++;
  189. return true;
  190. }
  191. else
  192. curr = curr->right_child;
  193.  
  194. }
  195. }
  196.  
  197. int size()
  198. {
  199. return Size;
  200. }
  201.  
  202. int height()
  203. {
  204. return height(root);
  205. }
  206.  
  207. void print()
  208. {
  209. print(root, 0);
  210. cout << endl;
  211. }
  212.  
  213. void print_preorder()
  214. {
  215. print_preorder(root);
  216. cout << endl;
  217. }
  218.  
  219. void print_inorder()
  220. {
  221. print_inorder(root);
  222. cout << endl;
  223. }
  224.  
  225. void print_postorder()
  226. {
  227. print_postorder(root);
  228. cout << endl;
  229. }
  230.  
  231. void remove(int val)
  232. {
  233. remove(root, val);
  234. }
  235. };
  236.  
  237.  
  238. int main()
  239. {
  240. int a[] = {5,4,9,1,3,2,12,7,6,8};
  241. Binary_tree tree;
  242. for (int i = 0; i < 9; i++)
  243. tree.add(a[i]);
  244. tree.print_inorder();
  245. tree.height();
  246. tree.add(15);
  247. tree.print_inorder();
  248. tree.remove(15);
  249. tree.remove(5);
  250. tree.add(18);
  251. tree.print_inorder();
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement