Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | None | 0 0
  1. #ifndef DS_EXCEPTIONS_H
  2. #define DS_EXCEPTIONS_H
  3.  
  4. class UnderflowException { };
  5. class IllegalArgumentException { };
  6. class ArrayIndexOutOfBoundsException { };
  7. class IteratorOutOfBoundsException { };
  8. class IteratorMismatchException { };
  9. class IteratorUninitializedException { };
  10.  
  11. #endif
  12.  
  13. #ifndef BINARY_SEARCH_TREE_H
  14. #define BINARY_SEARCH_TREE_H
  15.  
  16. #include "dsexceptions.h"
  17. #include <iostream> // For NULL
  18. using namespace std;
  19.  
  20. // BinarySearchTree class
  21. //
  22. // CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds
  23. //
  24. // ******************PUBLIC OPERATIONS*********************
  25. // void insert( x ) --> Insert x
  26. // void remove( x ) --> Remove x
  27. // bool contains( x ) --> Return true if x is present
  28. // Comparable findMin( ) --> Return smallest item
  29. // Comparable findMax( ) --> Return largest item
  30. // boolean isEmpty( ) --> Return true if empty; else false
  31. // void makeEmpty( ) --> Remove all items
  32. // void printTree( ) --> Print elements of the tree in order
  33. // ******************ERRORS********************************
  34. // Throws UnderflowException as warranted
  35.  
  36. template <typename Comparable>
  37. class BinarySearchTree
  38. {
  39. public:
  40.  
  41. BinarySearchTree( ) :root( NULL )
  42. { }
  43.  
  44. BinarySearchTree(const BinarySearchTree & rhs) : root(NULL)
  45. { *this = rhs; }
  46.  
  47. /**
  48. * Destructor for the tree
  49. */
  50. ~BinarySearchTree( )
  51. { makeEmpty( ); }
  52.  
  53. /**
  54. * Find the smallest item in the tree.
  55. * Throw UnderflowException if empty.
  56. */
  57. const Comparable & findMin( ) const
  58. {
  59. if (isEmpty( ))
  60. throw UnderflowException( );
  61. return findMin(root)->element;
  62. }
  63.  
  64. /**
  65. * Find the largest item in the tree.
  66. * Throw UnderflowException if empty.
  67. */
  68. const Comparable & findMax( ) const
  69. {
  70. if(isEmpty( ))
  71. throw UnderflowException( );
  72. return findMax( root )->element;
  73. }
  74.  
  75. /**
  76. * Returns true if x is found in the tree.
  77. */
  78. bool contains(const Comparable & x) const
  79. { return contains(x, root); }
  80.  
  81. /**
  82. * Test if the tree is logically empty.
  83. * Return true if empty, false otherwise.
  84. */
  85. bool isEmpty( ) const
  86. { return root == NULL; }
  87.  
  88. /**
  89. * Print the tree contents in sorted order.
  90. */
  91. void printTree(ostream & out = cout) const
  92. {
  93. if (isEmpty( ))
  94. out << "Empty tree" << endl;
  95. else
  96. printTree(root, out); /**********************************************************************************************************************/
  97. // As part of the assignment, students
  98. // must provide the code for the
  99. // overloaded private member function.
  100. }
  101.  
  102. void treeToList(ostream & out = cout) //to populate the linked list from the Binary Tree.
  103. {
  104. if(isEmpty())
  105. out << "Empty Tree." << endl;
  106. else
  107. {
  108. listInitial(head); //initializes the linked list 'head' element to be NULL.
  109. treeToList(head, root); //overloaded function of treeToList.
  110. }
  111. }
  112.  
  113. void displayList(ostream & out = cout) //public call to display the doubly linked list if there are values within.
  114. {
  115. if(head == NULL)
  116. out << "List is Empty." << endl;
  117. else
  118. {
  119. displayList(head);
  120. }
  121. }
  122.  
  123. /**
  124. * Make the tree logically empty.
  125. */
  126. void makeEmpty( )
  127. { makeEmpty(root); }
  128.  
  129. /**
  130. * Insert x into the tree; duplicates are ignored.
  131. */
  132. void insert(const Comparable & x)
  133. { insert(x, root); }
  134.  
  135. /**
  136. * Remove x from the tree. Nothing is done if x is not found.
  137. */
  138. void remove(const Comparable & x)
  139. { remove(x, root); }
  140.  
  141. /**
  142. * Deep copy.
  143. */
  144. const BinarySearchTree & operator=(const BinarySearchTree & rhs)
  145. {
  146. if (this != &rhs)
  147. {
  148. makeEmpty( );
  149. root = clone(rhs.root);
  150. }
  151. return *this;
  152. }
  153.  
  154. private:
  155. struct BinaryNode
  156. {
  157. Comparable element;
  158. BinaryNode *left;
  159. BinaryNode *right;
  160.  
  161. BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt)
  162. : element(theElement), left(lt), right(rt) { }
  163. };
  164.  
  165. BinaryNode *root; /*******************************************************************************************************************************************/
  166.  
  167. struct Node //node for linked list
  168. {
  169. int data;
  170. Node *next;
  171. };
  172.  
  173. Node *head;
  174.  
  175. /**
  176. * Internal method to insert into a subtree.
  177. * x is the item to insert.
  178. * t is the node that roots the subtree.
  179. * Set the new root of the subtree.
  180. */
  181. void insert(const Comparable & x, BinaryNode * & t)
  182. {
  183. if (t == NULL)
  184. t = new BinaryNode(x, NULL, NULL);
  185. else if (x < t->element)
  186. insert(x, t->left);
  187. else if (t->element < x)
  188. insert(x, t->right);
  189. else; // Duplicate; do nothing
  190. }
  191.  
  192. /**
  193. * Internal method to remove from a subtree.
  194. * x is the item to remove.
  195. * t is the node that roots the subtree.
  196. * Set the new root of the subtree.
  197. */
  198. void remove(const Comparable & x, BinaryNode * & t)
  199. {
  200. if (t == NULL)
  201. return; // Item not found; do nothing
  202. if (x < t->element)
  203. remove(x, t->left);
  204. else if (t->element < x)
  205. remove(x, t->right);
  206. else if (t->left != NULL && t->right != NULL) // Two children
  207. {
  208. t->element = findMin(t->right)->element;
  209. remove(t->element, t->right);
  210. }
  211. else
  212. {
  213. BinaryNode *oldNode = t;
  214. t = (t->left != NULL) ? t->left : t->right;
  215. delete oldNode;
  216. }
  217. }
  218.  
  219. /**
  220. * Internal method to find the smallest item in a subtree t.
  221. * Return node containing the smallest item.
  222. */
  223. BinaryNode * findMin(BinaryNode *t) const
  224. {
  225. if (t == NULL)
  226. return NULL;
  227. if (t->left == NULL)
  228. return t;
  229. return findMin(t->left);
  230. }
  231.  
  232. /**
  233. * Internal method to find the largest item in a subtree t.
  234. * Return node containing the largest item.
  235. */
  236. BinaryNode * findMax(BinaryNode *t) const
  237. {
  238. if (t != NULL)
  239. while (t->right != NULL)
  240. t = t->right;
  241. return t;
  242. }
  243.  
  244. /**
  245. * Internal method to test if an item is in a subtree.
  246. * x is item to search for.
  247. * t is the node that roots the subtree.
  248. */
  249. bool contains(const Comparable & x, BinaryNode *t) const
  250. {
  251. if (t == NULL)
  252. return false;
  253. else if (x < t->element)
  254. return contains(x, t->left);
  255. else if (t->element < x)
  256. return contains(x, t->right);
  257. else
  258. return true; // Match
  259. }
  260.  
  261. /**
  262. * Internal method to make subtree empty.
  263. */
  264. void makeEmpty(BinaryNode * & t)
  265. {
  266. if (t != NULL)
  267. {
  268. makeEmpty(t->left);
  269. makeEmpty(t->right);
  270. delete t;
  271. }
  272. t = NULL;
  273. }
  274.  
  275.  
  276. /***********************************************
  277. * Internal method to print a subtree rooted.
  278. */
  279. // Students provide the code for this function
  280. /**************************************************************************************************************************************************************
  281. */
  282. void printTree(BinaryNode *t, ostream & out = cout) const
  283. {
  284. if(t)
  285. {
  286. printTree(t->left, out);
  287. out << t->element << " ";
  288. printTree(t->right, out);
  289. }
  290. }
  291.  
  292. /*
  293. *Sets the list head to NULL.
  294. */
  295. void listInitial(Node *&head)
  296. {
  297. cout << "listInitial function has been called." << endl;
  298. head = NULL;
  299. }
  300.  
  301. /*
  302. * Method to populate the linked list with the BinaryTree printTree function.************************************************************************************
  303. */
  304. void treeToList(Node *&head, BinaryNode* t)
  305. {
  306. cout << "treeToList function has been called." << endl;
  307. if(t)
  308. {
  309. treeToList(head, t->left);
  310. insertToList(head, t->element);
  311. treeToList(head, t->right);
  312. }
  313. }
  314.  
  315.  
  316. void insertToList(Node *&head, int value) //this function inserts the node to the end of the linked list since inserts from the tree are already in order.******************
  317. {
  318. cout << "insertToList function has been called." << endl;
  319. Node *tempNode; //temporary node for the locating of the end-node's placement.
  320. tempNode = (Node*)malloc(sizeof(Node)); //allocating space for the node.
  321. tempNode = head; //give the address of the 'head' to 'tempNode'.
  322. while(tempNode->next != NULL) //goes to the last node in the list by searching until the node without a 'next' is found.
  323. tempNode = tempNode->next; //the address of 'temp1->next' is moved to temp1.
  324. //*************************************************************************************************************************************
  325. Node *temp2; //another temporary node, this time for the actual insert.
  326. temp2 = (Node*)malloc(sizeof(Node)); //allocating space for the node.
  327. temp2->data = value; //set the value field of the Node.
  328. temp2->next = NULL; //since this inserted node will be the new end of list, logically it's 'next' pointer is null.
  329. tempNode->next = temp2; //makes the node the last in list.
  330. }
  331.  
  332. void displayList(Node *head) //this function is from Professor Tim Hartley's code on his website: www.timhartley.com/mcc/DataStructures/swap.cpp ***********
  333. {
  334. Node *nodePtr; // Pointer to move through the list
  335. nodePtr = head; // Position nodePtr at head of the list
  336. if (head == NULL)
  337. cout << "list is empty" << endl;
  338. else
  339. {
  340. while (nodePtr) // Follow pointers through the list
  341. {
  342. cout << nodePtr->data << endl; // Display value in this node
  343. nodePtr = nodePtr->next; // Advance to next node
  344. }
  345. }
  346. }
  347.  
  348. /**
  349. * Internal method to clone subtree.
  350. */
  351. BinaryNode * clone(BinaryNode *t) const
  352. {
  353. if (t == NULL)
  354. return NULL;
  355. else
  356. return new BinaryNode(t->element, clone(t->left), clone(t->right));
  357. }
  358. };
  359.  
  360. #endif
  361.  
  362. #include <iostream>
  363. #include "BinarySearchTree.h"
  364. using namespace std;
  365.  
  366. int main( )
  367. {
  368. BinarySearchTree<int> t;
  369. int i;
  370.  
  371. cout << "inserting nodes into tree" << endl;
  372. t.insert(50);
  373. t.insert(60);
  374. t.insert(30);
  375. t.insert(20);
  376. t.insert(40);
  377. t.insert(70);
  378. t.insert(55);
  379. t.insert(65);
  380. t.insert(25);
  381. t.insert(35);
  382. t.insert(85);
  383. t.insert(100);
  384. t.insert(15);
  385. t.insert(45);
  386. t.insert(95);
  387. t.insert(105);
  388. t.insert(10);
  389. t.insert(75);
  390. t.insert(110);
  391. t.insert(12);
  392. t.insert(92);
  393. t.insert(32);
  394. t.insert(82);
  395. t.insert(22);
  396. t.insert(32);
  397.  
  398. t.printTree( );
  399.  
  400. cout << endl;
  401. cout << "Finished processing Binary Tree inserts.n" << endl;
  402.  
  403. cout << "********************************************n" << endl;
  404.  
  405. cout <<"Below is the linked list with data from least to greatest from the Binary Tree." << endl;
  406.  
  407. t.treeToList();
  408.  
  409. system("pause");
  410. return 0;
  411. }
  412.  
  413. inserting nodes into tree
  414. 10 12 15 20 22 25 30 32 35 40 45 50 55 60 65 70 75 82 85 92 95 100 105 110
  415. Finished processing Binary Tree inserts.
  416.  
  417. ********************************************
  418.  
  419. Below is the linked list with data from least to greatest from the Binary Tree.
  420. listInitial function was called.
  421. treeToList function was called.
  422. treeToList function was called.
  423. treeToList function was called.
  424. treeToList function was called.
  425. treeToList function was called.
  426. treeToList function was called.
  427. insertToList function was called.
  428. Segmentation fault (core dumped)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement