#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define status int
typedef struct bitnode
{
char data;
struct bitnode *next;
struct bitnode *lchild;
struct bitnode *rchild;
}linkstack,*sq;
int r=0;
linkstack *q;
status initstack(sq &S)
{
S=(sq)malloc(sizeof(sq));
S->next=NULL;
return 0;
}
void createbitree(sq &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
T=NULL;
else
{
T=(bitnode *)malloc(sizeof(bitnode));
T->data=ch;
if(r==0)
{
q=T;
r=1;
}
createbitree(T->lchild);
createbitree(T->rchild);
}
}
sq push(sq &S,linkstack *p)
{
p->next=S->next;
S->next=p;
return p;
}
sq pop(sq &S,linkstack *p)
{
p=S->next;
S->next=p->next;
return p;
}
status emptystack(sq &S)
{
if(S->next==NULL)
return 1;
else
return 0;
}
void inorder(linkstack *q,sq &S)
{
linkstack *p;
linkstack *g;
initstack(S);
p=q;
while(!emptystack(S)||p)
{
if(p)
{
g=push(S,p);
p=g->lchild;
}
else
{
g=pop(S,p);
printf("%c ",g->data);
p=g->rchild;
}
}
}
int main()
{
sq t;
sq s;
printf("create bitree:");
createbitree(t);
inorder(q,s);
return 0;
}
二叉树非递归遍历之中序遍历
最新推荐文章于 2022-12-20 17:50:43 发布