Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #ifndef NODE_H
  2. #define NODE_H
  3. #include <iostream>
  4.  
  5. template <typename T> class BSNode
  6. {
  7. public:
  8.  
  9. BSNode<T>(T data);
  10. BSNode<T>(const BSNode<T>& other);
  11. ~BSNode<T>();
  12. BSNode<T>& operator=(const BSNode<T>& other);
  13. bool isLeaf();
  14. void insertNode(BSNode<T>* newNode);
  15. BSNode<T>* search(T val);
  16.  
  17.  
  18. private:
  19. BSNode<T>* _left;
  20. BSNode<T>* _right;
  21. T _data;
  22. };
  23.  
  24. template <typename T> BSNode<T>::BSNode(T data)
  25. {
  26. this->_data = data;
  27. this->_left = 0;
  28. this->_right = 0;
  29. }
  30.  
  31. template <typename T> BSNode<T>::~BSNode()
  32. {
  33. if (this->_left)
  34. {
  35. delete this->_left;
  36. }
  37.  
  38. if (this->_right)
  39. {
  40. delete this->_right;
  41. }
  42. }
  43.  
  44. template <typename T> BSNode<T>::BSNode(const BSNode<T>& other)
  45. {
  46. this->_data = other._data;
  47.  
  48. if (other._left)
  49. {
  50. this->_left = new BSNode<T>(*(other._left));
  51. }
  52.  
  53. if (other._right)
  54. {
  55. this->_right = new BSNode<T>(*(other._right));
  56. }
  57. }
  58.  
  59. template <typename T> BSNode<T>& BSNode<T>::operator=(const BSNode<T>& other)
  60. {
  61. if (this->_data != other._data && this->_left != other._left && this->_right != other._right)
  62. {
  63. this->_data = 0;
  64.  
  65. if (this->_left)
  66. {
  67. delete this->_left;
  68. }
  69.  
  70. if (this->_right)
  71. {
  72. delete this->_right;
  73. }
  74.  
  75. this->_data = other._data;
  76.  
  77. if (other._left)
  78. {
  79. this->_left = new BSNode<T>(*(other._left));
  80. }
  81.  
  82. if (other._right)
  83. {
  84. this->_right = new BSNode<T>(*(other._right));
  85. }
  86. }
  87.  
  88. return(*this);
  89.  
  90. }
  91.  
  92. template <typename T> bool BSNode<T>::isLeaf()
  93. {
  94. if (this->_left && this->_right)
  95. {
  96. return true;
  97. }
  98.  
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104.  
  105. template <typename T> void BSNode<T>::insertNode(BSNode<T>* newNode)
  106. {
  107. if (newNode->_data > this->_data && this->_right != 0)
  108. {
  109. this->_right->insertNode(newNode);
  110. std::cout << "if1" << std::endl;
  111. }
  112.  
  113. else if (newNode->_data > this->_data && this->_right == 0)
  114. {
  115. this->_right = newNode;
  116. std::cout << "if2" << std::endl;
  117. }
  118.  
  119. else if (newNode->_data < this->_data && this->_left != 0)
  120. {
  121. this->_left->insertNode(newNode);
  122. std::cout << "if3" << std::endl;
  123. }
  124.  
  125. else if (newNode->_data < this->_data && this->_left == 0)
  126. {
  127. this->_left = newNode;
  128. std::cout << "if4" << std::endl;
  129. }
  130. }
  131.  
  132. template <typename T> BSNode<T>* BSNode<T>::search(T val)
  133. {
  134. if (this->_data > val && this->_right != 0)
  135. {
  136. this->search(val);
  137. }
  138.  
  139. else if (this->_data > val && this->_right == 0)
  140. {
  141. return(this);
  142. }
  143.  
  144. else if (this->_data < val && this->_left != 0)
  145. {
  146. this->search(val);
  147. }
  148.  
  149. else if (this->_data < val && this->_left == 0)
  150. {
  151. return(this);
  152. }
  153. }
  154. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement