Thread: Binary search tree

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    You may try a more sanitized wording, to help you understand better what your are trying to achieve.

    Perhaps something along the following lines...

    Code:
    typedef struct TreeNode TreeNode;
    struct TreeNode{
        int val;
        TreeNode *left;
        TreeNode *right;
    };
    
    typedef struct StackNode StackNode;
    struct StackNode {
        TreeNode  *treenode;
        StackNode *prev;
    };
    
    /* this is not really needed, but I followed your example */
    typedef struct Stack Stack;
    struct Stack {
        int nelems;
        StackNode *top;
    };
    Please note that forward declarations with typedef's are optional in your case (for example, it's not that you are implementing a library with opaque objects), but they help in writing shorter code later on.

    Then define the basic operations for your stack. For example...
    Code:
    void        stack_push( Stack *stack, const TreeNode *treenode );
    TreeNode   *stack_pop( Stack *stack );
    int         stack_empty( const Stack *stack );
    void        stack_destroy( Stack *stack );
    Finally define your stack, lets say...
    Code:
    ...
    int main( void )
    {
        Stack s = {0, NULL};
        ...
    and you should be good to go.

    PS. Btw, hi everybody, this is my first post in the forum.
    Last edited by migf1; 05-21-2013 at 04:50 PM. Reason: typos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Binary Search Tree
    By Pyrce in forum C Programming
    Replies: 1
    Last Post: 09-23-2005, 06:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM