akadjoker

Untitled

Jul 11th, 2025
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using std::cout;
  5. using std::endl;
  6.  
  7. struct Node
  8. {
  9.     int value{0};
  10.     Node* left{nullptr};
  11.     Node* right{ nullptr };
  12. };
  13.  
  14.  
  15. class Lista
  16. {
  17.  
  18.     Node* root{ nullptr };
  19.     int count{ 0 };
  20.  
  21.     public:
  22.  
  23.     void add(int v)
  24.     {
  25.         Node* node = new Node();
  26.         node->value = v;
  27.         count++;
  28.         if (!root)
  29.         {
  30.             root = node;
  31.             return;
  32.         }
  33.         _add(root,node);
  34.     }
  35.  
  36.  
  37.     void print()
  38.     {
  39.         if(root)
  40.         {
  41.             _print(root);
  42.         }
  43.     }
  44.  
  45.     bool find(int value)
  46.     {
  47.         if (!root) return false;
  48.         return _find(root,value);
  49.     }
  50.  
  51.     ~Lista()
  52.     {
  53.         _destroy(root);
  54.     }
  55.  
  56.    
  57.     private:
  58.    
  59.     void _destroy(Node* node) {
  60.         if (!node) return;
  61.         _destroy(node->left);
  62.         _destroy(node->right);
  63.         delete node;
  64.     }
  65.     bool _find(Node* root,int value)
  66.     {
  67.         if (!root) return false;
  68.         if (root->value == value) return true;
  69.         if (root->value > value) return _find(root->left, value);
  70.         if (root->value < value) return _find(root->right, value);
  71.         return false;
  72.     }
  73.  
  74. void _print(Node *root)
  75. {
  76.     if (root)
  77.     {
  78.         if (root->left) _print(root->left);
  79.         cout << root->value << endl;
  80.         if (root->right) _print(root->right);
  81.     }
  82. }
  83.  
  84.  
  85.     void _add(Node* _root, Node* node)
  86.     {
  87.         if (_root->value > node->value)
  88.         {
  89.             if (!_root->left)
  90.             {
  91.                 _root->left = node;
  92.                 return;
  93.             }
  94.             _add(_root->left, node);
  95.         }
  96.         else
  97.         {
  98.             if (!_root->right)
  99.             {
  100.                 _root->right = node;
  101.                 return;
  102.             }
  103.             _add(_root->right, node);
  104.         }
  105.         }
  106. };
  107.  
  108.  
  109. int main()
  110. {
  111.  
  112.     Lista lista;
  113.  
  114.  
  115.     lista.add(0);
  116.     lista.add(1);
  117.     lista.add(2);
  118.     lista.add(3);
  119.  
  120.     lista.add(-3);
  121.     lista.add(-2);
  122.     lista.add(-1);
  123.  
  124.  
  125.     if(lista.find(5))
  126.     {
  127.         cout << "FOUND" << endl;
  128.     } else
  129.     {
  130.         cout << "NOT FOUND" << endl;
  131.     }
  132.  
  133.  
  134.     lista.print();
  135.  
  136.     // lista.print();
  137.  
  138.     // lista.clear();
  139.     return 0;
  140. }
  141.  
Tags: tree
Advertisement
Add Comment
Please, Sign In to add comment