当前位置:文档之家› 遍历二叉树(递归+非递归)实验资料报告材料

遍历二叉树(递归+非递归)实验资料报告材料

实验报告
附:源程序:
递归算法程序
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define maxsize 100
#define FALSE 0
#define TRUE 1
typedef struct node //二叉树结构体类型定义{
char data;
struct node *lchild;
struct node *rchild;
}bitnode,*bitree;
/*扩展先序序列创建二叉链表*/
void cteatebitree(bitree *bt)
{
char ch;
ch=getchar();
if(ch=='.')*bt=NULL;
else
{
*bt=(bitree)malloc(sizeof(bitnode));
(*bt)->data=ch;
cteatebitree(&((*bt)->lchild));
cteatebitree(&((*bt)->rchild));
}
}
/*先序递归遍历*/
void preorder(bitree root)
{
if(root!=NULL)
{
printf("%c ",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
/*中序递归遍历*/
void inorder(bitree root)
{
if(root!=NULL)
{
preorder(root->lchild);
printf("%c ",root->data);
preorder(root->rchild);
}
}
/*后序递归遍历*/
void postorder(bitree root)
{
if(root!=NULL)
{
preorder(root->lchild);
preorder(root->rchild);
printf("%c ",root->data);
}
}
void main()
{
bitree bt;
cteatebitree(&bt);
printf("先序递归遍历序列:\n");
preorder(bt);
printf("\n");
printf("中序递归遍历序列:\n");
inorder(bt);
printf("\n");
printf("后序递归遍历序列:\n");
postorder(bt);
printf("\n");
}
非递归算法程序
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define FALSE 0
#define TRUE 1
#define maxsize 100
typedef struct node //二叉树结构体定义{
char data;
struct node *lchild;
struct node *rchild;
}bitnode,*bitree;
typedef struct //顺序栈结构体定义{
bitree elem[maxsize];
int top;
}seqstack;
int push(seqstack *s,bitree x) //入栈
{
if(s->top==maxsize-1)
return(FALSE);
s->top++;
s->elem[s->top]=x;
return(TRUE);
}
bitree pop(seqstack *s,bitree x) //出栈
{
if(s->top==-1) return NULL;
else
{
x=s->elem[s->top];
s->top--;
return x;
}
}
int gettop(seqstack *s,bitree *x) //读取栈顶元素
{
if(s->top==-1) return FALSE;
else
{
*x=s->elem[s->top];
return TRUE;
}
}
void createbitree(bitree *bt) //扩展先序序列创建二叉链表{
char ch;
ch=getchar();
if(ch=='.')*bt=NULL;
else
{
*bt=(bitree)malloc(sizeof(bitnode));
(*bt)->data=ch;
createbitree(&((*bt)->lchild));
createbitree(&((*bt)->rchild));
}
}
void preorder1(bitree root,seqstack s) //先序遍历
{
bitnode *p;
p=root;
while(p!=NULL||!(s.top==-1))
{
while(p!=NULL)
{
printf("%c",p->data);
push(&s,p);
p=p->lchild;
}
if(!(s.top==-1))
{
p=pop(&s,p);
p=p->rchild;
}
}
}
void inorder1(bitree root,seqstack s) //中序遍历{
bitnode *p;
s.top=-1;
p=root;
while(p!=NULL||!(s.top==-1))
{
if(p!=NULL)
{
push(&s,p);
p=p->lchild;
}
else
{
p=pop(&s,p);
printf("%c",p->data);
p=p->rchild;
}
}
}
void postorder1(bitree root) //后序遍历{
bitnode *p,*q;
seqstack s;
q=NULL;
p=root;
s.top=-1;
//printf("%c",p->data);
while(p!=NULL||!(s.top==-1))
{while(p!=NULL)
{
push(&s,p); p=p->lchild;
}
if(!(s.top==-1))
{
gettop(&s,&p);
if((p->rchild==NULL)||(p->rchild==q))
{
printf("%c",p->data);
q=p;
p=pop(&s,p);
p=NULL;
}
else p=p->rchild;
}
}
}
void main()
{
printf("先序序列创建二叉树\n");
seqstack s;
s.top=-1;
bitree root;
createbitree(&root);
printf("先序遍历序列:\n");
preorder1(root,s);
printf("\n");
printf("中序遍历序列:\n");
inorder1(root,s);
printf("\n");
printf("后序遍历序列:\n");
postorder1(root);
printf("\n");
}。

相关主题