简体   繁体   English

C 二叉搜索树后序遍历迭代使用堆栈

[英]C Binary Search Tree Post Order Traversal Iteratively using Stack

I've spent hours trying to figure out why it wouldn't print the root node at the end.我花了几个小时试图弄清楚为什么它不会在最后打印根节点。

It is unable to current = pop(&S);它无法current = pop(&S); at the last iteration.在最后一次迭代中。

I can't figure out what's wrong.我不知道出了什么问题。

Algorithm:算法:

1.1 Create an empty stack 2.1 Do following while root is not NULL a) Push root's right child and then root to stack. 1.1 创建一个空栈 2.1 当 root 不是 NULL 时执行以下操作 a) 将 root 的右孩子然后 root 压入堆栈。 b)Set root as root's left child. b) 将 root 设置为 root 的左孩子。 2.2 Pop an item from stack and set it as root. 2.2 从堆栈中弹出一个项目并将其设置为根。 a) If the popped item has a right child and the right child is at top of stack, then remove the right child from stack, push the root back and set root as root's right child. a) 如果弹出的项目有一个右孩子并且右孩子在栈顶,则从堆栈中删除右孩子,将根推回并将根设置为根的右孩子。 b) Else print root's data and set root as NULL. 2.3 Repeat steps 2.1 and 2.2 while stack is not empty. b) 否则打印 root 的数据并将 root 设置为 NULL。 2.3 当堆栈不为空时重复步骤 2.1 和 2.2。

I visualized, and drew it on a whiteboard and the logic works fine to me.我想象了一下,并将其画在白板上,逻辑对我来说很好用。 However when implementing it, it crashes at the last iteration.但是,在实施它时,它会在最后一次迭代时崩溃。

void postOrderIterativeS1(BSTNode *root)
{
    Stack S;
    S.top = NULL;
    BSTNode *current = root;

    int shouldContinue = 1;
    while(shouldContinue)
    {

        if(current != NULL)
        {

            if(current->right != NULL){


            push(&S, current->right);
            }

            push(&S, current);



            current = current->left;
        }
        else
        {
            current = pop(&S);
            if(peek(&S) == NULL){
                shouldContinue = 0;
            }
            if(current->right != NULL && peek(&S)->item == current->right->item)
            {
                pop(&S);
                push(&S,current);
                current = current->right;
            }

            else


            {

                int items= current->item;
                printf("%d ", current->item);
                current = NULL;
            }

        }

    }
}

If you want the full code, https://pastebin.com/z4rPebrJ如果你想要完整的代码, https://pastebin.com/z4rPebrJ

Exeuction:执行:

在此处输入图像描述

There is at least a problem here:这里至少有一个问题:

  current = pop(&S);
  if (peek(&S) == NULL) {
    shouldContinue = 0; 
    // peek(&S) is NULL here 
    // but in the if below you dereference peek(&S) which is NULL
    // and therefore you get a crash
  }
  if (current->right != NULL && peek(&S)->item == current->right->item)
  {
    pop(&S);
    push(&S, current);
    current = current->right;
  }

Just change this:只需更改此:

if (current->right != NULL && peek(&S)->item == current->right->item)

to this:对此:

if (shouldContinue && current->right != NULL && peek(&S)->item == current->right->item)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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