Advertisement
dxvmxnd

Untitled

Nov 18th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. #define RIGHT 'R'
  6. #define LEFT 'L'
  7. #define ROOT '+'
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define MAX_CHOICE 8
  11. #define MIN_CHOICE 1
  12.  
  13. void setColor(int textColor, int bgColor) {
  14. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  15. SetConsoleTextAttribute(hConsole, (bgColor << 4) | textColor);
  16. }
  17.  
  18. enum chooseAction {
  19. addToTree = 1,
  20. drawTree,
  21. ARBTr,
  22. RABTr,
  23. ABRTr,
  24. firmwareF,
  25. delete,
  26. exitProg,
  27.  
  28.  
  29. };
  30.  
  31. struct TreeNode {
  32. int data;
  33. struct TreeNode *left;
  34. struct TreeNode *right;
  35. struct TreeNode* next;
  36. int isFirmware;
  37. };
  38. struct TreeNode* y = NULL;
  39.  
  40. struct TreeNode* createNode(const int data) {
  41. struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
  42. node->data = data;
  43. node->left = NULL;
  44. node->right = NULL;
  45. node->next = NULL;
  46. return node;
  47. }
  48.  
  49. struct TreeNode* findLeaf(struct TreeNode* root, const int data) {
  50. if ((root == NULL) || (root->data == data)) {
  51. return root;
  52. } else if (root->data > data) {
  53. return findLeaf(root->left, data);
  54.  
  55. } else {
  56. return findLeaf(root->right, data);
  57. }
  58.  
  59. }
  60.  
  61. struct TreeNode* insert(struct TreeNode* root, int data) {
  62. if (root == NULL) {
  63. root = createNode(data);
  64. } else {
  65. if (data < root->data){
  66. root->left = insert(root->left, data);
  67. } else {
  68. root->right = insert(root->right, data);
  69. }
  70. }
  71. return root;
  72. }
  73.  
  74. void ARB(const struct TreeNode* root) {
  75. if (root == NULL) {
  76. setColor(7, 0);
  77. printf("%d ", 0);
  78. return;
  79. }
  80. setColor(7, 0);
  81. printf("%d ", root->data);
  82. ARB(root->left);
  83.  
  84.  
  85. setColor(2, 0);
  86. printf("%d ", root->data);
  87.  
  88.  
  89. ARB(root->right);
  90. setColor(7, 0);
  91. printf("%d ", root->data);
  92.  
  93. }
  94.  
  95. void RAB( const struct TreeNode* root) {
  96. if (root == NULL) {
  97. setColor(7, 0);
  98. printf("%d ", 0);
  99. return;
  100. }
  101. setColor(2, 0);
  102. printf("%d ", root->data);
  103.  
  104. RAB(root->left);
  105. setColor(7, 0);
  106. printf("%d ", root->data);
  107.  
  108. RAB(root->right);
  109. setColor(7, 0);
  110. printf("%d ", root->data);
  111.  
  112. }
  113. void ABR(const struct TreeNode* root) {
  114. if (root == NULL) {
  115. setColor(7, 0);
  116. printf("%d ", 0);
  117. return;
  118. }
  119.  
  120. setColor(7, 0);
  121. printf("%d ", root->data);
  122. ABR(root->left);
  123.  
  124. setColor(7, 0);
  125. printf("%d ", root->data);
  126. ABR(root->right);
  127.  
  128. setColor(2, 0);
  129. printf("%d ", root->data);
  130. }
  131.  
  132.  
  133. void printTree(const struct TreeNode* root,const int level,const char side) {
  134. if (root == NULL) {
  135. return;
  136. }
  137. if (root->right != NULL) {
  138. printTree(root->right, level+1, RIGHT);
  139. }
  140. for (int i = 0; i < level; i++) {
  141. printf(" ");
  142. }
  143. printf("(%c)%d", side, root->data);
  144. if (root->next != NULL) {
  145. printf("(%d)", root->next->data);
  146. }printf("\n");
  147. if (root->left != NULL) {
  148. printTree(root->left, level+1, LEFT);
  149. }
  150.  
  151. }
  152.  
  153. void findElement(struct TreeNode* root, struct TreeNode** parent, struct TreeNode** target, const int data) {
  154. *target = root;
  155. *parent = NULL;
  156. while ((*target != NULL) && ((*target)->data != data)) {
  157. *parent = target;
  158. if (data < (*target)->data) {
  159. *target = (*target)->left;
  160. } else {
  161. *target = (*target)->right;
  162. }
  163. }
  164. }
  165.  
  166. struct TreeNode* deleteNode(struct TreeNode* root) {
  167. struct TreeNode* parent = NULL;
  168. struct TreeNode* target = NULL;
  169. int data;
  170. printf("Введи элемент который нужно удалить: ");
  171. scanf("%d", &data);
  172. findElement(root, &parent, &target, data);
  173. if (target == NULL) {
  174.  
  175. }
  176. if ((target->left != NULL) && (target->right != NULL)) {
  177. struct TreeNode* minParent = target;
  178. struct TreeNode* min = target->right;
  179. while (min->left != NULL) {
  180. minParent = min;
  181. min = min->left;
  182. }
  183. target->data = min->data;
  184.  
  185. if (minParent == target) {
  186. minParent->right = min->right;
  187. } else {
  188. minParent->left = min->right;
  189. }
  190. free(min);
  191. } else {
  192. struct TreeNode* child = NULL;
  193. if (target->left != NULL) {
  194. child = target->left;
  195. } else {
  196. child = target->right;
  197. }
  198. if (parent == NULL) {
  199. root = child;
  200. } else if (target == parent->left) {
  201. parent->left = child;
  202. } else {
  203. parent->right = child;
  204. }
  205. free(target);
  206. }
  207. }
  208.  
  209. enum chooseAction getAction() {
  210. int action = 0;
  211. int isNotCorrect = TRUE;
  212. char buff = '1';
  213. do {
  214. printf("Введите действие: ");
  215. scanf("%d", &action);
  216. isNotCorrect = ((action < MIN_CHOICE || action > MAX_CHOICE) && action != exitProg) || (buff = getchar()) != '\n';
  217. if (isNotCorrect) {
  218. printf("Ошибка\n\n");
  219. while ((buff = getchar()) != '\n' && buff != EOF);
  220. }
  221. } while(isNotCorrect);
  222. return action;
  223. }
  224.  
  225. struct TreeNode* getNewLeaf(struct TreeNode* root) {
  226. int newLeaf = 0;
  227. printf("Значение нового листа: ");
  228. scanf("%d", &newLeaf);
  229. printf("\n");
  230. if (root == NULL) {
  231. root = createNode(newLeaf);
  232. } else if (findLeaf(root, newLeaf) == NULL) {
  233. root = insert(root, newLeaf);
  234. } else {
  235. printf("Такой элемент уже существует\n");
  236. }
  237. return root;
  238. }
  239.  
  240. struct TreeNode* copyTree(struct TreeNode* root) {
  241. if (root == NULL) {
  242. return NULL;
  243. }
  244. struct TreeNode* newNode = createNode(root->data);
  245.  
  246. newNode->left = copyTree(root->left);
  247. newNode->right = copyTree(root->right);
  248.  
  249. return newNode;
  250. }
  251.  
  252. struct TreeNode* binaryFirmware(struct TreeNode* curr, struct TreeNode* prev) {
  253. if (curr == NULL) {
  254. return prev;
  255. }
  256. prev = binaryFirmware(curr->left, NULL);
  257. if (prev != NULL) {
  258. prev->next = curr;
  259. }
  260. prev = binaryFirmware(curr->right, curr);
  261. return prev;
  262. }
  263.  
  264. void firmwareTree(struct TreeNode* root) {
  265. struct TreeNode* last = binaryFirmware(root, NULL);
  266. root->isFirmware = TRUE;
  267. last->next = root;
  268. }
  269.  
  270.  
  271.  
  272. void printMenu (struct TreeNode* root) {
  273.  
  274. enum chooseAction action;
  275. do {
  276. printf("1) Добавить\n ");
  277. printf("2) Визуализация\n ");
  278. printf("3) ARB\n ");
  279. printf("4) RAB\n ");
  280. printf("5) ABR\n ");
  281. printf("6) Прошивка\n ");
  282. printf("7) Удалить\n ");
  283. printf("8) Выход\n ");
  284. action = getAction();
  285. switch (action) {
  286. case addToTree:
  287. root = getNewLeaf(root);
  288. break;
  289. case drawTree:
  290. printTree(root, 0, ROOT);
  291. break;
  292. case ARBTr: //лево корень право
  293. ARB(root);
  294. printf("\n");
  295. setColor(7,0);
  296. break;
  297. case RABTr:
  298. RAB(root);
  299. printf("\n");
  300. setColor(7,0);
  301. break;
  302. case ABRTr:
  303. ABR(root);
  304. printf("\n");
  305. setColor(7,0);
  306. break;
  307. case firmwareF:
  308. firmwareTree(root);
  309.  
  310. break;
  311. case delete:
  312. deleteNode(root);
  313. printf("\n");
  314.  
  315. }
  316. } while (action != exitProg);
  317. }
  318.  
  319. int main(void)
  320. {
  321. system("chcp 1251");
  322. struct TreeNode* root = NULL;
  323. root = insert(root, 23);
  324. insert(root, 45);
  325. insert(root, 34);
  326. insert(root, 12);
  327. insert(root, 15);
  328. insert(root, 6);
  329. insert(root, 56);
  330. printMenu(root);
  331. return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement