Advertisement
rado_dimitrov66

Untitled

May 21st, 2024
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int key;
  6.     Node* left;
  7.     Node* right;
  8. } *root = NULL; // Външен указател
  9.  
  10. // Функция за създаване на нов възел
  11. Node* createNode(int key) {
  12.     Node* newNode = new Node();
  13.     newNode->key = key;
  14.     newNode->left = NULL;
  15.     newNode->right = NULL;
  16.     return newNode;
  17. }
  18.  
  19. // Функция за добавяне на нов възел в дървото
  20. Node* insertNode(Node* root, int key) {
  21.     if (root == NULL) {
  22.         return createNode(key);
  23.     }
  24.  
  25.     if (key < root->key) {
  26.         root->left = insertNode(root->left, key);
  27.     } else {
  28.         root->right = insertNode(root->right, key);
  29.     }
  30.     return root;
  31. }
  32.  
  33. // Функция за намиране на минималния възел в дясното поддърво
  34. Node* findMin(Node* root) {
  35.     while (root->left != NULL) {
  36.         root = root->left;
  37.     }
  38.     return root;
  39. }
  40.  
  41. // Функция за изтриване на възел от дървото
  42. Node* deleteNode(Node* root, int key) {
  43.     if (root == NULL) {
  44.         return root;
  45.     }
  46.  
  47.     if (key < root->key) {
  48.         root->left = deleteNode(root->left, key);
  49.     } else if (key > root->key) {
  50.         root->right = deleteNode(root->right, key);
  51.     } else {
  52.         // Възел с един наследник или без наследници
  53.         if (root->left == NULL) {
  54.             Node* temp = root->right;
  55.             delete root;
  56.             return temp;
  57.         } else if (root->right == NULL) {
  58.             Node* temp = root->left;
  59.             delete root;
  60.             return temp;
  61.         }
  62.  
  63.         // Възел с два наследника
  64.         Node* temp = findMin(root->right);
  65.         root->key = temp->key;
  66.         root->right = deleteNode(root->right, temp->key);
  67.     }
  68.     return root;
  69. }
  70.  
  71. // Функция за принтиране на дървото (in-order)
  72. void inorderPrint(Node* root) {
  73.     if (root != NULL) {
  74.         inorderPrint(root->left);
  75.         cout << root->key << " ";
  76.         inorderPrint(root->right);
  77.     }
  78. }
  79.  
  80. // Функция за преброяване на възлите с по един наследник
  81. int countNodesWithOneChild(Node* root) {
  82.     if (root == NULL) {
  83.         return 0;
  84.     }
  85.  
  86.     int count = 0;
  87.     if ((root->left == NULL && root->right != NULL) || (root->left != NULL && root->right == NULL)) {
  88.         count = 1;
  89.     }
  90.  
  91.     return count + countNodesWithOneChild(root->left) + countNodesWithOneChild(root->right);
  92. }
  93.  
  94. // Основно меню
  95. void menu() {
  96.     int choice, key;
  97.  
  98.     while (true) {
  99.         cout << "\nМеню:\n";
  100.         cout << "1. Добавяне на елемент\n";
  101.         cout << "2. Изтриване на елемент\n";
  102.         cout << "3. Принтиране на всички елементи\n";
  103.         cout << "4. Преброяване на възлите с по един наследник\n";
  104.         cout << "5. Изход\n";
  105.         cout << "Вашият избор: ";
  106.         cin >> choice;
  107.  
  108.         switch (choice) {
  109.             case 1:
  110.                 cout << "Въведете ключ за добавяне: ";
  111.                 cin >> key;
  112.                 root = insertNode(root, key);
  113.                 break;
  114.             case 2:
  115.                 cout << "Въведете ключ за изтриване: ";
  116.                 cin >> key;
  117.                 root = deleteNode(root, key);
  118.                 break;
  119.             case 3:
  120.                 cout << "Елементите на дървото са: ";
  121.                 inorderPrint(root);
  122.                 cout << endl;
  123.                 break;
  124.             case 4:
  125.                 cout << "Броят на възлите с по един наследник е: " << countNodesWithOneChild(root) << endl;
  126.                 break;
  127.             case 5:
  128.                 return;
  129.             default:
  130.                 cout << "Невалиден избор. Опитайте отново.\n";
  131.         }
  132.     }
  133. }
  134.  
  135. int main() {
  136.     menu();
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement