实现了顺序存储结构的二叉树和队列的基本操作,以下是相关函数及类型的声明
http://www.cnblogs.com/Alex-bg/archive/2012/08/12/2634203.html
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <math.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define MAX_TREE_SIZE 100 #define MAX_QUEUE_SIZE 5 #define Nil ' ' typedef char TElemtype; //将树的结点类型设置为字符型 typedef int QElemType; //将队列的结点类型设置为整形 typedef struct{ QElemType *base; //队列的基地址 int front; int rear; }SqQueue; typedef TElemtype SqBiTree[MAX_TREE_SIZE]; //定义顺序存储二叉树的结构数组 typedef struct{ int level; //树的层号 int order; //一层中的序号(从左向右) }position; /**-------队列的函数声明------*/ int InitQueue(SqQueue *); //初始化队列 int DestroyQueue(SqQueue *); //销毁队列 int ClearQueue(SqQueue *); //清空队列 int QueueEmpty(SqQueue *); //队列是否为空 int GetQueueLength(SqQueue *); //获取队列的长度 int GetQueueHead(SqQueue *,QElemType *); //获取队头元素 int EnterQueue(SqQueue *,QElemType); //向队列中插入元素 int DeleteQueue(SqQueue *,QElemType *); //向队列中删除元素 int (*QueueVisit)(QElemType); //函数变量 int QueueTraverse(SqQueue *,int (*) (QElemType)); //遍历队列 /*---------二叉树的函数声明*/ int InitBiTree(SqBiTree); //初始化二叉树 int CreateBiTree(SqBiTree); //创建二叉树 int BiTreeEmpty(SqBiTree); //判断二叉树是否为空 int BiTreeDepth(SqBiTree); //计算二叉树的深度 int GetRoot(SqBiTree,TElemtype *); //获取二叉树的根结点 TElemtype GetNodeValue(SqBiTree,position); //获取二叉树结点元素 int Assign(SqBiTree,position,TElemtype); //给结点元素赋值 TElemtype GetParent(SqBiTree,TElemtype); //获取结点的双亲结点 TElemtype GetLeftChild(SqBiTree,TElemtype); //获取结点的左孩子 TElemtype GetRightChild(SqBiTree,TElemtype); //获取结点的右孩子 TElemtype GetLeftSibling(SqBiTree,TElemtype); //获取结点的左兄弟 TElemtype GetRightSibling(SqBiTree,TElemtype); //获取结点的右兄弟 void Move(SqBiTree,int,SqBiTree,int); //移动结点 int InsertChild(SqBiTree,TElemtype,int,SqBiTree); //插入子结点 int DeleteChild(SqBiTree,position,int); //删除子结点 int (*TreeVisit)(TElemtype); //函数变量 int PreOrderTraverse(SqBiTree, int (*Visit)(TElemtype)); //先序遍历二叉树 int InOrderTraverse(SqBiTree,int (*Visit)(TElemtype)); //中序遍历二叉树 int PostOrderTraverse(SqBiTree,int (*Visit)(TElemtype)); //后序遍历二叉树 void LevelOrderTraverse(SqBiTree,int (*Visit)(TElemtype)); //层序遍历二叉树 void PrintTree(SqBiTree); //打印二叉树 *-----------队列的函数定义-----------*/ /*初始化队列*/ int InitQueue(SqQueue *Q) { //分配内存区域 Q->base=(QElemType *)malloc(MAX_QUEUE_SIZE*sizeof(QElemType)); if(!Q->base) { printf("存储分配失败. "); return ERROR; } Q->front=Q->rear=0; //将队列置空 return OK; } /*销毁队列*/ int DestroyQueue(SqQueue *Q) { if(Q->base) { free(Q->base); } Q->front=Q->rear=0; return OK; } /*清空队列*/ int ClearQueue(SqQueue *Q) { Q->front=Q->rear=0; return OK; } /*判断队列是否为空*/ int QueueEmpty(SqQueue *Q) { if(Q->front==Q->rear) { return OK; } else { return ERROR; } } /*获取队列长度*/ int GetQueueLength(SqQueue *Q) { return (Q->rear-Q->front); } /*获取队列头的元素*/ int GetQueueHead(SqQueue *Q,QElemType *e) { if(!QueueEmpty(Q)) { *e=*(Q->base+Q->front); return OK; } return ERROR; } /*使元素进队*/ int EnterQueue(SqQueue *Q,QElemType e) { if(Q->rear == MAX_QUEUE_SIZE) //队列满了,重新分配内存区域,用realloc { printf("队列已满,尝试增加存储单元... "); Q->base=(QElemType *)realloc(Q->base,MAX_QUEUE_SIZE+1); if(!Q->base) { printf("增加队列存储单元失败. "); return ERROR; } } *(Q->base+Q->rear)=e; Q->rear++; return OK; } /*删除队头元素*/ int DeleteQueue(SqQueue *Q,QElemType *e) { if(QueueEmpty(Q)) { //printf("队列为空. "); return ERROR; } *e=*(Q->base+Q->front); Q->front++; return OK; } /*遍历队列*/ int QueueTraverse(SqQueue *Q,int (*QV)(QElemType) ) { int i=0; while(i<Q->rear) { (*QV)(*(Q->base+i)); i++; } } /*访问队列中元素的函数,将元素值打印出来*/ int QueueVisitFunc(QElemType e) { printf("%d ",e); } /*-------二叉树的函数定义-------*/ /*初始化树*/ int InitBiTree(SqBiTree T) { int i; for(i=0;i<MAX_TREE_SIZE;i++) { T[i]=Nil; //将初值设为空格 } T[MAX_TREE_SIZE]='