Advertisement
onezee

BinTree

May 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     int data;
  8.     Node* leftChild;
  9.     Node* rightChild;
  10.     Node(float fdata) {
  11.         data = fdata;
  12.         leftChild = nullptr;
  13.         rightChild = nullptr;
  14.     }
  15.     void displayNode() {
  16.         cout << data << ' ';
  17.     }
  18. };
  19.  
  20. struct Tree {
  21.     Node* root;
  22.  
  23.     Tree() {
  24.         root = nullptr;
  25.     }
  26.  
  27.     bool find(int data) {
  28.         if (root != NULL) {
  29.             Node* current = root;
  30.  
  31.             while (current->data != data) {
  32.                 if (data < current->data) current = current->leftChild;
  33.                 else current = current->rightChild;
  34.                 if (current == nullptr) return false;
  35.             }
  36.  
  37.             return true;
  38.         }
  39.         else return false;
  40.     }
  41.  
  42.     void insert(int data) {
  43.         Node* newNode = new Node(data);
  44.         if (root != NULL) {
  45.             Node* current = root;
  46.             Node* parent;
  47.  
  48.             while (true) {
  49.                 parent = current;
  50.                 if (data < current->data) {
  51.                     current = current->leftChild;
  52.                     if (current == NULL) {
  53.                         parent->leftChild = newNode;
  54.                         return;
  55.                     }
  56.                 }
  57.                 else {
  58.                     current = current->rightChild;
  59.                     if (current == NULL) {
  60.                         parent->rightChild = newNode;
  61.                         return;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         else {
  67.             root = newNode;
  68.         }
  69.     }
  70.  
  71.     bool del(int data) {
  72.         Node* current = root;
  73.         Node* parent = root;
  74.         bool isLeftChild = true;
  75.  
  76.         if (root != NULL) {
  77.             while (current->data != data) {
  78.                 parent = current;
  79.                 if (data < current->data) {
  80.                     isLeftChild = true;
  81.                     current = current->leftChild;
  82.                 }
  83.                 else {
  84.                     isLeftChild = false;
  85.                     current = current->rightChild;
  86.                 }
  87.                 if (current == NULL) return false;
  88.             }
  89.         }
  90.         else return false;
  91.  
  92.         // Нет потомков
  93.         if (current->leftChild == nullptr && current->rightChild == nullptr) {
  94.             if (current == root) root = nullptr;
  95.             else if (isLeftChild) parent->leftChild = nullptr;
  96.             else parent->rightChild = nullptr;
  97.         }
  98.         // Один потомок. Если нет левого потомка заменяем правым поддеревом
  99.         else if (current->leftChild == nullptr) {
  100.             if (current == root) root = current->rightChild;
  101.             else if (isLeftChild) parent->leftChild = current->rightChild;
  102.             else parent->rightChild = current->rightChild;
  103.         }
  104.         // Один потомок. Если нет правого потомка заменяем левым поддеревом
  105.         else if (current->rightChild == nullptr) {
  106.             if (current == root) root = current->leftChild;
  107.             else if (isLeftChild) parent->leftChild = current->leftChild;
  108.             else parent->rightChild = current->leftChild;
  109.         }
  110.         // Два потомка
  111.         else {
  112.             Node* success = getSuccessor(current);
  113.             if (current == root) root = success;
  114.             else if (isLeftChild) parent->leftChild = success;
  115.             else parent->rightChild = success;
  116.             success->leftChild = current->leftChild;
  117.         }
  118.         return true;
  119.     }
  120.  
  121.     // Обходы
  122.     // Симметричный
  123.     void inOrder(Node* localRoot) {
  124.         if (!localRoot)
  125.             return;
  126.         inOrder(localRoot->leftChild);
  127.         cout << localRoot->data << ' ';
  128.         inOrder(localRoot->rightChild);
  129.     }
  130.     // Прямой
  131.     void preOrder(Node* localRoot) {
  132.         if (!localRoot)
  133.             return;
  134.         cout << localRoot->data << ' ';
  135.         preOrder(localRoot->leftChild);
  136.         preOrder(localRoot->rightChild);
  137.     }
  138.     // Обратный
  139.     void postOrder(Node* localRoot) {
  140.         if (!localRoot)
  141.             return;
  142.         postOrder(localRoot->leftChild);
  143.         postOrder(localRoot->rightChild);
  144.         cout << localRoot->data << ' ';
  145.     }
  146.     void Order(string l) {
  147.         if (l == "in") inOrder(root);
  148.         else if (l == "pre") preOrder(root);
  149.         else if (l == "post") postOrder(root);
  150.         else return;
  151.     }
  152.  
  153.     // Поиск приемника для двух потомков
  154.     Node* getSuccessor(Node* delNode) {
  155.         Node* successorParent = delNode;
  156.         Node* successor = delNode;
  157.         Node* current = delNode->rightChild;
  158.         while (current != NULL) {
  159.             successorParent = successor;
  160.             successor = current;
  161.             current = current->leftChild;
  162.         }
  163.  
  164.         if (successor != delNode->rightChild) {
  165.             successorParent->leftChild = successor->rightChild;
  166.             successor->rightChild = delNode->rightChild;
  167.         }
  168.         return successor;
  169.     }
  170.  
  171.     int F2p() {
  172.         int c = 0;
  173.         c = prepreOrder(root, c);
  174.         return c;
  175.     }
  176.  
  177.     int prepreOrder(Node* localRoot, int c) {
  178.         if (!localRoot)
  179.             return c;
  180.         if (getSuccessor(localRoot)) c++;
  181.         prepreOrder(localRoot->leftChild, c);
  182.         prepreOrder(localRoot->rightChild, c);
  183.     }
  184.  
  185. };
  186.  
  187. void main() {
  188.     setlocale(LC_ALL, "Russian");
  189.  
  190.     Tree* tree = new Tree;
  191.  
  192.     for (int i = 0; i <= 10; i++) {
  193.         tree->insert(rand() % 100 - 50);
  194.     }
  195.  
  196.     cout << "Прямой обход :" << endl;
  197.     tree->Order("pre");
  198.     cout << "\nОбратный обход :" << endl;
  199.     tree->Order("post");
  200.     cout << "\nОбход в порядке возрастания :" << endl;
  201.     tree->Order("in");
  202.  
  203.     int r = rand() % 100 - 50;
  204.     bool fl = false;
  205.     while (!fl) {
  206.         if (tree->find(r)) {
  207.             tree->del(r);
  208.             fl = true;
  209.             cout << "\n\nПоиск и удаление элемента = " << r << " завершено" << endl;
  210.         }
  211.         else r = rand() % 100 - 50;
  212.     }
  213.  
  214.     cout << "\nКоличество узлов с двумя потомками = "<< tree->F2p() << endl;
  215.  
  216.     _getch();
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement