C 无递归的后订单打印

C 无递归的后订单打印,c,C,这就是我目前所拥有的。我真的不明白我哪里出错了。它需要在访问最左边的节点后进行打印,然后右键再打印 void printPostOrderIterative(BSTNode *root) { BSTNode *cur; Stack s; s.top = NULL; Stack output; output.top = NULL; if (!root) { return; } push(&s, root)

这就是我目前所拥有的。我真的不明白我哪里出错了。它需要在访问最左边的节点后进行打印,然后右键再打印

void printPostOrderIterative(BSTNode *root)
{
    BSTNode *cur;
    Stack s;
    s.top = NULL;
    Stack output;
    output.top = NULL;

    if (!root) {
        return;
    }


    push(&s, root);

    while (isEmpty(&s) != 1 ) {
        cur = s.top;
        push(&output,cur);
        pop(&s);
        if (cur->left){
            push(&s, cur->left);
        }
        if (cur->right){
            push(&s, cur->right);
        }
    }
    while (isEmpty(&output) != 1) {
        printf("%d", output.top);
        pop(&output);
    }
}

目前,代码只是在第一个while循环中保持循环。

以下是一些工作代码。我必须实现与堆栈代码和BST代码的近似,但我只对函数做了非常简单的更改。有一些论点认为我不应该进行创建:堆栈溢出鼓励您创建MCVE SSCCE-同一基本理念的两个名称和链接

在没有任何主要算法调整的情况下,代码a完成,b在一个样本数据集上生成与递归后序打印函数相同的答案

static void printPostOrderIterative(BSTNode *root)
{
    if (!root)         // Moved to avoid stack initialization before returning
        return;        // …ditto…

    Stack s;
    Stack output;
    init(&s);         // Different initialization
    init(&output);    // Different initialization
    push(&s, root);

    while (isEmpty(&s) != 1)
    {
        BSTNode *cur = top(&s);    // Different code
        push(&output, cur);
        pop(&s);
        if (cur->left)
            push(&s, cur->left);
        if (cur->right)
            push(&s, cur->right);
    }

    while (isEmpty(&output) != 1)
    {
        BSTNode *c = top(&output);  // Different code
        printf(" %d", c->data);     // Added space
        pop(&output);
    }
    putchar('\n');       // Extra code

    clean(&s);           // Extra code
    clean(&output);      // Extra code
}
因此,问题可能不在您显示的代码中,而更可能在您未显示的代码中。我最好的猜测是堆栈实现有点错误,但由于您没有显示它,所以很难确定

完整代码 讨论 因为您使用了交错声明和语句,所以至少使用了C99,所以我使用了和bool类型。至少在使用Mac OS X 10.11.4、GCC 5.3.0选项编译时,此代码的编译是干净的:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror nr-post.c -o nr-post
$
该代码在3.12.0-SVN下干净地运行

输出的第一行按顺序打印,因为树是由随机生成的0..99范围内的30个整数组成的。这让我看到,这棵树是从地上正确建造的。最后两行输出首先来自printPostOrderRecursive,这是一个简单的后序打印递归实现,然后来自我对您的printPostOrderRecursive的轻微修改

如果没有可用的堆栈实现,我不得不猜测您在其中的某个地方遇到了问题。使用如图所示的堆栈的直接实现,顶部是节点指针数组的索引,代码就可以了。我们无法判断您是否忽略了编译警告


嗯,回顾我的代码,我发现它没有检查top是否返回非空指针。添加断言和运行时错误,而不是返回null,这很有诱惑力。还有很多其他断言可以添加,特别是传递给堆栈函数的非空堆栈指针。

什么不起作用?只是在循环@Shiven时在第一个循环中保持循环
 71
 57 71
 4 57 71
 4 31 57 71
 4 31 47 57 71
 4 31 47 57 65 71
 4 31 47 57 65 69 71
 4 31 47 57 65 65 69 71
 4 31 47 57 65 65 69 71 98
 4 31 47 57 65 65 69 71 81 98
 4 31 47 54 57 65 65 69 71 81 98
 4 31 47 54 57 58 65 65 69 71 81 98
 4 17 31 47 54 57 58 65 65 69 71 81 98
 4 17 31 37 47 54 57 58 65 65 69 71 81 98
 4 17 31 37 47 48 54 57 58 65 65 69 71 81 98
 4 17 31 37 47 48 54 57 58 64 65 65 69 71 81 98
 4 17 31 37 47 48 54 57 58 64 64 65 65 69 71 81 98
 4 17 31 37 47 48 54 57 58 64 64 65 65 69 71 81 93 98
 4 17 31 37 40 47 48 54 57 58 64 64 65 65 69 71 81 93 98
 4 17 22 31 37 40 47 48 54 57 58 64 64 65 65 69 71 81 93 98
 4 17 22 31 37 40 45 47 48 54 57 58 64 64 65 65 69 71 81 93 98
 4 17 22 31 37 40 45 47 48 54 57 58 62 64 64 65 65 69 71 81 93 98
 4 17 22 31 37 40 45 47 48 54 57 58 62 64 64 65 65 69 71 81 93 98 99
 4 17 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 69 71 81 93 98 99
 4 17 18 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 69 71 81 93 98 99
 4 17 18 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 69 71 81 86 93 98 99
 4 17 18 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 67 69 71 81 86 93 98 99
 4 17 18 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 67 69 71 81 86 93 98 99 99
 4 17 18 22 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 67 68 69 71 81 86 93 98 99 99
 4 17 18 22 27 31 37 40 45 47 47 48 54 57 58 62 64 64 65 65 67 68 69 71 81 86 93 98 99 99
 18 27 22 17 45 40 37 47 48 54 47 31 4 62 64 64 58 68 67 65 69 65 57 86 93 81 99 99 98 71
 18 27 22 17 45 40 37 47 48 54 47 31 4 62 64 64 58 68 67 65 69 65 57 86 93 81 99 99 98 71
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror nr-post.c -o nr-post
$