binary tree postfix calculator

Hey guys, me again. This time around, I'm working on a binary tree calculator. Unfortunately, although my algorithms are most likely correct. it seems my grasp of c++ has failed me, and am getting compile errors like crazy, and am at my wits end as to what's wrong-which is a lot.
my code is below. can anyone assist me in spotting any glaring errors? Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "TreeCalc.h"
#include <iostream>

using namespace std;

//Constructor
TreeCalc::TreeCalc()
{
 
  stack<TreeNode*> myStack;
}

//Destructor- frees memory
TreeCalc::~TreeCalc()
{
  cleanTree(myStack->top());
}

//Deletes tree/frees memory
void TreeCalc::cleanTree(TreeNode* ptr)
{
  if(ptr->value!=NULL){
    cleanTree(ptr->left);
    cleanTree(ptr->right);
  }
  else delete ptr;  
}

//Gets data from user
void TreeCalc::readInput()
{
    string response;
    cout << "Enter elements one by one in postfix notation" << endl
    << "Any non-numeric or non-operator character,"
    << " e.g. #, will terminate input" << endl;

    cout << "Enter first element: ";
    cin >> response;

    //while input is legal
    while (isdigit(response[0]) || response[0]=='/' || response[0]=='*'
            || response[0]=='-' || response[0]=='+' )
    {
        insert(response);
        cout << "Enter next element: ";
        cin >> response;
    }
}

//Puts value in tree stack
void TreeCalc::insert(const string& val)
{ 
  if(isdigit(val)){
    TreeNode* t=new TreeNode(val);
    myStack->push(t);
  }
  else{
    TreeNode* w=new TreeNode(val);
    w->right=myStack->top();
    myStack->pop();
    w->left=myStack->top();
    myStack->pop();
    myStack->push(w);
  }

}

