Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将二叉树非递归地作为后序输出?_C_Runtime Error_Binary Tree_Postorder - Fatal编程技术网

C 如何将二叉树非递归地作为后序输出?

C 如何将二叉树非递归地作为后序输出?,c,runtime-error,binary-tree,postorder,C,Runtime Error,Binary Tree,Postorder,问题描述:我想以非递归方式输出二叉树作为post顺序。程序在运行时发生错误,打印堆栈已满,周期性推送失败。我想问题发生在堆栈中,但我无法解决它 MainFunc.c #include "AllFun.h" int main(int argc, char* argv[]) { BiTree T; T = create(); /* Traverse nonrecursively binary tree as

问题描述:我想以非递归方式输出二叉树作为post顺序。程序在运行时发生错误,打印
堆栈已满,周期性推送失败。我想问题发生在堆栈中,但我无法解决它

MainFunc.c

    #include "AllFun.h"
    int main(int argc, char* argv[])
    {
        BiTree T;
        T = create();
        /* Traverse nonrecursively binary tree as post order */
        PostOrder(T);
        return 0;
    }
其他功能

    #include "AllFun.h"
    BiTree newNode(int value)
    {
        BiTree T = (BiTNode*)malloc(sizeof(BiTNode));
        if (T) {
            T->data = value;
            T->lchild = T->rchild = NULL;
        }
        return T;
    }
    void insert(BiTree* T, int x)
    {
        if (*T == NULL) {
            *T = newNode(x);
            return;
        }
        if (x < (*T)->data) {
            insert(&(*T)->lchild, x);
        }
        else {
            insert(&(*T)->rchild, x);
        }
    }
    BiTree create()
    {
        ElemType nums[MaxSize];
        for (int i = 0; i < MaxSize; ++i) {
            nums[i] = ' ';
        }
        BiTree T = NULL;
        printf("Enter value of node to create a binary search tree: ");
        for (int i = 0; i < MaxSize; ++i) {
            scanf_s("%d", &nums[i]);
            insert(&T, nums[i]);
        }
        return T;
    }
    void init(SqStack *s)
    {
        s->top = -1;
    }
    int isEmpty(SqStack *s)
    {
        int ret = 0;
        if (s->top == -1) {
            ret = 1;
        }
        return ret;
    }
    void push(SqStack *s, BiTree *p)
    {
        if (s->top == MaxSize - 1) {
            printf("stack is full, push fail\n");
        }
        else {
            s->PArray[++s->top] = *p;
        }
        return;
    }
    void getTop(SqStack *s, BiTree *p)
    {
        if (s->top == -1) {
            printf("stack is empty\n");
            return;
        }
        *p = s->PArray[s->top];
        return;
    }
    void pop(SqStack *s, BiTree *p)
    {
        if (s->top == -1) {
            printf("stack is empty, pop fail\n");
        }
        *p = s->PArray[s->top--];
        return;
    }
    void PostOrder(BiTree T)
    {
        BiTree p = T, r = NULL;
        SqStack s;
        init(&s);
        while (p || !isEmpty(&s)) {
            if (p) {
                push(&s, &p);
                p = p->lchild;
            }
            else {
                getTop(&s, &p);
                if (p) {
                    if (p->rchild && p->rchild != r) {
                        p = p->rchild;
                        push(&s, &p);
                        p = p->lchild;
                    }
                }
                else {
                    pop(&s, &p);
                    if (p) {
                        printf("%d ", p->data);
                    }
                    r = p;
                    p = NULL;
                }
            }
        }// while
    }
    #include "AllFun.h"
    BiTree newNode(int value)
    {
        BiTree T = (BiTNode*)malloc(sizeof(BiTNode));
        if (T) {
            T->data = value;
            T->lchild = T->rchild = NULL;
        }
        return T;
    }
    void insert(BiTree* T, int x)
    {
        if (*T == NULL) {
            *T = newNode(x);
            return;
        }
        if (x < (*T)->data) {
            insert(&(*T)->lchild, x);
        }
        else {
            insert(&(*T)->rchild, x);
        }
    }
    BiTree create()
    {
        ElemType nums[MaxSize];
        for (int i = 0; i < MaxSize; ++i) {
            nums[i] = ' ';
        }
        BiTree T = NULL;
        printf("Enter value of node to create a binary search tree: ");
        for (int i = 0; i < MaxSize; ++i) {
            scanf_s("%d", &nums[i]);
            insert(&T, nums[i]);
        }
        return T;
    }
    void init(SqStack *s)
    {
        s->top = -1;
    }
    int isEmpty(SqStack *s)
    {
        int ret = 0;
        if (s->top == -1) {
            ret = 1;
        }
        return ret;
    }
    void push(SqStack *s, BiTree *p)
    {
        if (s->top == MaxSize - 1) {
            printf("stack is full, push fail\n");
        }
        else {
            s->PArray[++s->top] = *p;
        }
        return;
    }
    void getTop(SqStack *s, BiTree *p)
    {
        if (s->top == -1) {
            printf("stack is empty\n");
            return;
        }
        *p = s->PArray[s->top];
        return;
    }
    void pop(SqStack *s, BiTree *p)
    {
        if (s->top == -1) {
            printf("stack is empty, pop fail\n");
        }
        *p = s->PArray[s->top--];
        return;
    }
    void PostOrder(BiTree T)
    {
        BiTree p = T, r = NULL;
        SqStack s;
        init(&s);
        while (p || !isEmpty(&s)) {
            if (p) {
                push(&s, &p);
                p = p->lchild;
            }
            else {
                getTop(&s, &p);
                if (p) {
                    if (p->rchild && p->rchild != r) {
                        p = p->rchild;
                        push(&s, &p);
                        p = p->lchild;
                    }
                }
                else {
                    pop(&s, &p);
                    if (p) {
                        printf("%d ", p->data);
                    }
                    r = p;
                    p = NULL;
                }
            }
        }// while
    }
#包括“AllFun.h”
比特树新节点(int值)
{
比特树T=(比特节点*)malloc(sizeof(比特节点));
if(T){
T->数据=值;
T->lchild=T->rchild=NULL;
}
返回T;
}
空插入(比特树*T,整数x)
{
如果(*T==NULL){
*T=新节点(x);
返回;
}
如果(x<(*T)->数据){
插入(&(*T)->lchild,x);
}
否则{
插入(&(*T)->rchild,x);
}
}
比特树创建()
{
ElemType nums[MaxSize];
对于(int i=0;itop=-1;
}
int isEmpty(SqStack*s)
{
int-ret=0;
如果(s->top==-1){
ret=1;
}
返回ret;
}
无效推送(SqStack*s,BiTree*p)
{
如果(s->top==MaxSize-1){
printf(“堆栈已满,推送失败\n”);
}
否则{
s->PArray[++s->top]=*p;
}
返回;
}
void getTop(SqStack*s,BiTree*p)
{
如果(s->top==-1){
printf(“堆栈为空\n”);
返回;
}
*p=s->PArray[s->top];
返回;
}
void pop(SqStack*s,BiTree*p)
{
如果(s->top==-1){
printf(“堆栈为空,弹出失败\n”);
}
*p=s->PArray[s->top--];
返回;
}
无效邮购(比特树T)
{
比特树p=T,r=NULL;
SQS;
初始化(&s);
而(p||!isEmpty(&s)){
如果(p){
推送(s&p);
p=p->lchild;
}
否则{
getTop(标准普尔);
如果(p){
如果(p->rchild&&p->rchild!=r){
p=p->rchild;
推送(s&p);
p=p->lchild;
}
}
否则{
流行音乐(标准普尔);
如果(p){
printf(“%d”,p->data);
}
r=p;
p=零;
}
}
}//当
}
#包括“AllFun.h”
比特树新节点(int值)
{
比特树T=(比特节点*)malloc(sizeof(比特节点));
if(T){
T->数据=值;
T->lchild=T->rchild=NULL;
}
返回T;
}
空插入(比特树*T,整数x)
{
如果(*T==NULL){
*T=新节点(x);
返回;
}
如果(x<(*T)->数据){
插入(&(*T)->lchild,x);
}
否则{
插入(&(*T)->rchild,x);
}
}
比特树创建()
{
ElemType nums[MaxSize];
对于(int i=0;itop=-1;
}
int isEmpty(SqStack*s)
{
int-ret=0;
如果(s->top==-1){
ret=1;
}
返回ret;
}
无效推送(SqStack*s,BiTree*p)
{
如果(s->top==MaxSize-1){
printf(“堆栈已满,推送失败\n”);
}
否则{
s->PArray[++s->top]=*p;
}
返回;
}
void getTop(SqStack*s,BiTree*p)
{
如果(s->top==-1){
printf(“堆栈为空\n”);
返回;
}
*p=s->PArray[s->top];
返回;
}
void pop(SqStack*s,BiTree*p)
{
如果(s->top==-1){
printf(“堆栈为空,弹出失败\n”);
}
*p=s->PArray[s->top--];
返回;
}
无效邮购(比特树T)
{
比特树p=T,r=NULL;
SQS;
初始化(&s);
而(p||!isEmpty(&s)){
如果(p){
推送(s&p);
p=p->lchild;
}
否则{
getTop(标准普尔);
如果(p){
如果(p->rchild&&p->rchild!=r){
p=p->rchild;
推送(s&p);
p=p->lchild;
}
}
否则{
流行音乐(标准普尔);
如果(p){
printf(“%d”,p->data);
}
r=p;
p=零;
}
}
}//当
}
好好玩

    #include <stddef.h>   // NULL
    #include <stdlib.h>   //malloc()
    #include <stdio.h>
    #define MaxSize 10
    typedef int ElemType;
    typedef struct BiTNode {
        ElemType data;
        struct BiTNode* lchild, * rchild;
    } BiTNode, * BiTree;
    typedef struct Stack {
        BiTNode *PArray[100];
        int top;
    } SqStack;
    BiTree create();
    void PostOrder(BiTree T);
#包含//空
#包括//malloc()
#包括
#定义最大尺寸10
typedef int ElemType;
类型定义结构位节点{
元素类型数据;
结构BiTNode*lchild,*rchild;
}位节点,*位树;
typedef结构堆栈{
比特节点*阵列[100];
int top;
}SqStack;
BiTree create();
无效邮购(比特树T);

当没有合适的子对象下降时,
else
子句必须补充条件
if(p->rchild&&p->rchild!=r)
,在这里您下降到右侧,但在您的代码中,它补充了
if(p)
,这应该总是正确的