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. Other Discussions
  3. Article Writing
  4. How would you push a binary tree onto a stack?

How would you push a binary tree onto a stack?

Scheduled Pinned Locked Moved Article Writing
data-structuresc++tutorialquestionlearning
1 Posts 1 Posters 1 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.
  • J Offline
    J Offline
    jimekrakorn
    wrote on last edited by
    #1

    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

    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