Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #ifndef BST_H
  2. #define BST_H
  3.  
  4. #include<string>
  5. #include<exception>
  6. #include<cassert>
  7.  
  8.  
  9. template<class DataType>
  10. struct Node
  11. {
  12. Node();
  13.  
  14. DataType Item; //assumes a copy constructor has been written for a DataType
  15. Node* Left;
  16. Node* Right;
  17. };
  18.  
  19. template<class DataType>
  20. Node<DataType>::Node()
  21. {
  22. Left = NULL;
  23. Right = NULL;
  24. }
  25.  
  26. template<class DataType>
  27. class BST
  28. {
  29. typedef void(*FunctionType)(DataType& Item);
  30.  
  31. public:
  32. BST();
  33. ~BST();
  34. bool IsEmpty();
  35. void Insert(const DataType& NewItem);
  36. void Delete(const DataType& SearchItem);
  37. bool Retrieve(const DataType& SearchItem, DataType& Result);
  38. void PreorderTraverse(FunctionType Visit);
  39. void InorderTraverse(FunctionType Visit);
  40. void PostorderTraverse(FunctionType Visit);
  41. private:
  42. Node<DataType>* Root;
  43. void Destroy(Node<DataType>*& pNode); //called in the destructor
  44. void InsertItem(Node<DataType>*& pNode, const DataType& NewItem);
  45. void DeleteItem(Node<DataType>*& pNode, const DataType& SearchItem);
  46. void DeleteNodeItem(Node<DataType>*& pNode);
  47. void DeleteLeftMost(Node<DataType>*& pNode, DataType& Item);
  48. bool RetrieveItem(Node<DataType>* pNode, const DataType& SearchItem, DataType& Item);
  49. void Preorder(Node<DataType>* pNode, FunctionType Visit);
  50. void Inorder(Node<DataType>* pNode, FunctionType Visit);
  51. void Postorder(Node<DataType>* pNode, FunctionType Visit);
  52. };
  53.  
  54. template<class DataType>
  55. BST<DataType>::BST()
  56. {
  57. Root = NULL;
  58. }
  59.  
  60. template<class DataType>
  61. BST<DataType>::~BST()
  62. {
  63. Destroy(Root);
  64. }
  65.  
  66. template<class DataType>
  67. bool BST<DataType>::IsEmpty()
  68. {
  69. return (Root == NULL);
  70. }
  71.  
  72. template<class DataType>
  73. void BST<DataType>::Insert(const DataType& NewItem)
  74. {
  75. InsertItem(Root, NewItem);
  76. }
  77.  
  78. template<class DataType>
  79. void BST<DataType>::Delete(const DataType& SearchItem)
  80. {
  81. DeleteItem(Root, SearchItem);
  82. }
  83.  
  84. template<class DataType>
  85. bool BST<DataType>::Retrieve(const DataType& SearchItem, DataType& Result)
  86. {
  87. return RetrieveItem(Root, SearchItem, Result);
  88. }
  89.  
  90. template<class DataType>
  91. void BST<DataType>::PreorderTraverse(FunctionType Visit)
  92. {
  93. Preorder(Root, Visit);
  94. }
  95.  
  96. template<class DataType>
  97. void BST<DataType>::InorderTraverse(FunctionType Visit)
  98. {
  99. Inorder(Root, Visit);
  100. }
  101.  
  102. template<class DataType>
  103. void BST<DataType>::PostorderTraverse(FunctionType Visit)
  104. {
  105. Postorder(Root, Visit);
  106. }
  107.  
  108. template<class DataType>
  109. void BST<DataType>::Destroy(Node<DataType>*& pNode) //called in the destructor
  110. {
  111. if (pNode->Left != NULL)
  112. {
  113. Destroy(pNode->Left);
  114. pNode->Left = NULL;
  115. }
  116. if (pNode->Right != NULL)
  117. {
  118. Destroy(pNode->Right);
  119. pNode->Right = NULL;
  120. }
  121. if (pNode != NULL)
  122. {
  123. delete pNode;
  124. pNode = NULL;
  125. }
  126. else
  127. {
  128. assert(false);
  129. }
  130.  
  131. }
  132.  
  133. template<class DataType>
  134. void BST<DataType>::InsertItem(Node<DataType>*& pNode, const DataType& NewItem)
  135. {
  136. if (pNode == NULL)
  137. {
  138. pNode = new Node<DataType>();
  139. pNode->Item = NewItem;
  140. }
  141. else if (NewItem < pNode->Item)
  142. {
  143. InsertItem(pNode->Left, NewItem);
  144. }
  145. else if (NewItem > pNode->Item)
  146. {
  147. InsertItem(pNode->Right, NewItem);
  148. }
  149.  
  150. }
  151.  
  152. template<class DataType>
  153. inline void BST<DataType>::DeleteItem(Node<DataType>*& pNode, const DataType & SearchItem)
  154. {
  155. }
  156.  
  157. template<class DataType>
  158. inline void BST<DataType>::DeleteNodeItem(Node<DataType>*& pNode)
  159. {
  160. }
  161.  
  162. template<class DataType>
  163. inline void BST<DataType>::DeleteLeftMost(Node<DataType>*& pNode, DataType & Item)
  164. {
  165. }
  166.  
  167. //operators == and > must be defined for DataType
  168. template<class DataType>
  169. bool BST<DataType>::RetrieveItem(Node<DataType>* pNode,
  170. const DataType& SearchItem,
  171. DataType& Item)
  172. {
  173. bool result = false;
  174. if (pNode == NULL)
  175. {
  176. result = false;
  177. }
  178. else if (SearchItem == pNode->Item)
  179. {
  180. result = true;
  181. Item = pNode->Item;
  182. }
  183. else if (SearchItem > pNode->Item)
  184. {
  185. result = RetrieveItem(pNode->Right, SearchItem, Item);
  186. }
  187. else
  188. {
  189. result = RetrieveItem(pNode->Left, SearchItem, Item);
  190. }
  191. return result;
  192. }
  193.  
  194. template<class DataType>
  195. void BST<DataType>::Preorder(Node<DataType>* pNode, FunctionType Visit)
  196. {
  197. if (pNode != NULL)
  198. {
  199. Visit(pNode->Item);
  200. }
  201. if (pNode->Left != NULL)
  202. {
  203. Preorder(pNode->Left, Visit);
  204. }
  205. if (pNode->Right != NULL)
  206. {
  207. Preorder(pNode->Right, Visit);
  208. }
  209. }
  210. template<class DataType>
  211. void BST<DataType>::Inorder(Node<DataType>* pNode, FunctionType Visit)
  212. {
  213. if (pNode->Left != NULL)
  214. {
  215. Inorder(pNode->Left, Visit);
  216. }
  217. if (pNode != NULL)
  218. {
  219. Visit(pNode->Item);
  220. }
  221. if (pNode->Right != NULL)
  222. {
  223. Inorder(pNode->Right, Visit);
  224. }
  225. }
  226.  
  227. template<class DataType>
  228. void BST<DataType>::Postorder(Node<DataType>* pNode, FunctionType Visit)
  229. {
  230. if (pNode->Left != NULL)
  231. {
  232. Postorder(pNode->Left, Visit);
  233. }
  234. if (pNode->Right != NULL)
  235. {
  236. Postorder(pNode->Right, Visit);
  237. }
  238. if (pNode != NULL)
  239. {
  240. Visit(pNode->Item);
  241. }
  242. }
  243.  
  244. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement