Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. expression Tree

expression Tree

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsdata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    volvox21
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • V volvox21

      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.

      S Offline
      S Offline
      Sauro Viti
      wrote on last edited by
      #2

      Please, don't cross post. You have already posted the same question on Q&A (and you got an answer there).

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups