Advertisement
Foxyzboi

Untitled

Dec 25th, 2022
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <class T>
  4. class BinaryTree // Шаблонный класс дерева
  5. {
  6.     struct node { // Узел дерева
  7.         T data;
  8.         struct node* rightBr;
  9.         struct node* leftBr;
  10.     };
  11.  
  12. public:
  13.     BinaryTree(); // Конструктор дерева
  14.     ~BinaryTree(); // Деструктор дерева
  15.     void add(T dat); // Добавить элемент
  16.     void printTree(); // Вывести дерево на экран
  17.     int size(); // Возвращение размера дерева
  18.     bool lookup(T dat); // Поиск похожих значений дерева
  19. private:
  20.     struct node* root;
  21.     int treeSize;
  22.     void add(struct node** node, T dat);
  23.     bool lookup(struct node* node, T dat);
  24.     void printTree(struct node* node);
  25.     void deleteTree(struct node* node);
  26. };
  27.  
  28. template <class T>
  29. BinaryTree<T>::BinaryTree() {
  30.     this->root = nullptr;
  31.     this->treeSize = 0;
  32. }
  33.  
  34. template <class T>
  35. BinaryTree<T>::~BinaryTree() {
  36.     deleteTree(this->root);
  37. }
  38.  
  39. template <class T>
  40. int BinaryTree<T>::size() {
  41.     return this->treeSize;
  42. }
  43.  
  44. template <class T>
  45. void BinaryTree<T>::add(T dat) {
  46.     add(&(this->root), dat);
  47. }
  48.  
  49. template <class T>
  50. void BinaryTree<T>::add(struct node** node, T dat) {
  51.  
  52.     if (*node == nullptr) {
  53.         struct node* temp = new struct node;
  54.         temp->data = dat;
  55.         temp->leftBr = nullptr;
  56.         temp->rightBr = nullptr;
  57.         *node = temp;
  58.  
  59.         this->treeSize++;
  60.     }
  61.     else {
  62.         if (dat > (*node)->data) {
  63.             add(&(*node)->rightBr, dat);
  64.         }
  65.         else {
  66.             add(&(*node)->leftBr, dat);
  67.         }
  68.     }
  69. }
  70.  
  71. template <class T>
  72. void BinaryTree<T>::printTree() {
  73.     printTree(this->root);
  74.     std::cout << std::endl;
  75. }
  76.  
  77. template <class T>
  78. void BinaryTree<T>::printTree(struct node* node) {
  79.     if (node != nullptr) {
  80.         printTree(node->leftBr);
  81.         std::cout << node->data << ", ";
  82.         printTree(node->rightBr);
  83.     }
  84. }
  85.  
  86. template <class T>
  87. void BinaryTree<T>::deleteTree(struct node* node) {
  88.     if (node != nullptr) {
  89.         deleteTree(node->leftBr);
  90.         deleteTree(node->rightBr);
  91.         delete node;
  92.     }
  93. }
  94.  
  95. template <class T>
  96. bool BinaryTree<T>::lookup(T dat) {
  97.     return lookup(this->root, dat);
  98. }
  99.  
  100. template <class T>
  101. bool BinaryTree<T>::lookup(struct node* node, T dat) {
  102.     if (node == nullptr) {
  103.         return false;
  104.     }
  105.     else {
  106.         if (dat == node->data) {
  107.             return true;
  108.         }
  109.  
  110.         if (dat > node->data) {
  111.             return lookup(node->rightBr, dat);
  112.         }
  113.         else {
  114.             return lookup(node->leftBr, dat);
  115.         }
  116.     }
  117.  
  118. }
  119.  
  120. int main() {
  121.     setlocale(LC_ALL, "ru");
  122.     BinaryTree<int> tree;
  123.  
  124.     tree.add(5);
  125.     tree.add(4);
  126.     tree.add(7);
  127.     tree.add(10);
  128.     tree.add(1);
  129.     tree.add(2);
  130.  
  131.     tree.printTree();
  132.  
  133.     std::cout << "Размер дерева: " << tree.size() << std::endl;
  134.  
  135.     std::cout << "Содержит 7?: " << ((tree.lookup(7)) ? "true" : "false") << std::endl;
  136.  
  137.     std::cout << "Удаляем 7: " << std::endl;
  138.     tree.printTree();
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement