我有这样的bitree preorder遍历算法
void Postorder_stack(BiTree T)
{
sqstack s,k;
InitStack(&s);
InitStack(&k);
BiTree p=T;
push(&s,p);
while(!StackEmpty(s))
{
pop(&s,&p);
push(&k,p);
if(p->lch!=NULL);
push(&s,p->lch);
if(p->rch!=NULL)
push(&s,p->rch);
}
while(!StackEmpty(k))
{
pop(&k,&p);
printf("%c",p->data);
}
}
当我使用gdb逐步调试时,我发现NULL T推入堆栈,例如:
(gdb) p s
$6 = {top = 0x603178, base = 0x603170, stacksize = 10}
(gdb) p p.lch
$7 = (BiNode *) 0x0
(gdb) step
push (s=0x7fffffffddc0, e=0x0) at test_bitree.cpp:55 //p.lch is NULL but it push into stack
55 *((*s).top)++=e;
(gdb) p e
$8 = (BiTree) 0x0
(gdb) step
58 }
(gdb)
Breakpoint 3, Postorder_stack (T=0x603010) at test_bitree.cpp:136
136 if(p->rch!=NULL)
(gdb) p p->rch
$9 = (BiNode *) 0x0
(gdb) p s
$10 = {top = 0x603180, base = 0x603170, stacksize = 10}
(gdb) step
我使用'abd ## e ## c ##'进行测试,我把整个程序放在这里 谢谢!
#include <stdlib.h>
#include <stdio.h>
struct BiNode {
char data;
struct BiNode *lch;
struct BiNode *rch;
}BiNode;
typedef struct BiNode *BiTree;
struct sqstack{
BiTree *top,*base;
int stacksize;
};
void InitStack(sqstack *s)
{
(*s).base=(BiTree*)malloc(10*sizeof(BiTree));
(*s).top=(*s).base;
(*s).stacksize=10;
}
int StackEmpty(sqstack s)
{
if(s.top==s.base)
return 1;
else return 0;
}
int Gettop(sqstack s,BiTree *e)
{
if(s.top>s.base)
{
*e=*(s.top-1);
return 1;
}
else return 0;
}
void push(sqstack *s,BiTree e)
{
*((*s).top)++=e;
// *(*s).top=e;
// *((*s).top)++;
}
int pop(sqstack *s,BiTree *e)
{
if((*s).top==(*s).base)
return 0;
else{
*e=*--(*s).top;
return 1;
}
}
void CreatBitree(BiTree *T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
*T=NULL;
else
{
*T=(BiTree)malloc(sizeof(BiNode));
(*T)->data=ch;
CreatBitree(&(*T)->lch);
CreatBitree(&(*T)->rch);
}
}
void Preorder_stack(BiTree T)
{
BiTree p=T;
sqstack s;
InitStack(&s);
push(&s,T);
while(!StackEmpty(s))
{
while(Gettop(s,&p)&&p)
{
printf("%c",p->data);
push(&s,p->lch);
}
pop(&s,&p);
if(!StackEmpty(s))
{
pop(&s,&p);
push(&s,p->rch);
}
}
}
void Inorder_stack(BiTree T)
{
sqstack s;
BiTree p;
InitStack(&s);
push(&s,T);
while(!StackEmpty(s))
{
while(Gettop(s,&p)&&p)
push(&s,p->lch);
pop(&s,&p);
if(!StackEmpty(s))
{
pop(&s,&p);
printf("%c",p->data);
push(&s,p->rch);
}
}
}
void Postorder_stack(BiTree T)
{
sqstack s,k;
InitStack(&s);
InitStack(&k);
BiTree p=T;
push(&s,p);
while(!StackEmpty(s))
{
pop(&s,&p);
push(&k,p);
if(p->lch!=NULL);
push(&s,p->lch);
if(p->rch!=NULL)
push(&s,p->rch);
}
while(!StackEmpty(k))
{
pop(&k,&p);
printf("%c",p->data);
}
}
void InitBitree (BiTree *T)
{
*T=NULL;
}
int main()
{
BiTree T;
InitBitree(&T);
CreatBitree(&T);
Preorder_stack(T);
Inorder_stack(T);
Postorder_stack(T);
}
答案 0 :(得分:4)
下面:
if(p->lch!=NULL);
// right ^ here
push(&s,p->lch);
你有一个不受欢迎的分号会导致没有任何保护的推送。你需要:
if(p->lch!=NULL)
push(&s,p->lch);