简体   繁体   中英

Binary tree traversal without using recursion

任何人都可以帮助创建二叉树并在c中对二叉树进行非递归的预遍历吗?

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.

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM