二叉树遍历完整程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// BitTree.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct BitNode{
char data;
BitNode *lchild,*rchild;
}BitNode,*BitTree,ElemType; //树的结构体定义
typedef struct node{
ElemType stack;
struct node *next;
}linkstack; //栈的结构体定义
typedef struct QNode
{
BitNode data;
struct QNode *next;
} QNode,*QueuePtr; //队列节点类型typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue; //队列的头尾指针
void InitQueue(LinkQueue *Q) //创建队列
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
Q->rear->next=NULL;
}
void EnQueue(LinkQueue *Q,BitNode e) //入队操作
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
BitNode DeQueue(LinkQueue *Q)//出对操作
{
BitNode e;QueuePtr p;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
return (e);
}
int QueueEmpty(LinkQueue *Q)//判断队列是否为空
{
if(Q->front==Q->rear )
return 1;
else
return 0;
}
int initstack(linkstack **s) //初始化一个带头结点的空栈
{
*s=(linkstack *)malloc(sizeof(linkstack));
if(*s==NULL)exit(-1);
(*s)->next=NULL;
return 1;
}
int push(linkstack *s,ElemType *x) //入栈操作,将x的数据元素插入栈s中,使x
{ 成为新的栈顶元素linkstack *p,*q;
q=s;
p=(linkstack *)malloc(sizeof(linkstack));
if(!p) exit(-1);
p->stack=*x;
p->next=NULL;
while(q->next)q=q->next;
q->next=p;
return 1;
}
int pop(linkstack *s,ElemType *e) //出栈操作,先将栈s的栈顶结点的值送到e所指向{ 的内存单元,然后删除栈顶结点linkstack *p,*q;
p=s;
if(s->next==NULL) return 0;
while(p->next)
{
q=p;
p=p->next;
}
q->next=NULL;
*e=p->stack;
free(p);
return 1;
}
int emptystack(linkstack *s) //判断栈是否为空{
if(s->next==NULL)
return 1;
else return 0;
}
int gettop(linkstack *s,ElemType *e)
{
linkstack *p,*q;
p=s;
if(s->next==NULL) return 0;
while(p->next)
{
q=p;
p=p->next;
}
*e=p->stack;
return 1;
}
BitTree CreateBiTree()
{
BitTree bt;
char x;
scanf("%c",&x);
if(x=='#')
bt=NULL;
else
{
bt=(BitTree)malloc(sizeof(BitNode));
bt->data=x;
bt->lchild=CreateBiTree();