Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- class BinaryTree
- {
- private:
- template<typename T>
- class Node
- {
- public:
- Node* pRight;
- Node* pLeft;
- T data;
- Node(T data = T(), Node* pRight = nullptr, Node* pLeft = nullptr)
- {
- this->data = data;
- this->pRight = pRight;
- this->pLeft = pLeft;
- }
- };
- Node<T>* root;
- size_t size;
- public:
- BinaryTree();
- ~BinaryTree();
- Node* push(T data, Node* branch);
- void add(T data);
- void clear();
- int getSize() { return size; }
- T operator[] (const int index);
- };
- template<typename T> typename BinaryTree<T>::Node* BinaryTree<T>::push(T data, Node* branch)
- {
- if (root == nullptr)
- {
- root = new Node<T>(data);
- branch = root;
- }
- if (!branch)
- {
- branch = new Node<T>(data);
- }
- else
- {
- if (branch->data < data)
- branch->pRight = push(data, branch->pRight);
- if (branch->data > data)
- branch->pLeft = push(data, branch->pLeft);
- }
- size++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement