expression Tree
-
I wrote a expression tree and preorder traversal , but this doesn't work. The problem is probably related to pointers(*left and *right) But I haven't found anything wrong. Code is here;
#include <iostream>
#include <string>
#include <vector>
#include "Node.h"using namespace std;
void preorderTraversal(Node *root){
if(root != NULL){
cout << root->c;
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}int main() {
string s = "ab+cde+\*\*"; vector<Node> lst; Node \*left ; Node \*right ; for(int i = 0; i < s.size(); i++){ bool b =((s\[i\]=='\*') || (s\[i\]=='+') || (s\[i\]=='-') || (s\[i\]=='\\\\') || (s\[i\]=='^')); if(b){ left = new Node(); right = new Node(); \*right = lst.at(lst.size()-1); lst.pop\_back(); \*left = lst.at(lst.size()-1); lst.pop\_back(); lst.push\_back(Node(s\[i\],left,right)); delete left; delete right; left = NULL; right = NULL; }else{ lst.push\_back(Node(s\[i\])); } } cout << lst.size()<<endl; preorderTraversal(&lst.back()); return 0;
}
#define NODE_H_
using namespace std;
#include <iostream>
class Node {
public:
Node();
Node(char c, Node *left, Node *right);
Node(char c);
~Node();
char c;
Node *left;
Node *right;
private:
};
#endif /* NODE_H_ */#include "Node.h"
Node::Node() {
// TODO Auto-generated constructor stub
this->c = NULL;
left = NULL;
right = NULL;}
Node::Node(char c){
this->c = c;
left = NULL;
right = NULL;
}
Node::Node(char c, Node *left, Node *right){
this->c = c;
this->left = left;
this->right = right;
}
Node::~Node() {
// TODO Auto-generated destructor stub
}If anyone can help with this I would really appreciate it.
-
I wrote a expression tree and preorder traversal , but this doesn't work. The problem is probably related to pointers(*left and *right) But I haven't found anything wrong. Code is here;
#include <iostream>
#include <string>
#include <vector>
#include "Node.h"using namespace std;
void preorderTraversal(Node *root){
if(root != NULL){
cout << root->c;
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}int main() {
string s = "ab+cde+\*\*"; vector<Node> lst; Node \*left ; Node \*right ; for(int i = 0; i < s.size(); i++){ bool b =((s\[i\]=='\*') || (s\[i\]=='+') || (s\[i\]=='-') || (s\[i\]=='\\\\') || (s\[i\]=='^')); if(b){ left = new Node(); right = new Node(); \*right = lst.at(lst.size()-1); lst.pop\_back(); \*left = lst.at(lst.size()-1); lst.pop\_back(); lst.push\_back(Node(s\[i\],left,right)); delete left; delete right; left = NULL; right = NULL; }else{ lst.push\_back(Node(s\[i\])); } } cout << lst.size()<<endl; preorderTraversal(&lst.back()); return 0;
}
#define NODE_H_
using namespace std;
#include <iostream>
class Node {
public:
Node();
Node(char c, Node *left, Node *right);
Node(char c);
~Node();
char c;
Node *left;
Node *right;
private:
};
#endif /* NODE_H_ */#include "Node.h"
Node::Node() {
// TODO Auto-generated constructor stub
this->c = NULL;
left = NULL;
right = NULL;}
Node::Node(char c){
this->c = c;
left = NULL;
right = NULL;
}
Node::Node(char c, Node *left, Node *right){
this->c = c;
this->left = left;
this->right = right;
}
Node::~Node() {
// TODO Auto-generated destructor stub
}If anyone can help with this I would really appreciate it.
Please, don't cross post. You have already posted the same question on Q&A (and you got an answer there).