//Prints data in prefix form
void TreeCalc::printPrefix(TreeNode* ptr) const
{
  if(ptr!=NULL){
    cout<<ptr->value;
    printPrefix(ptr->left);
    printPrefix(ptr->right);
}

//Prints data in infix form
void TreeCalc::printInfix(TreeNode* ptr) const
{
  if(ptr!=NULL){
    if(ptr->value=='+'||ptr->value=='-'||ptr->value=='*'||ptr->value=='/'){
      cout<<ptr->value;
      else {
	cout<<'(';
	printInfix(ptr->left);
	cout<<ptr->value;
	printInfix(ptr->right);
	cout<<ptr->')';
      }
    }
}

//Prints data in postfix form
void TreeCalc::printPostfix(TreeNode* ptr) const
{
  if(ptr!=NULL){
    printPostfix(ptr->left);
    printPostfix(ptr->right);
    cout<<ptr->value;
  }
}

// Prints tree in all 3 (pre,in,post) forms

void TreeCalc::printOutput() const
{
    if (mystack->size()!=0 && mystack->top()!=NULL)
    {
        cout << "Expression tree in postfix expression: ";
        // call your implementation of printPostfix()
	printPostfix(myStack->top());
        cout << endl;

        cout << "Expression tree in infix expression: ";
        // call your implementation of printInfix()
        printInfix(myStack->top());
        cout << endl;

        cout << "Expression tree in prefix expression: ";
        // call your implementation of printPrefix()
	printPrefix(myStack->top());
        cout << endl;
    }
    else
        cout<< "Size is 0." << endl;
}

//Evaluates tree, returns value
// private calculate() method
int TreeCalc::calculate(TreeNode* ptr) const
{
  if(ptr->left==NULL&&ptr->right==NULL)
    return atoi((ptr->value).c_str());
  else{
    int ret=0;
    switch(ptr->value){
    case '+':
      int l=calculate(ptr->left);
      int r=calculate(ptr->right);
      ret=l+r;
      break;
    case '-':
      int l=calculate(ptr->left);
      int r=calculate(ptr->right);
      ret=l-r;
      break;
    case '*':
      int l=calculate(ptr->left);
      int r=calculate(ptr->right);
      ret=l*r;
      break;
    case '/':
      int l=calculate(ptr->left);
      int r=calculate(ptr->right);
      ret=l/r;
      break;
    }
  return ret;
  }   



 
}

//Calls calculate, sets the stack back to a blank stack
// public calculate() method. Hides private data from user
int TreeCalc::calculate()
{
    int i = 0;
    // call private calculate method here
    i=calculate(myStack->top());
    return i;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TREENODE_H
#define TREENODE_H

#include <string>
using namespace std;

class TreeNode
{
public:
    TreeNode();						//Default Constructor
    TreeNode(const string & val);	//Constructor

private:
    string value;
    TreeNode *left, *right;			// for trees
    friend class TreeCalc;			//gives TreeCalc access to private data
};

#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef TREECALC_H
#define TREECALC_H
#include <stack>

#include "TreeNode.h"
using namespace std;

class TreeCalc
{
public:

    TreeCalc();						//Constructor
    ~TreeCalc();					//Destructor

    void cleanTree(TreeNode * ptr);	//Deletes tree/frees memory
    void readInput();				//gets data from user
    void insert(const string & val);	//puts value in tree

    // print methods
    void printPrefix(TreeNode * curNode) const;	//prints data in prefix form
    void printInfix(TreeNode * curNode) const;	//prints data in infix form
    void printPostfix(TreeNode * curNode) const;//prints data in postfix form
    void printOutput() const;				//prints in pre,in,post form
    int calculate();					//calls private calculate method

private:
    stack<int> myStack;
    int calculate(TreeNode* ptr) const;		//Evaluates tree, returns value

};

#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "TreeNode.h"

//Default Constructor -left and right are NULL, value '?'
TreeNode::TreeNode()
{
    value="?";
    left=NULL;
    right=NULL;
}

//Constructor - sets value to val
TreeNode::TreeNode(const string & val)
{
    value=val;
    left=NULL;
    right=NULL;
}

please note that TreeNode.cpp is not to be altered, and neither is readInput() in TreeCalc.cpp.
What compile errors?

Jim
the full list is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
TreeCalc.cpp: In destructor ‘TreeCalc::~TreeCalc()’:
TreeCalc.cpp:19:20: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp: In member function ‘void TreeCalc::cleanTree(TreeNode*)’:
TreeCalc.cpp:25:18: error: no match foroperator!=’ in ‘ptr->TreeNode::value != 0’
TreeCalc.cpp:25:18: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:132:5: note: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:137:5: note: template<class _Tp> bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/stl_deque.h:259:5: note: template<class _Tp, class _Ref, class _Ptr> bool std::operator!=(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&)
/usr/include/c++/4.6/bits/stl_deque.h:266:5: note: template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool std::operator!=(const std::_Deque_iterator<_Tp, _RefL, _PtrL>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&)
/usr/include/c++/4.6/bits/stl_deque.h:1943:5: note: template<class _Tp, class _Alloc> bool std::operator!=(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_stack.h:265:5: note: template<class _Tp, class _Seq> bool std::operator!=(const std::stack<_Tp, _Seq>&, const std::stack<_Tp, _Seq>&)
/usr/include/c++/4.6/bits/postypes.h:223:5: note: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/ext/new_allocator.h:128:5: note: template<class _Tp> bool __gnu_cxx::operator!=(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
/usr/include/c++/4.6/bits/stl_iterator.h:817:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.6/bits/stl_iterator.h:811:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
TreeCalc.cpp: In member function ‘void TreeCalc::insert(const string&)’:
TreeCalc.cpp:56:17: error: no matching function for call to ‘isdigit(const string&)’
TreeCalc.cpp:56:17: note: candidates are:
/usr/include/ctype.h:115:1: note: int isdigit(int)
/usr/include/ctype.h:115:1: note:   no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘int’
/usr/include/c++/4.6/bits/locale_facets.h:2566:5: note: template<class _CharT> bool std::isdigit(_CharT, const std::locale&)
TreeCalc.cpp:57:32: error: conversion from ‘TreeNode*’ to non-scalar type ‘TreeNode’ requested
TreeCalc.cpp:58:12: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp:61:32: error: conversion from ‘TreeNode*’ to non-scalar type ‘TreeNode’ requested
TreeCalc.cpp:62:6: error: base operand of ‘->’ has non-pointer type ‘TreeNode’
TreeCalc.cpp:62:21: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp:63:12: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp:64:6: error: base operand of ‘->’ has non-pointer type ‘TreeNode’
TreeCalc.cpp:64:20: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp:65:12: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp:66:12: error: base operand of ‘->’ has non-pointer type ‘std::stack<TreeNode*>’
TreeCalc.cpp: In member function ‘void TreeCalc::printPrefix(TreeNode*) const’:
TreeCalc.cpp:82:1: error: a function-definition is not allowed here before ‘{’ token
TreeCalc.cpp:177:1: error: expected ‘}’ at end of input
The first error says that in the TreeCalc::~TreeCalc destructor you're trying the use the pointer dereference operator '->' on something that's not a pointer.

If we look, you have

 
cleanTree(myStack->top());


Following to the declaration of myStack, we find that it's an actual object, not a pointer to an object. So, that code needs to be

 
cleanTree(myStack.top());


I see you've done throughout the code, so fix those up and then move on to the next error.

Which is saying that in TreeCalc::cleanTree it doesn't know how to compare TreeNode::value with zero.

 
if(ptr->value!=NULL){


If we look at the definition of TreeNode::value, it's a string object. You can't compare a string object with a number.

Don't get confused by what's returned when you dereference a pointer.
This code - ptr->value dereferences the ptr pointer and gets the value of the member called value. The actual value - which in this case is a string.

If you're using an empty string to mean "not-initialised" then you can probably just compare the string to "", rather than NULL (which is just another name for zero)

Fix those problems and your error list will be a bit shorter.

Jim
thanks man, this helped a lot.
No worries
Topic archived. No new replies allowed.