Binary Tree - Works in C++ 6.0 but doesn't work in VS2008

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Binary Tree - Works in C++ 6.0 but doesn't work in VS2008

 
0
  #1
Mar 31st, 2009
Hey guys,

Can anyone tell me why this doesn't work in VS2008? I've commented out the #includes and whatnot so you can paste it into a single .cpp file and run it.

Here are my errors:
1>test.obj : error LNK2028: unresolved token (0A000320) "int __cdecl eval(class Tree<struct InfoNode> &)" (?eval@@$$FYAHAAV?$Tree@UInfoNode@@@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)


1>test.obj : error LNK2028: unresolved token (0A00032D) "void __cdecl buildExprTree(class Tree<struct InfoNode> &)" (?buildExprTree@@$$FYAXAAV?$Tree@UInfoNode@@@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)


1>test.obj : error LNK2019: unresolved external symbol "int __cdecl eval(class Tree<struct InfoNode> &)" (?eval@@$$FYAHAAV?$Tree@UInfoNode@@@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)


1>test.obj : error LNK2019: unresolved external symbol "void __cdecl buildExprTree(class Tree<struct InfoNode> &)" (?buildExprTree@@$$FYAXAAV?$Tree@UInfoNode@@@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
And here's the code:
  1. // queue.h
  2. // linked list implementation of Queue ADT
  3. #include <iostream>
  4. #include <cstddef> // for NULL
  5. #include <new> // for bad_alloc exception
  6. using namespace std;
  7.  
  8. template <class T>
  9. struct Node; // forward reference, Node defined below
  10.  
  11. template<class T>
  12. class Queue
  13. {
  14. private:
  15. Node<T> * front; // pointer to front of Queue
  16. Node<T> * rear; // pointer to rear of Queue
  17. int length; // logical length of Queue
  18. public:
  19. Queue(); // default c-tor
  20. ~Queue(); // d-tor
  21.  
  22. void makeMT(); // make Queue logically empty
  23. // Post: front and rear have been reset to the empty state
  24. bool isMT() const; // test for Queue being empty
  25. // returns true if the queue is empty; false otherwise
  26. bool isFull() const; // test for Queue being full
  27. // Returns true if the queue is full; false otherwise
  28. void enqueue(T item); // enqueue an item
  29. // Post: parameter item is at the rear of the queue
  30. void dequeue(T & item); // dequeue an item
  31. // Post: front of queue has been removed and a copy returned in item
  32. int getLength() const; // returns length queue
  33. // Post: Queue is unchanged
  34. void replaceItem(T old, T nu); // replaces all occurrences of old with nu
  35. // Pre: queue has been initialized
  36. // Post: each occurrence of old in queue has been changed to nu
  37. void prntQueue() const; // displays contents of queue
  38. // Pre: queue has been initialized
  39. // Post: contents of queue have been printed
  40.  
  41. }; // end Queue
  42.  
  43. // implementations
  44. /*template <class T>
  45. struct Node
  46. {
  47.   T info;
  48.   Node<T> * next;
  49. }; */
  50.  
  51. template<class T> // default class c-tor
  52. Queue<T>::Queue() : front(NULL), rear(NULL), length(0)
  53. { }
  54.  
  55. template<class T> // d-tor
  56. Queue<T>::~Queue()
  57. {
  58. makeMT();
  59. }
  60.  
  61. template<class T>
  62. void Queue<T>::makeMT()
  63. {
  64. Node <T> * temp;
  65.  
  66. while ( front != NULL )
  67. {
  68. temp = front;
  69. front = front->next;
  70. delete temp;
  71. } // endwhile
  72. rear = NULL;
  73. length = 0;
  74. return;
  75. } // end makeMT()
  76.  
  77. template<class T>
  78. bool Queue<T>::isMT() const
  79. {
  80. return ( length == 0 ); // alternative is to return ( front == NULL );
  81. } // end isMT()
  82.  
  83. template<class T>
  84. bool Queue<T>::isFull() const
  85. {
  86. Node<T> * location;
  87. try
  88. {
  89. location = new Node<T>;
  90. delete location;
  91. return false;
  92. } // endtry
  93. catch(bad_alloc exception)
  94. {
  95. return true;
  96. } // endcatch
  97. } // end isFull()
  98.  
  99. template<class T>
  100. void Queue<T>::enqueue(T item)
  101. {
  102. Node<T> * newNode;
  103.  
  104. newNode = new Node<T>;
  105. newNode->info = item;
  106. newNode->next = NULL;
  107. if ( rear == NULL )
  108. front = newNode;
  109. else
  110. rear->next = newNode;
  111. rear = newNode;
  112. length++;
  113. } // end enqueue()
  114.  
  115. template<class T>
  116. void Queue<T>::dequeue(T & item)
  117. {
  118. Node<T> * temp;
  119.  
  120. temp = front;
  121. item = front->info;
  122. front = front->next;
  123. if ( front == NULL )
  124. rear = NULL;
  125. delete temp;
  126. length--;
  127. return;
  128. } // end dequeue()
  129.  
  130. template<class T>
  131. int Queue<T>::getLength() const
  132. {
  133. return length;
  134. } // end getLength()
  135.  
  136. template<class T>
  137. void Queue<T>::replaceItem(T old, T nu)
  138. {
  139. Node<T> * location = front;
  140.  
  141. while ( location != NULL )
  142. {
  143. if ( location->info == old )
  144. location->info = nu;
  145. location = location->next;
  146. } // endwhile
  147. } // end replaceItem()
  148.  
  149. template<class T>
  150. void Queue<T>::prntQueue() const
  151. {
  152. Node<T> * location = front;
  153.  
  154. cout << "\n Printing contents of queue:\n";
  155. while ( location != NULL )
  156. {
  157. cout << "\t" << location->info;
  158. location = location->next;
  159. } // endwhile
  160. cout << endl;
  161. return;
  162. } // end prntQueue()
  163. // end Queue implementations
  164.  
  165.  
  166. // stack.h
  167. // header file for Stack ADT using a templated linked list
  168. //#pragma once
  169.  
  170. //#include "stkxcptn.h"
  171. template <class T>
  172. struct Node; // forward reference - struct Node is defined below
  173.  
  174. template<class T>
  175. class Stack
  176. {
  177. private:
  178. Node<T> * top;
  179.  
  180. public:
  181. Stack();
  182. ~Stack();
  183.  
  184. void makeMT();
  185. // Function: sets stack to an empty state
  186. // Post: Stack is empty
  187.  
  188. bool isFull() const;
  189. // Function: determines whether the stack is full
  190. // Pre: Stack has been initialized
  191. // Post: Function value = false (assuming new succeeds)
  192.  
  193. bool isMT() const;
  194. // Function: determines whether the stack is empty
  195. // Pre: Stack has been initialized
  196. // Post: Function value = (stack is empty)
  197.  
  198. void push(T item);
  199. // Function: adds item to the top of the stack
  200. // Pre: Stack has been initialized
  201. // Post: If (stack is full), PushOnFullStack exception is thrown;
  202. // otherwise, item is at the top of the stack
  203.  
  204. void pop(T & item);
  205. // Function: removes top item from the stack and returns it in item
  206. // Pre: Stack has been initialized
  207. // Post: If (stack is MT), PopOnMTStack exception is thrown;
  208. // otherwise, top element has been removed from stack
  209.  
  210. void prntStack() const;
  211. // Function: displays contents of the stack
  212. // Pre: Stack has been initialized
  213. // Post: Stack is walked and members printed
  214. }; // end Stack
  215.  
  216. // stack.cpp
  217.  
  218. // Stack ADT - implementation using a templated linked list
  219.  
  220. //#include <cstddef>
  221. //#include <new>
  222. template <class T>
  223. struct Node
  224. {
  225. T info; // Node data of type T
  226. Node * next; // link to next Node
  227.  
  228. Node<T>() : info(0), next(NULL) // default c-tor for Node
  229. { }
  230.  
  231. Node<T>( const T & e, Node * nx = NULL ) // parameterized c-tor for Node
  232. : info(e), next(nx)
  233. { }
  234. }; // end Node
  235.  
  236. template<class T>
  237. Stack<T>::Stack() : top(NULL)
  238. { }
  239.  
  240. template <class T>
  241. Stack<T>::~Stack()
  242. {
  243. Node<T> * temp;
  244.  
  245. while ( top != NULL )
  246. {
  247. temp = top;
  248. top = top->next;
  249. delete temp;
  250. } // endwhile
  251. } // end ~Stack()
  252.  
  253. template<class T>
  254. void Stack<T>::pop(T & item)
  255. {
  256. if ( isMT() )
  257. throw PopOnMTStack();
  258. else
  259. {
  260. Node<T> * temp;
  261. temp = top;
  262. item = top->info;
  263. top = top->next;
  264. delete temp;
  265. } // endif
  266. } // end pop()
  267.  
  268. template<class T>
  269. bool Stack<T>::isFull() const
  270. {
  271. Node<T> * location;
  272. try
  273. {
  274. location = new Node<T>;
  275. delete location;
  276. return false;
  277. } // endtry
  278. catch(bad_alloc exception)
  279. {
  280. return true;
  281. } // endcatch
  282. } // end isFull()
  283.  
  284. template <class T>
  285. void Stack<T>::push(T item)
  286. {
  287. if ( isFull() )
  288. throw PushOnFullStack();
  289. else
  290. {
  291. Node<T> * ptr;
  292. ptr = new Node<T>(item);
  293. ptr->next = top;
  294. top = ptr;
  295. } // endif
  296. } // end push()
  297.  
  298. template <class T>
  299. void Stack<T>::makeMT()
  300. {
  301. Node<T> * temp;
  302.  
  303. while ( top != NULL )
  304. {
  305. temp = top;
  306. top = top->next;
  307. delete temp;
  308. } // endwhile
  309. } // end makeMT()
  310.  
  311. template <class T>
  312. bool Stack<T>::isMT() const
  313. {
  314. return ( top == NULL );
  315. } // end isMT()
  316.  
  317. template <class T>
  318. void Stack<T>::prntStack() const
  319. {
  320. Node<T> * ptr = top;
  321. cout << "\n Walking the stack: "
  322. while ( ptr != NULL )
  323. {
  324. cout << "\t" << ptr->info;
  325. ptr = ptr->next;
  326. } // endwhile
  327.  
  328. return;
  329. } // end prntStack()
  330.  
  331. // stkxcptn.h - header file for PopOnMTStack and PushOnFullStack exception class
  332.  
  333. //#include <iostream>
  334. //using namespace std;
  335.  
  336. class PopOnMTStack
  337. {
  338. private :
  339. string msg;
  340.  
  341. public :
  342. PopOnMTStack() // default c-tor
  343. {
  344. msg = string("\n Stack underflow - operation aborted");
  345. }
  346. const string message() const
  347. {
  348. return msg;
  349. }
  350. }; // end PopOnMTStack
  351.  
  352.  
  353. class PushOnFullStack
  354. {
  355. private :
  356. string msg;
  357.  
  358. public :
  359. PushOnFullStack() // default c-tor
  360. {
  361. msg = string("\n Stack overflow - operation aborted");
  362. }
  363. const string message() const
  364. {
  365. return msg;
  366. }
  367. }; // end PopOnMTStack
  368.  
  369.  
  370. // File Tree.h contains the definition of class Tree
  371. // Class is templated
  372.  
  373. //#pragma once
  374. //#include "Queue.h"
  375. //#include "Stack.h"
  376. #include <fstream>
  377.  
  378. const char LAST_SYMBOL = '=';
  379. enum OpType { OPERATOR, OPERAND };
  380.  
  381. struct InfoNode
  382. {
  383. OpType whichType; // Type field
  384. union // Anonymous union
  385. {
  386. char operation;
  387. int operand;
  388. };
  389. };
  390.  
  391. typedef InfoNode T;
  392.  
  393. template <class T>
  394. struct TreeNode;
  395. // Assumption: T is a type for which the operators "<"
  396. // and "==" are defined - either an appropriate built-in type or
  397. // a class that overloads these operators
  398.  
  399. enum OrderType { PRE_ORDER, IN_ORDER, POST_ORDER };
  400.  
  401. template <class T>
  402. class Tree
  403. {
  404. private:
  405. TreeNode<T> * root;
  406. // The following queues are used for traversals
  407. Queue<T> preQue;
  408. Queue<T> inQue;
  409. Queue<T> postQue;
  410. public:
  411. Tree(); // constructor
  412. ~Tree(); // destructor
  413. Tree(const Tree<T> & oTree);
  414. // copy constructor
  415. void operator=(const Tree<T> & oTree);
  416. // Overloads the assignment operator
  417. void makeMT();
  418. // Function: Initializes tree to the empty state
  419. // Post: Tree is empty
  420. bool isMT() const;
  421. // Function: Determines whether the tree is empty
  422. // Post: Function value = (tree is empty)
  423. bool isFull() const;
  424. // Function: Determines whether the tree is full
  425. // Post: Function value = (tree is full)
  426. int numberOfNodes() const;
  427. // Function: Determines the number of elements in the tree
  428. // Post: Function value = number of elements in the tree
  429. void retrieveItem(T & item, bool & found);
  430. // Function: Retrieves item whose key matches item's key (if present)
  431. // Pre: Key of item has been initialized
  432. // Post: If there is an item someItem whose key matches item's
  433. // key, then found is true and item is a copy of someItem;
  434. // otherwise found is false and item is unchanged
  435. // The tree is unchanged
  436. void insertItem(T item);
  437. // Function: Adds item to the tree
  438. // Pre: Tree is not full; item is not in the tree
  439. // Post: item is in the tree
  440. // Binary search property is maintained
  441. void removeItem(T item);
  442. // Function: Deletes the item whose key matches item's key
  443. // Pre: Key of item has been initialized
  444. // One and only one item in the tree has a key matching
  445. // item's key
  446. // Post: No element in tree has key matching item's key
  447. void resetTree(OrderType order);
  448. // Function: Initializes current position for an iteration
  449. // through the tree in order specified
  450. // Post: Current position is prior to root of the tree
  451. void getNextItem (T & item, OrderType order, bool & fini);
  452. // Function: Gets the next element in the tree in the order specified in order
  453. // Pre: Current position is defined
  454. // Item at current position is not last in the tree
  455. // Post: Current position is one position beyond current position at entry to getNextItem()
  456. // fini = (current position is last in tree)
  457. // item is a copy of element at current position
  458. void printTree(std::ofstream & outFile) const;
  459. // Function: Prints the values in the tree in ascending key
  460. // order on outFile
  461. // Pre: outFile has been opened for writing
  462. // Post: Values in the three have been printed in ascending key order
  463. // outFile is still open
  464. friend void buildExprTree(Tree<T> & expr);
  465. friend int eval(Tree<T> & expr);
  466. }; // end Tree
  467.  
  468. //#include "Tree.cpp"
  469.  
  470. // Implementation file for Binary Search Tree;
  471. // recursive insertion and deletion functions
  472.  
  473.  
  474. template <class T>
  475. struct TreeNode
  476. {
  477. T info;
  478. TreeNode * left;
  479. TreeNode * right;
  480. TreeNode() // default c-tor for TreeNode
  481. { }
  482. };
  483.  
  484. template <class T>
  485. int countNodes(Tree<T> * tree)
  486. // Post: returns the number of nodes in the tree
  487. {
  488. if (tree == NULL)
  489. return 0;
  490. else
  491. return countNodes(tree->left) + countNodes(tree->right) + 1;
  492. }
  493.  
  494. template <class T>
  495. int Tree<T>::numberOfNodes() const
  496. // Calls recursive function CountNodes to count the
  497. // nodes in the tree
  498. {
  499. return countNodes(root);
  500. }
  501.  
  502. template <class T>
  503. void retrieve(TreeNode<T> * tree, T & item, bool & found)
  504. // Recursively searches tree for item
  505. // Post: If there is an element someItem whose key matches item's, found
  506. // is true and item is set to a copy of someItem; otherwise found
  507. // is false and item is unchanged
  508. {
  509. if (tree == NULL)
  510. found = false; // item is not found
  511. else if ( item < tree->info ) // Search left subtree
  512. retrieve(tree->left, item, found); // Search right subtree
  513. else if ( item > tree->info )
  514. retrieve(tree->right, item, found);
  515. else
  516. {
  517. item = tree->info; // item is found
  518. found = true;
  519. }
  520. }
  521.  
  522. template <class T>
  523. void Tree<T>::retrieveItem(T& item, bool& found)
  524. // Calls recursive function Retrieve to search the tree for item
  525. {
  526. retrieve(root, item, found);
  527. }
  528.  
  529. template <class T>
  530. void insert(TreeNode<T> * & tree, T item)
  531. // Inserts item into tree
  532. // Post: item is in tree; search property is maintained
  533. {
  534. if ( tree == NULL )
  535. { // insertion place found
  536. tree = new TreeNode<T>;
  537. tree->right = NULL;
  538. tree->left = NULL;
  539. tree->info = item;
  540. }
  541. else if ( item < tree->info )
  542. Insert(tree->left, item); // insert in left subtree
  543. else
  544. Insert(tree->right, item); // nsert in right subtree
  545. }
  546.  
  547. template <class T>
  548. void Tree<T>::insertItem(T item)
  549. // Calls recursive function Insert to insert item into tree
  550. {
  551. insert(root, item);
  552. }
  553.  
  554. template <class T>
  555. void removeNode(TreeNode<T> * & tree)
  556. // Deletes the node pointed to by tree
  557. // Post: The user's data in the node pointed to by tree is no longer in the
  558. // tree. If tree is a leaf node or has only non-NULL child pointer
  559. // the node pointed to by tree is deleted; otherwise, the user's data
  560. // is replaced by its logical predecessor and the predecessor's node
  561. // is deleted
  562. {
  563. T data;
  564. TreeNode<T>* tempPtr;
  565.  
  566. tempPtr = tree;
  567. if ( tree->left == NULL )
  568. {
  569. tree = tree->right;
  570. delete tempPtr;
  571. }
  572. else if ( tree->right == NULL )
  573. {
  574. tree = tree->left;
  575. delete tempPtr;
  576. }
  577. else
  578. {
  579. getPredecessor(tree->left, data);
  580. tree->info = data;
  581. remove(tree->left, data); // Delete predecessor node
  582. }
  583. }
  584.  
  585. template <class T>
  586. void remove(TreeNode<T>*& tree, T item)
  587. // Deletes item from tree
  588. // Post: item is not in tree
  589. {
  590. if (item < tree->info )
  591. remove(tree->left, item); // Look in left subtree
  592. else if ( item > tree->info )
  593. remove(tree->right, item); // Look in right subtree
  594. else
  595. removeNode(tree); // Node found; call removeNode
  596. }
  597.  
  598. template <class T>
  599. void Tree<T>::removeItem(T item)
  600. // Calls recursive function remove() to delete item from tree
  601. {
  602. remove(root, item);
  603. }
  604.  
  605. template<class T>
  606. void getPredecessor(TreeNode<T> * tree, T & data)
  607. // Sets data to the info member of the right-most node in tree
  608. {
  609. while ( tree->right != NULL )
  610. tree = tree->right;
  611. data = tree->info;
  612. }
  613.  
  614. template <class T>
  615. void Tree<T>::printTree(std::ofstream & outFile) const
  616. // Calls recursive function print() to print items in the tree
  617. {
  618. print(root, outFile);
  619. }
  620.  
  621. template<class T>
  622. void print(TreeNode<T> * tree, std::ofstream & outFile)
  623. // Prints info member of items in tree in sorted order on outFile
  624. {
  625. if ( tree != NULL )
  626. {
  627. print(tree->left, outFile); // Print left subtree
  628. outFile << tree->info;
  629. print(tree->right, outFile); // Print right subtree
  630. }
  631. }
  632.  
  633. template <class T>
  634. Tree<T>::Tree()
  635. {
  636. root = NULL;
  637. }
  638.  
  639. template<class T>
  640. void destroy(TreeNode<T> * & tree)
  641. // Post: tree is empty; nodes have been deallocated
  642. {
  643. if ( tree != NULL )
  644. {
  645. destroy(tree->left);
  646. destroy(tree->right);
  647. delete tree;
  648. }
  649. }
  650.  
  651. template <class T>
  652. Tree<T>::~Tree()
  653. // Calls recursive function destroy() to destroy the tree
  654. {
  655. destroy(root);
  656. }
  657.  
  658. template <class T>
  659. void copyTree(TreeNode<T> * & copy, TreeNode<T> * originalTree )
  660. // Post: copy is the root of a tree that is a duplicate
  661. // of tree pointed to by originalTree
  662. {
  663. if ( originalTree == NULL )
  664. copy = NULL;
  665. else
  666. {
  667. copy = new TreeNode<T>;
  668. copy->info = originalTree->info;
  669. copyTree(copy->left, originalTree->left);
  670. copyTree(copy->right, originalTree->right);
  671. }
  672. }
  673.  
  674.  
  675. template <class T>
  676. Tree<T>::Tree(const Tree<T> & originalTree)
  677. // Calls recursive function copyTree() to copy originalTree into root
  678. {
  679. copyTree(root, originalTree.root);
  680. }
  681.  
  682. template <class T>
  683. void Tree<T>::operator=(const Tree<T> & originalTree)
  684. // Calls recursive function CopyTree to copy originalTree into root
  685. {
  686. if ( &originalTree == this )
  687. return; // Ignore assigning self to self
  688. destroy(root); // Deallocate existing tree nodes
  689. copyTree(root, originalTree.root);
  690. }
  691.  
  692. // Function prototypes for auxiliary functions
  693. template <class T>
  694. void buildExprTree(Tree<T> & expr);
  695. // builds a preorder binary expression tree
  696.  
  697. template <class T>
  698. void preorder(TreeNode<T> *, Queue<T> &);
  699. // Enqueues tree items in preorder
  700.  
  701. template <class T>
  702. void inorder(TreeNode<T> *, Queue<T> &);
  703. // Enqueues tree items in inorder
  704.  
  705. template <class T>
  706. void postorder(TreeNode<T> *, Queue<T> &);
  707. // Enqueues tree items in postorder
  708.  
  709. template <class T>
  710. void Tree<T>::resetTree(OrderType order)
  711. // Calls function to create a queue of the tree elements in
  712. // the specified order
  713. {
  714. switch ( order )
  715. {
  716. case PRE_ORDER : preorder(root, preQue);
  717. break;
  718. case IN_ORDER : inorder(root, inQue);
  719. break;
  720. case POST_ORDER: postorder(root, postQue);
  721. break;
  722. } // endswitch
  723. }
  724.  
  725. template <class T>
  726. void preorder(TreeNode<T> * tree, Queue<T> & preQue)
  727. // Post: preQue contains the tree items in preorder
  728. {
  729. if (tree != NULL)
  730. {
  731. preQue.enqueue(tree->info);
  732. preorder(tree->left, preQue);
  733. preorder(tree->right, preQue);
  734. }
  735. }
  736.  
  737. template <class T>
  738. void inorder(TreeNode<T>* tree, Queue<T>& inQue)
  739. // Post: inQue contains the tree items in inorder
  740. {
  741. if ( tree != NULL )
  742. {
  743. inorder(tree->left, inQue);
  744. inQue.enqueue(tree->info);
  745. inorder(tree->right, inQue);
  746. } // endif
  747. }
  748.  
  749. template <class T>
  750. void postorder(TreeNode<T> * tree, Queue<T> & postQue)
  751. // Post: postQue contains the tree items in postorder
  752. {
  753. if (tree != NULL)
  754. {
  755. postorder(tree->left, postQue);
  756. postorder(tree->right, postQue);
  757. postQue.enqueue(tree->info);
  758. } // endif
  759. }
  760.  
  761. template <class T>
  762. void Tree<T>::getNextItem(T & item, OrderType order, bool & fini)
  763. // Returns the next item in the desired order
  764. // Post: For the desired order, item is the next item in the queue
  765. // If item is the last one in the queue, fini is true; otherwise
  766. // fini is false
  767. {
  768. fini = false;
  769. switch ( order )
  770. {
  771. case PRE_ORDER : preQue.dequeue(item);
  772. if ( preQue.isMT() )
  773. fini = true;
  774. break;
  775. case IN_ORDER : inQue.dequeue(item);
  776. if ( inQue.isMT() )
  777. fini = true;
  778. break;
  779. case POST_ORDER: postQue.dequeue(item);
  780. if ( postQue.isMT() )
  781. fini = true;
  782. break;
  783. } // endswitched
  784. }
  785.  
  786. template <class T>
  787. void buildExprTree(Tree<T> & expr)
  788. {
  789. buildTree(expr.root);
  790. }
  791.  
  792. // buildTree.cpp
  793. // buildTree() builds a binary expression tree.
  794. // Note: operands are restricted to single digits
  795.  
  796. //using namespace std;
  797.  
  798. void buildTree(TreeNode<T> * & tree)
  799. {
  800. enum MoveType { RIGHT, LEFT };
  801. MoveType nextMove = LEFT;
  802.  
  803. TreeNode<T> * nptr = new TreeNode<T>;
  804. TreeNode<T> * last;
  805. char symbol;
  806.  
  807. Stack <TreeNode<T> *> stack;
  808.  
  809. cin >> symbol;
  810.  
  811. cout << " operator: " << symbol << endl; // first symbol is an operator
  812.  
  813. nptr->info.whichType = OPERATOR;
  814. nptr->info.operation = symbol;
  815.  
  816. tree = nptr;
  817.  
  818. cin >> symbol; // this version uses single-digit operands
  819.  
  820. while ( symbol != LAST_SYMBOL )
  821. {
  822. last = nptr;
  823. nptr = new TreeNode<T>;
  824.  
  825. if ( ispunct(symbol) )
  826. {
  827. cout << " operator: " << symbol << endl;
  828. nptr->info.whichType = OPERATOR;
  829. nptr->info.operation = symbol;
  830. }
  831. else
  832. {
  833. cout << " operand: " << symbol << endl;
  834. nptr->info.whichType = OPERAND;
  835. nptr->info.operand = int(symbol - '0');
  836. nptr->right = NULL;
  837. nptr->left = NULL;
  838. } // endif
  839.  
  840. switch (nextMove)
  841. {
  842. case LEFT : last->left = nptr;
  843. stack.push(last);
  844. break;
  845. case RIGHT : stack.pop(last);
  846. last->right = nptr;
  847. break;
  848. } // endswitch
  849.  
  850. if ( nptr->info.whichType == OPERATOR )
  851. nextMove = LEFT;
  852. else
  853. nextMove = RIGHT;
  854. cin >> symbol;
  855. } // endwhile
  856. return;
  857. } // end buildTree()
  858.  
  859.  
  860. template <class T>
  861. int eval(Tree<T> & expr)
  862. {
  863. return eval(expr.root);
  864. }
  865.  
  866. // Function to evaluate a binary expression tree.
  867. // Pre: ptr is a pointer to a binary expression tree
  868. // Post: Function value = the value of the expression
  869. // represented by the binary tree pointed to by ptr
  870. int eval(TreeNode<T> * ptr)
  871. {
  872. switch ( ptr->info.whichType ) {
  873. case OPERAND :
  874. return ptr->info.operand;
  875. case OPERATOR :
  876. switch ( ptr->info.operation ) {
  877. case '+' : return ( eval(ptr->left) + eval(ptr->right) );
  878.  
  879. case '-' : return ( eval(ptr->left) - eval(ptr->right) );
  880.  
  881. case '*' : return ( eval(ptr->left) * eval(ptr->right) );
  882.  
  883. case '/' : return ( eval(ptr->left) / eval(ptr->right) );
  884.  
  885. } // endswitch
  886. } // endswitch
  887. } // end eval()
  888.  
  889. //tstexpr.cpp
  890. /* ------------------------------------------------------------------------------
  891.  * program to test binary tree expression evaluation
  892.  * CS 320 - Data Structures l l craddock
  893.  */
  894.  
  895. //#include "tree.h"
  896.  
  897. void main()
  898. {
  899. Tree<T> e ;
  900.  
  901. cout << "\n Use a Binary Tree for Preorder Expression Evaluation\n" ;
  902.  
  903. cout << "\n Enter an expression terminated by an equal sign, e.g.: - * 6 3 5 =\n" ;
  904.  
  905. buildExprTree(e) ;
  906.  
  907. cout << "\n The value of the expression is " << eval(e) ;
  908.  
  909. cout << "\n -- Done --\n" ;
  910. return ;
  911. }
  912.  
  913. /* Sample output:
  914.  
  915.  Use a Binary Tree for Preorder Expression Evaluation
  916.  
  917.  Enter an expression terminated by an equal sign, e.g.: - * 6 3 5 =
  918. * - 8 5 / + 4 2 3 =
  919.  operator: *
  920.  operator: -
  921.   operand: 8
  922.   operand: 5
  923.  operator: /
  924.  operator: +
  925.   operand: 4
  926.   operand: 2
  927.   operand: 3
  928.  
  929.  The value of the expression is 6
  930.  -- Done -- */

Please help! I'm almost positive it was working at one point in vs2008 but now it doesn't seem to work in anything other than VS6.0
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Binary Tree - Works in C++ 6.0 but doesn't work in VS2008

 
0
  #2
Mar 31st, 2009
I think the two friend declarations need template <class T>
template <class T>
class Tree
{
...
template <class T> friend void buildExprTree(Tree<T> & expr);
template <class T> friend int eval(Tree<T> & expr);
...
};
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Binary Tree - Works in C++ 6.0 but doesn't work in VS2008

 
0
  #3
Mar 31st, 2009
Near miss.......

  1. template <class T>
  2. class Tree
  3. {
  4. ...
  5. template <typename T> friend void buildExprTree(Tree<T> & expr);
  6. template <typename T> friend int eval(Tree<T> & expr);
  7. ...
  8. };
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Binary Tree - Works in C++ 6.0 but doesn't work in VS2008

 
0
  #4
Mar 31st, 2009
Awesomes. Thanks!
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC