Advertisement
venik2405

tree_c

Nov 19th, 2021
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <string>
  5. #include <conio.h>
  6. #include <vector>
  7. #include <iostream>
  8. #include "ConsoleApplicationTrash8.h"
  9. #include <queue>
  10. using namespace std;
  11. using std::cout; using std::cin;
  12. using std::endl; using std::string;
  13. using std::vector;
  14.  
  15. #define CMP_EQ(a, b) ((a) == (b))
  16. #define CMP_LT(a, b) ((a) < (b))
  17. #define CMP_GT(a, b) ((a) > (b))
  18.  
  19. typedef struct Node {
  20. int data;
  21. struct Node* left;
  22. struct Node* right;
  23. } Node;
  24.  
  25. int getSize() {
  26. int ind;
  27. int size;
  28. int flag;
  29. do {
  30. do {
  31. rewind(stdin);
  32. printf("Enter the size of the list\n");
  33. ind = scanf_s("%d", &size);
  34. if (ind != 1) {
  35. printf("Incorrect input!!!\n");
  36. }
  37. } while (ind != 1);
  38. if ((size > 20) || (size < 1))
  39. {
  40. printf("Enter the size between 1 and 20");
  41. flag = 1;
  42. }
  43. else {
  44. flag = 0;
  45. }
  46. } while (flag == 1);
  47. return size;
  48. }
  49.  
  50. int getNumber(int i) {
  51. int ind;
  52. int num;
  53. int flag;
  54. do {
  55. do {
  56. rewind(stdin);
  57. printf("Enter element number %d: ", i + 1);
  58. ind = scanf_s("%d", &num);
  59. if (ind != 1) {
  60. printf("Incorrect input!!!\n");
  61. }
  62. } while (ind != 1);
  63. if ((num > 50) || (num < -50))
  64. {
  65. printf("Enter the number, which is higher than -50 and lower than 50\n");
  66. flag = 1;
  67. }
  68. else {
  69. flag = 0;
  70. }
  71. } while (flag == 1);
  72. return num;
  73. }
  74.  
  75.  
  76. Node* getFreeNode(int value) {
  77. Node* tmp = (Node*)malloc(sizeof(Node));
  78. tmp->left = tmp->right = NULL;
  79. tmp->data = value;
  80. return tmp;
  81. }
  82.  
  83. void insert(Node** head, int value) {
  84. Node* tmp = NULL;
  85. Node* ins = NULL;
  86. int lvl = 1;
  87. if (*head == NULL) {
  88. *head = getFreeNode(value);
  89. return;
  90. }
  91.  
  92. tmp = *head;
  93. while (tmp) {
  94. if (CMP_GT(value, tmp->data)) {
  95. if (tmp->right) {
  96. tmp = tmp->right;
  97. lvl++;
  98. continue;
  99. }
  100. else {
  101. tmp->right = getFreeNode(value);
  102. return;
  103. }
  104. }
  105. else if (CMP_LT(value, tmp->data)) {
  106. if (tmp->left) {
  107. tmp = tmp->left;
  108. lvl++;
  109. continue;
  110. }
  111. else {
  112. tmp->left = getFreeNode(value);
  113. return;
  114. }
  115. }
  116. else {
  117. exit(2);
  118. }
  119. }
  120. }
  121.  
  122. Node* getNodeByValue(Node* root, int value) {
  123. while (root) {
  124. if (CMP_GT(root->data, value)) {
  125. root = root->left;
  126. continue;
  127. }
  128. else if (CMP_LT(root->data, value)) {
  129. root = root->right;
  130. continue;
  131. }
  132. else {
  133. return root;
  134. }
  135. }
  136. return NULL;
  137. }
  138.  
  139. Node * getMaxNode(Node * root) {
  140. while (root->right) {
  141. root = root->right;
  142. }
  143. return root;
  144. }
  145.  
  146. void inorder(Node* root)
  147. {
  148. if (root != NULL) {
  149. inorder(root->left);
  150. printf("%d ", root->data);
  151. inorder(root->right);
  152. }
  153. }
  154.  
  155. Node* insert(Node* node, int key)
  156. {
  157. if (node == NULL)
  158. return getFreeNode(key);
  159. if (key < node->data)
  160. node->left = insert(node->left, key);
  161. else
  162. node->right = insert(node->right, key);
  163. return node;
  164. }
  165.  
  166. Node* minValueNode(Node* node)
  167. {
  168. Node* current = node;
  169. while (current && current->left != NULL)
  170. current = current->left;
  171.  
  172. return current;
  173. }
  174.  
  175. Node* deleteNode(Node* root, int key)
  176. {
  177. if (root == NULL)
  178. return root;
  179. if (key < root->data)
  180. root->left = deleteNode(root->left, key);
  181. else if (key > root->data)
  182. root->right = deleteNode(root->right, key);
  183. else {
  184. if (root->left == NULL) {
  185. Node* temp = root->right;
  186. free(root);
  187. return temp;
  188. }
  189. else if (root->right == NULL) {
  190. Node* temp = root->left;
  191. free(root);
  192. return temp;
  193. }
  194. Node* temp = minValueNode(root->right);
  195. root->data = temp->data;
  196. root->right = deleteNode(root->right, temp->data);
  197. }
  198. return root;
  199. }
  200.  
  201. int findDigits(int num) {
  202. int n = 0;
  203. while (num != 0) {
  204. num = num / 10;
  205. n++;
  206. }
  207. return n;
  208. }
  209.  
  210. int findHeight(Node* root) {
  211. if (root == nullptr) return 0;
  212. return std::max(findHeight(root->left), findHeight(root->right)) + 1;
  213. }
  214.  
  215. void visualise(Node* root) {
  216. if (root == nullptr) return;
  217. int height = findHeight(root);
  218. std::queue<Node*> n;
  219. std::queue<Node*> e;
  220. n.push(root);
  221. int i;
  222. int digits = 1;
  223. Node* temp = nullptr;
  224. for (int level = 0; level < height; level++) {
  225. temp = n.front();
  226. e.push(n.front());
  227. n.pop();
  228. for (i = 0; i < pow(2, height - level) - 2; i++) std::cout << " ";
  229.  
  230. if (temp != nullptr) std::cout << temp->data;
  231. else std::cout << " ";
  232. for (int k = 1; k < pow(2, level); k++) {
  233. if (temp != NULL) digits = findDigits(temp->data);
  234. temp = n.front();
  235. e.push(n.front());
  236. n.pop();
  237. for (i = 0; i < pow(2, height + 1 - level) - digits; i++) std::cout << " ";
  238. digits = 1;
  239. if (temp != nullptr) std::cout << temp->data;
  240. else std::cout << " ";
  241. }
  242.  
  243. for (i = 0; i < pow(2, height - level) - 2; i++) std::cout << " ";
  244. std::cout << "\n";
  245.  
  246. if (level != height - 1) {
  247. for (i = 0; i < (pow(2, height - level) - 2 - pow(2, height - level - 2)); i++) std::cout << " ";
  248.  
  249. for (int k = 0; k < pow(2, level); k++) {
  250. temp = e.front();
  251. if (temp == NULL) temp = new Node();
  252. n.push(temp->left);
  253. if (temp->left != NULL) std::cout << "/";
  254. else std::cout << " ";
  255. for (i = 0; i < pow(2, height - level - 1) - 1; i++) std::cout << " ";
  256. n.push(temp->right);
  257. if (temp->right != NULL) std::cout << "\\";
  258. else std::cout << " ";
  259. for (i = 0; i < (pow(2, height - level + 1) - 1 - pow(2, height - level - 1)); i++) std::cout << " ";
  260. e.pop();
  261. }
  262.  
  263. for (i = 0; i < (pow(2, height - level) - 2 - pow(2, height - level - 2)); i++) std::cout << " ";
  264. std::cout << "\n";
  265. }
  266. }
  267. }
  268.  
  269. void main() {
  270. Node* root = NULL;
  271. int size = 0;
  272. int num = 0;
  273. size = getSize();
  274. for (int i = 0; i < size; i++) {
  275. num = getNumber(i);
  276. insert(&root, num);
  277. }
  278. visualise(root);
  279. printf("Max node is %d\n", getMaxNode(root)->data);
  280. deleteNode(root, 2);
  281. visualise(root);
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement