1

Can anyone help to to create a binary tree and do a non recursive preorder traversal of the binary tree in c?

1
  • Use a local dynamic stack. Commented Mar 25, 2015 at 19:38

3 Answers 3

1

You might be able to use a variant of a threaded binary tree; use the right link of the leaf nodes to point to the next node in the traverse, something like

                    +---+
                    | A |
                    +---+
                   /     \
              +---+       +---+
              | B |       | E |
              +---+       +---+
             /     \      ^
        +---+       +---+ |
        | C |       | D | |
        +---+       +---+ |
            |       ^   | |
            +-------+   +-+

The leaf node C explicitly points to D, which is the next node in the preorder traverse, and D explicitly points to E. This makes insertions and deletions a bit more of a pain, but it gives you an easy preorder traverse without recursion and without an auxiliary stack.

Sign up to request clarification or add additional context in comments.

Comments

0

As pre-order can be done via depth-first search, it could be done in this way; note that depth-first search is a recursive approach. That being said, I does not need to be implemented by recursive function calls but can be implemented by using a stack as an auxiliary data structure, which effectively is used to generate the visiting sequence which would otherwise be generated by recursion. In pseudocode, this can be done as follows, where visit would be the function to actually visit a node.

push the root of the tree to the stack;
while (stack is not empty)
{
    Node = top of stack;
    visit Node;
    if Node has unvisited left child
        push left child of Node
    else if right child of Node is Not visited
        push right child of Node
    else
        pop;
}

Comments

0

I found this code :

void iterativePreorder(node *root)
{
     // Base Case
    if (root == NULL)
       return;

    // Create an empty stack and push root to it
    stack<node *> nodeStack;
    nodeStack.push(root);

    /* Pop all items one by one. Do following for every popped item
       a) print it
       b) push its right child
       c) push its left child
    Note that right child is pushed first so that left is processed first */
    while (nodeStack.empty() == false)
    {
        // Pop the top item from stack and print it
        struct node *node = nodeStack.top();
        printf ("%d ", node->data);
        nodeStack.pop();

        // Push right and left children of the popped node to stack
        if (node->right)
            nodeStack.push(node->right);
        if (node->left)
            nodeStack.push(node->left);
    }
}

here: http://www.geeksforgeeks.org/iterative-preorder-traversal/

I am aware that this code is in C++ but you can create a simple stack set of functions in C to bypass that problem.

If needed this link can help with that too :

http://groups.csail.mit.edu/graphics/classes/6.837/F04/cpp_notes/stack1.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.