Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <Windows.h>
  7. #define MAXWORD 100
  8. struct tnode {                // узел дерева
  9.     char* word;                  // указатель на строку (слово)
  10.     int count;                   // число вхождений
  11.     struct tnode* left;          // левый потомок
  12.     struct tnode* right;         // правый потомок
  13. };
  14. // Функция добавления узла к дереву
  15. struct tnode* addtree(struct tnode* p, char* w) {
  16.     int cond;
  17.     if (p == NULL) {
  18.         p = (struct tnode*)malloc(sizeof(struct tnode));
  19.         p->word = _strdup(w);
  20.         p->count = 1;
  21.         p->left = p->right = NULL;
  22.     }
  23.     else if ((cond = strcmp(w, p->word)) == 0)
  24.         p->count++;
  25.     else if (cond < 0)
  26.         p->left = addtree(p->left, w);
  27.     else
  28.         p->right = addtree(p->right, w);
  29.     return p;
  30. }
  31. // Функция удаления поддерева
  32. void freemem(tnode* tree) {
  33.     if (tree != NULL) {
  34.         freemem(tree->left);
  35.         freemem(tree->right);
  36.         free(tree);
  37.     }
  38. }
  39. // Функция вывода дерева
  40. void treeprint(tnode* p) {
  41.     if (p != NULL) {
  42.         treeprint(p->left);
  43.         printf("%s\n", p->word);
  44.         treeprint(p->right);
  45.     }
  46. }
  47. void del(tnode* p, char* str)
  48. {
  49.     if (p != NULL) {
  50.         if (strcmp(p->word, str) == 0)
  51.         {
  52.             freemem(p);
  53.             p = NULL;
  54.         }
  55.         else
  56.         {
  57.             del(p->left, str);
  58.             del(p->right, str);
  59.         }
  60.     }
  61.  
  62. }
  63. int main() {
  64.     SetConsoleCP(1251);
  65.     SetConsoleOutputCP(1251);
  66.     tnode* root = NULL;
  67.     char word[MAXWORD];
  68.     int created=0;
  69.     while (true)
  70.     {
  71.         system("cls");
  72.         printf("1-Создать\n2-Добавить элемент\n3-Удалить элемент\n4-Вывести\n5-Выход\n");
  73.         switch (_getch())
  74.         {
  75.         case '1':
  76.             created = 1;
  77.             printf("Дерево создано\n");
  78.             system("pause");
  79.             break;
  80.         case '2':
  81.             if (created)
  82.             {
  83.                 printf("Введите строку\n");
  84.                 scanf_s(" %s", word, MAXWORD);
  85.                 if (isalpha(word[0]))
  86.                     root = addtree(root, word);
  87.             }
  88.             else
  89.             {
  90.                 printf("Сначала создайте дерево\n");
  91.                 system("pause");
  92.             }
  93.             break;
  94.         case '3':
  95.             if (created)
  96.             {
  97.                 printf("Введите строку\n");
  98.                 scanf_s(" %s", word, MAXWORD);
  99.                 del(root, word);
  100.             }
  101.             else
  102.             {
  103.                 printf("Сначала создайте дерево\n");
  104.                 system("pause");
  105.             }
  106.             break;
  107.         case '4':
  108.             if (created)
  109.             {
  110.                 system("cls");
  111.                 treeprint(root);
  112.                 system("pause");
  113.             }
  114.             else
  115.             {
  116.                 printf("Сначала создайте дерево\n");
  117.                 system("pause");
  118.             }
  119.             break;
  120.         case '5':
  121.             return 0;
  122.         }
  123.     }
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement