How would you push a binary tree onto a stack?
-
I'm working on a binary expression tree parser and cannot figure out how to call the stack push method with a binary tree of only one node. I tried this, but got a segmentation fault: stack theStack; BinaryTree* tree1; tree1 = new BinaryTree ('A'); theStack.push(*tree1); As you can see, my BinaryTree uses only constructors to insert values (prevents the need for helper functions). How would you push a binary tree onto a stack using these .h files? (i'm using c++ of course): /*********BinaryTree.h*********/ #include #include template class BinaryTree { private: template struct Tree_Node { Node_Type Node_Info; BinaryTree * left; BinaryTree * right; }; Tree_Node * root; // prints value within a given node to screen void visit(); // helper function for remove Node_Type replace(BinaryTree *whichTree); public: // function: sets root to NULL BinaryTree(); // function: sets Node_Info=nodeValue, left=NULL, // and right=NULL BinaryTree(Node_Type nodeValue); // function: set Node_Info=nodeValue, left=leftTree, // and right=rightTree BinaryTree(Node_Type nodeValue, BinaryTree * leftTree, BinaryTree * rightTree); // function: class destructor ~BinaryTree(); // function: accepts one parameter of Node_Type and // removes the node with that value BinaryTree * remove(Node_Type nodeValue); // function: accept one parameter of Node_Type and // return true if that value is in the tree // otherwise false is returned bool search(const Node_Type nodeValue); // function: returns true if root=NULL,otherwise, // false is returned bool isEmpty(); // function: performs an inOrder traversal of a tree void inOrder(); // function: performs a preOrder traversal of a tree void preOrder(); // function: performs a postOrder traversal of tree void postOrder(); }; /******************************************************/ /********Stack.h*********/ template struct Element { eType StackElement; Element* Next; }; template class stack { private: Element* Top_Pointer; public: stack(); // Class constructor ~stack(); // Class destructor bool isEmpty() const; // Function: Determines whether the stack