Advertisement
MaksNew

Untitled

Mar 10th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. template<typename T>
  2. class BinaryTree
  3. {
  4. private:
  5.     template<typename T>
  6.     class Node
  7.     {
  8.     public:
  9.         Node* pRight;
  10.         Node* pLeft;
  11.         T data;
  12.         Node(T data = T(), Node* pRight = nullptr, Node* pLeft = nullptr)
  13.         {
  14.             this->data = data;
  15.             this->pRight = pRight;
  16.             this->pLeft = pLeft;
  17.         }
  18.     };
  19.     Node<T>* root;
  20.     size_t size;
  21. public:
  22.     BinaryTree();
  23.     ~BinaryTree();
  24.     Node* push(T data, Node* branch);
  25.     void add(T data);
  26.     void clear();
  27.     int getSize() { return size; }
  28.     T operator[] (const int index);
  29. };
  30.  
  31. template<typename T> typename BinaryTree<T>::Node* BinaryTree<T>::push(T data, Node* branch)
  32. {
  33.     if (root == nullptr)
  34.     {
  35.         root = new Node<T>(data);
  36.         branch = root;
  37.     }
  38.     if (!branch)
  39.     {
  40.         branch = new Node<T>(data);
  41.     }
  42.     else
  43.     {
  44.         if (branch->data < data)
  45.             branch->pRight = push(data, branch->pRight);
  46.         if (branch->data > data)
  47.             branch->pLeft = push(data, branch->pLeft);
  48.     }
  49.     size++;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement