errors in code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 22
Reputation: chubbywubba is an unknown quantity at this point 
Solved Threads: 0
chubbywubba's Avatar
chubbywubba chubbywubba is offline Offline
Newbie Poster

errors in code

 
0
  #1
Feb 5th, 2008
I need help with the assignment operator and destructor, and the deleting by copy. Can someone help me with this? and when i compile just this i get an error on line 219 saying term does not evaluate to a function taking 0 arguments. Does anyone know what this means




  
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Node used in BST
  6. struct BSTNode {
  7. int key;
  8. BSTNode* left;
  9. BSTNode* right;
  10. };
  11.  
  12. // Binary Search Tree
  13. class BST {
  14. private:
  15. BSTNode* root;
  16. int size;
  17.  
  18. bool insert (BSTNode* newNode);
  19. BSTNode** find (int key);
  20. void preOrderTraversal (BSTNode* subRoot);
  21. void inOrderTraversal (BSTNode* subRoot);
  22. void postOrderTraversal (BSTNode* subRoot);
  23.  
  24. public:
  25. // Managers
  26. BST ();
  27. BST (BST& bst);
  28. ~BST ();
  29. BST& operator = (BST& bst);
  30.  
  31. // Accessors
  32. bool isEmpty ();
  33. bool isFull ();
  34. void preOrderTraversal ();
  35. void inOrderTraversal ();
  36. void postOrderTraversal ();
  37.  
  38. // Mutators
  39. bool insert (int key);
  40. bool deleteByMerge (int key);
  41. bool deleteByCopy (int key);
  42. };
  43.  
  44. // default Constructor
  45. BST::BST () {
  46. root = NULL;
  47. size = 0;
  48. }
  49. // Copy Constructor
  50. BST::BST (BST& bst) {
  51. root = bst.root;
  52. size = bst.size;
  53. }
  54.  
  55. /*************************************/
  56. // Destructor
  57. BST::~BST () {
  58. // delete root until tree is empty
  59. }
  60.  
  61. // Assignment Operator
  62. BST& BST::operator = (BST& bst) {
  63. // ???
  64. return *this;
  65. }
  66. /***************************************/
  67. // isEmpty: returns if tree is empty
  68. bool BST::isEmpty () {
  69. return !root;
  70. }
  71.  
  72. // isFull: returns if memory is available
  73. bool BST::isFull () {
  74. BSTNode* bogusNode = NULL;
  75. bogusNode = new BSTNode;
  76. if (bogusNode) {
  77. delete bogusNode;
  78. return false;
  79. }
  80. return true; // Note: nothing allocated
  81. }
  82.  
  83. // insert (public): adds a node to tree given key data
  84. bool BST::insert (int key) {
  85. if (isFull())
  86. return false;
  87.  
  88. BSTNode* newNode = new BSTNode;
  89. newNode->key = key;
  90. newNode->left = NULL;
  91. newNode->right = NULL;
  92. if (insert (newNode))
  93. return true;
  94. delete newNode;
  95. return false;
  96. }
  97.  
  98. // insert (private): adds a node to tree given new-node
  99. bool BST::insert (BSTNode* newNode) {
  100. BSTNode** walker = &root;
  101. while (*walker) {
  102. if ((*walker)->key > newNode->key)
  103. walker = &((*walker)->left);
  104. else if ((*walker)->key < newNode->key)
  105. walker = &((*walker)->right);
  106. else // Duplicate of key found
  107. return false;
  108. }
  109. *walker = newNode;
  110. return true;
  111. }
  112.  
  113. // find (private): returns dbl-pointer to target
  114. BSTNode** BST::find (int key) {
  115. BSTNode** walker = &root;
  116. while (*walker) {
  117. if ((*walker)->key == key)
  118. break;
  119. else if ((*walker)->key > key)
  120. walker = &((*walker)->left);
  121. else
  122. walker = &((*walker)->right);
  123. }
  124. return walker; // Note: if *walker is NULL, key not found
  125. }
  126.  
  127. // delete (public): using by-merge method: delete node containing key data
  128. bool BST::deleteByMerge (int key) {
  129. // find pointer to target deletion
  130. BSTNode** walker = find(key);
  131.  
  132. // Determine whether key is found and deletion is to occur
  133. if (!(*walker))
  134. return false; // deletion failed - target not found
  135. // keep pointer to node for deletion
  136. BSTNode* eliminationNode = *walker;
  137.  
  138. //CASE I - left-subtree exists
  139. if (eliminationNode->left) {
  140. // find right-most child of left subtree
  141. BSTNode** rtMostOfLeftSubtree = &(eliminationNode->left);
  142. while (*rtMostOfLeftSubtree)
  143. rtMostOfLeftSubtree = &((*rtMostOfLeftSubtree)->right);
  144.  
  145. // link right-most child of left subtree to right of target deletion
  146. *rtMostOfLeftSubtree = eliminationNode->right;
  147.  
  148. // bypass target deletion
  149. *walker = eliminationNode->left;
  150. }
  151. // CASE II - no left-subtree
  152. else {
  153. // bypass target deletion
  154. *walker = eliminationNode->right;
  155. }
  156.  
  157. // delete target
  158. delete eliminationNode;
  159.  
  160. // return success of deletion
  161. return true; // deletion successful
  162. }
  163.  
  164. /****************************/
  165. // delete (public): using by-copy method: delete node containing key data
  166. bool BST::deleteByCopy (int key) {
  167. // Use code from deleteByMerge where appropriate
  168.  
  169. // CASE I - target has no children
  170. // CASE II - target has left child, but no right child
  171. // CASE III - target has right child, but no left child
  172. // CASE IV - target has two children
  173.  
  174. // return success of deletion
  175. return true; // deletion successful
  176. }
  177.  
  178. // preOrderTraversal (public): calls private preOrderTraversal
  179. void BST::preOrderTraversal () {
  180. cout << "Pre-Order Traversal" << endl;
  181. preOrderTraversal(root);
  182. cout << "\b\b " << endl << endl;
  183. }
  184.  
  185. // preOrderTraversal (private): traverses tree by preOrder
  186. void BST::preOrderTraversal (BSTNode* subRoot) {
  187. // base case
  188. if (!subRoot)
  189. return;
  190.  
  191. // recursive method
  192. // process node
  193. cout << subRoot->key << ", ";
  194. // move left
  195. preOrderTraversal(subRoot->left);
  196. // move right
  197. preOrderTraversal(subRoot->right);
  198. }
  199.  
  200. // inOrderTraversal (public): calls private inOrderTraversal
  201. void BST::inOrderTraversal () {
  202. cout << "In-Order Traversal" << endl;
  203. inOrderTraversal(root);
  204. cout << "\b\b " << endl << endl;
  205. }
  206. // inOrderTraversal (private): traverses tree by inOrder
  207. void BST::inOrderTraversal (BSTNode* subRoot) {
  208. //base case
  209. if (subRoot == NULL)
  210. return;
  211. //rec case
  212.  
  213. //go right
  214. inOrderTraversal(subRoot->left());
  215. //go left
  216. cout << subRoot->key << ", ";
  217. //process
  218. inOrderTraversal(subRoot->right());
  219. }
  220.  
  221. // postOrderTraversal (public): calls private postOrderTraversal
  222. void BST::postOrderTraversal () {
  223. cout << "Post-Order Traversal" << endl;
  224. postOrderTraversal(root);
  225. cout << "\b\b " << endl << endl;
  226. }
  227. // postOrderTraversal (private): traverses tree by postOrder
  228. void BST::postOrderTraversal (BSTNode* subRoot) {
  229. //base case
  230. if (subRoot == NULL)
  231. return;
  232. //rec case
  233.  
  234. //go left
  235. postOrderTraversal(subRoot->left());
  236. //go right
  237. postOrderTraversal(subRoot->right());
  238.  
  239. cout << subRoot->key << ", ";
  240.  
  241. }
  242.  
  243. /****************************/
  244. void main () {
  245. BST bst;
  246. bst.insert(5);
  247. bst.insert(10);
  248. bst.insert(3);
  249. bst.insert(4);
  250. bst.insert(9);
  251. bst.insert(7);
  252. bst.insert(8);
  253. bst.preOrderTraversal();
  254. bst.inOrderTraversal();
  255. bst.postOrderTraversal();
  256.  
  257. }
Last edited by chubbywubba; Feb 5th, 2008 at 3:23 pm. Reason: forgot to put what the error was
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: errors in code

 
0
  #2
Feb 5th, 2008
line 235 (as posted here): left is not a function or a function pointer so you have to remove the parentheses postOrderTraversal(subRoot->left);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC