Thread: bin-search-tree inorder issue

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    There is nothing wrong with doing it this way. In fact, it allows you to pass extra arguments to the recursive function, for example if you wanted to say control how the recursion is done. It is also common to make the recursive function only visible in the current compilation unit:
    Code:
    static void recurse(const node *const node, const int direction)
    {
        if (direction > 0 && node->left)
            recurse(node->left, direction);
        else
        if (direction < 0 && node->right)
            recurse(node->right, direction);
    
        /* Do this node */
    
        if (direction > 0 && node->right)
            recurse(node->right, direction);
        else
        if (direction < 0 && node->left)
            recurse(node->left, direction);
    }
    
    void inorder_ascending(const b_s_tree *const tree)
    {
        if (tree && tree->root)
            recurse(tree->root, +1);
    }
    
    void inorder_descending(const b_s_tree *const tree)
    {
        if (tree && tree->root)
            recurse(tree->root, -1);
    }
    Note that the recurse() function above is not intended to be called directly by "user" code, only by the inorder_foo() functions. This way you can very easily add instrumentation -- for example, like I showed earlier, printing the entire tree in DOT format for detailed examination of the tree and the traversal process -- without having to modify any code that calls the inorder_foo() functions.

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by Nominal Animal View Post
    There is nothing wrong with doing it this way. In fact, it allows you to pass extra arguments to the recursive function, for example if you wanted to say control how the recursion is done. It is also common to make the recursive function only visible in the current compilation unit:
    Code:
    static void recurse(const node *const node, const int direction)
    {
        if (direction > 0 && node->left)
            recurse(node->left, direction);
        else
        if (direction < 0 && node->right)
            recurse(node->right, direction);
    
        /* Do this node */
    
        if (direction > 0 && node->right)
            recurse(node->right, direction);
        else
        if (direction < 0 && node->left)
            recurse(node->left, direction);
    }
    
    void inorder_ascending(const b_s_tree *const tree)
    {
        if (tree && tree->root)
            recurse(tree->root, +1);
    }
    
    void inorder_descending(const b_s_tree *const tree)
    {
        if (tree && tree->root)
            recurse(tree->root, -1);
    }
    Note that the recurse() function above is not intended to be called directly by "user" code, only by the inorder_foo() functions. This way you can very easily add instrumentation -- for example, like I showed earlier, printing the entire tree in DOT format for detailed examination of the tree and the traversal process -- without having to modify any code that calls the inorder_foo() functions.
    Yeah, that actually makes a lot of sense. Thanks for that mate!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inorder benary search with and with ADT
    By Hadi Abu Maaruf in forum C Programming
    Replies: 8
    Last Post: 06-17-2015, 03:04 PM
  2. recriating a bst tree from inorder
    By cfan in forum C Programming
    Replies: 5
    Last Post: 08-06-2009, 02:15 AM
  3. constarcting tree from its preorder,inorder..
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:05 PM
  4. Displaying tree thru inorder
    By Lord CyKill in forum C Programming
    Replies: 2
    Last Post: 04-12-2003, 10:23 AM
  5. InOrder traversal : show tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-17-2001, 10:38 PM