Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef NODE_H
- #define NODE_H
- #include <iostream>
- template <typename T> class BSNode
- {
- public:
- BSNode<T>(T data);
- BSNode<T>(const BSNode<T>& other);
- ~BSNode<T>();
- BSNode<T>& operator=(const BSNode<T>& other);
- bool isLeaf();
- void insertNode(BSNode<T>* newNode);
- BSNode<T>* search(T val);
- private:
- BSNode<T>* _left;
- BSNode<T>* _right;
- T _data;
- };
- template <typename T> BSNode<T>::BSNode(T data)
- {
- this->_data = data;
- this->_left = 0;
- this->_right = 0;
- }
- template <typename T> BSNode<T>::~BSNode()
- {
- if (this->_left)
- {
- delete this->_left;
- }
- if (this->_right)
- {
- delete this->_right;
- }
- }
- template <typename T> BSNode<T>::BSNode(const BSNode<T>& other)
- {
- this->_data = other._data;
- if (other._left)
- {
- this->_left = new BSNode<T>(*(other._left));
- }
- if (other._right)
- {
- this->_right = new BSNode<T>(*(other._right));
- }
- }
- template <typename T> BSNode<T>& BSNode<T>::operator=(const BSNode<T>& other)
- {
- if (this->_data != other._data && this->_left != other._left && this->_right != other._right)
- {
- this->_data = 0;
- if (this->_left)
- {
- delete this->_left;
- }
- if (this->_right)
- {
- delete this->_right;
- }
- this->_data = other._data;
- if (other._left)
- {
- this->_left = new BSNode<T>(*(other._left));
- }
- if (other._right)
- {
- this->_right = new BSNode<T>(*(other._right));
- }
- }
- return(*this);
- }
- template <typename T> bool BSNode<T>::isLeaf()
- {
- if (this->_left && this->_right)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- template <typename T> void BSNode<T>::insertNode(BSNode<T>* newNode)
- {
- if (newNode->_data > this->_data && this->_right != 0)
- {
- this->_right->insertNode(newNode);
- std::cout << "if1" << std::endl;
- }
- else if (newNode->_data > this->_data && this->_right == 0)
- {
- this->_right = newNode;
- std::cout << "if2" << std::endl;
- }
- else if (newNode->_data < this->_data && this->_left != 0)
- {
- this->_left->insertNode(newNode);
- std::cout << "if3" << std::endl;
- }
- else if (newNode->_data < this->_data && this->_left == 0)
- {
- this->_left = newNode;
- std::cout << "if4" << std::endl;
- }
- }
- template <typename T> BSNode<T>* BSNode<T>::search(T val)
- {
- if (this->_data > val && this->_right != 0)
- {
- this->search(val);
- }
- else if (this->_data > val && this->_right == 0)
- {
- return(this);
- }
- else if (this->_data < val && this->_left != 0)
- {
- this->search(val);
- }
- else if (this->_data < val && this->_left == 0)
- {
- return(this);
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement