Advertisement
MaksNew

call of duty

Mar 9th, 2021
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. class BinaryTree
  6. {
  7. private:
  8.     template<typename T>
  9.     class Node
  10.     {
  11.     public:
  12.         Node* pRight;
  13.         Node* pLeft;
  14.         T data;
  15.         Node(T data = T(), Node* pRight = nullptr, Node* pLeft = nullptr)
  16.         {
  17.             this->data = data;
  18.             this->pRight = pRight;
  19.             this->pLeft = pLeft;
  20.         }
  21.     };
  22.     Node<T>* root;
  23.     size_t size;
  24. public:
  25.     BinaryTree();
  26.     ~BinaryTree();
  27.     void push(T data, Node*& node);
  28.     void clear();
  29.     int getSize() { return size; }
  30.     T operator[] (const int index);
  31. };
  32.  
  33. template<typename T>
  34. BinaryTree<T>::BinaryTree()
  35. {
  36.     size = 0;
  37.     root = nullptr;
  38. }
  39.  
  40. template<typename T>
  41. BinaryTree<T>::~BinaryTree()
  42. {
  43.     clear();
  44. }
  45.  
  46. template<typename T>
  47. void BinaryTree<T>::push(T data, Node*& node)
  48. {
  49.     if (root == nullptr)
  50.     {
  51.         root = new Node<T>(data);
  52.     }
  53.     else
  54.     {
  55.         Node<T>* current = this->root;
  56.         if (current->data < data)
  57.             push(data, current->pLeft);
  58.         else
  59.             push(data, current->pRight);
  60.     }
  61.     size++;
  62. }
  63.  
  64. template<typename T>
  65. void BinaryTree<T>::clear()
  66. {
  67.     while (size)
  68.     {
  69.         //pop_front();
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     cout << "Hello World!\n";
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement