#include<stdlib.h>
#include<stdio.h>
#define MAXQSIZE 50
#define STACK_INIT_SIZE 50
#define TRUE 1 /*状态码预定义*/
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild; //左右孩子指针
}BiTNode, *BiTree;
typedef struct BiTNode* QElemtype;
typedef struct BiTNode* SElemtype;
typedef struct
{
QElemtype *base;
int front,rear;
}SqQueue;
typedef struct
{
SElemtype base[STACK_INIT_SIZE];
int top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
S->top=0;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
SElemtype GetTop(SqStack S)
{
if(S.top==0)
return ERROR;
else
return S.base[S.top-1];
}
Status Push(SqStack *S,SElemtype e)
{
S->base[S->top++]=e;
return OK;
}
SElemtype Pop(SqStack *S)
{
if(S->top == 0) return ERROR;
S->top--;
return S->base[S->top];
}
Status StackEmpty(SqStack S)
{
return S.top==0;
}
int GetStackSize(SqStack S)
{
return S.top-1;
}
Status InitQueue(SqQueue *Q)
{
Q->base=(QElemtype*)malloc(sizeof(QElemtype));
if(!Q->base) exit(OVERFLOW);
Q->front=Q->rear=0;
return OK;
}
Status EnQueue(SqQueue *Q,QElemtype e)
{
if((Q->rear+1)%MAXQSIZE==Q->front) /*If the queue is full*/
{
printf("The queue is full!\n");
return ERROR;
}
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXQSIZE;
return OK;
}
Status Dequeue(SqQueue *Q,QElemtype *e)
{
if(Q->front==Q->rear)
{
printf("The queue is empty!\n");
return ERROR;
}
*e=Q->base[Q->front];
Q->front=(Q->front+1)%MAXQSIZE;
return OK;
}
Status TraverseQueue(SqQueue Q)
{
int i;
i=Q.front;
printf("The elements in the queue are:");
while((i%MAXQSIZE)!=Q.rear)
{
printf("%d ",Q.base[i]);
i++;
}
printf("\n");
return OK;
}
Status QueueEmpty(SqQueue q)
{
return q.rear==q.front;
}
Status CreateBiTree_1(BiTree *T,char ch[]) //先序遍历创建节点,递归(自动输入)
{
static int i=-1; //静态变量
i++;
if(ch[i]=='#')
*T=NULL;
else if(ch[i]!='\0')
{
*T=(BiTNode *)malloc(sizeof(BiTNode)); //为T分配内存空间
if(T==NULL) exit(OVERFLOW);
(*T)->data=ch[i];
CreateBiTree_1(&(*T)->lchild,ch);
CreateBiTree_1(&(*T)->rchild,ch);
}
return OK;
}
Status PrintElement(TElemType e) //简单的打印元素函数
{
printf("%c",e);
return OK;
}
/*先序遍历,使用递归*/
void PreOrderTraverse(BiTree T,Status (*visit)(TElemType e))
{
if(T==NULL) return;
visit(T->data);
PreOrderTraverse(T->lchild,visit);
PreOrderTraverse(T->rchild,visit);
}
/*非递归中序遍历方法1 算法6.1 P130 */
Status InOrderTraverse_1(BiTree T,Status (*visit)(TElemType e))
{
SqStack S;
BiTree p;
InitStack(&S); //初始化栈
Push(&S,T);
while(!StackEmpty(S)) //栈非空
{
while(p=GetTop(S)) Push(&S,p->lchild);
p=Pop(&S);
if(!StackEmpty(S))
{
p=Pop(&S); if(!visit(p->data)) return ERROR;
Push(&S,p->rchild);
}
}
}
/*非递归中序遍历方法2 算法6.2 P131*/
Status InOrderTraverse_2(BiTree T,Status(*visit)(TElemType e))
{
SqStack S;
BiTree p;
InitStack(&S);
p=T;
while(p||!StackEmpty(S))
{
if(p)
{
Push(&S,p);
p=p->lchild;
} //左子树全部进栈
else
{
p=Pop(&S);
if(!visit(p->data)) return ERROR;
p=p->rchild;
}
}
return OK;
}
/*递归后序遍历*/
Status PostOrderTraverse(BiTree T,Status(*visit)(TElemType e))
{
if(T==NULL)
return ERROR;
PostOrderTraverse(T->lchild,visit); /*先左后右*/
PostOrderTraverse(T->rchild,visit);
visit(T->data);
}
/* */
int NodeCount(BiTree T){
if(T==NULL) return FALSE;
else return NodeCount(T->lchild)+ NodeCount(T->rchild)+1;
}
/*递归后序遍历
(3)在上述二叉树的基础上,后序遍历,求解二叉树深度的算法*/
int Depth(BiTree T){
int m,n;
if(T==NULL) return FALSE;
else {
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n) return (m+1);
else return(n+1);
}
}
/*
(2)在上述二叉树的基础上,实现先序遍历,统计叶子节点个数的算法
*/
//后序
int CountLeaf(BiTree &T)
{ // int cright,cleft;
if (T==NULL)
return 0;
if ((T->lchild==NULL)&&(T->rchild==NULL))
return 1; // 对叶子结点计数
/* else
{
cleft=CountLeaf(T->lchild);
cright=CountLeaf(T->rchild);
return (cleft+cright);
}
*/
return CountLeaf(T->lchild)+CountLeaf(T->rchild);
} // CountLeaf
/*
void leaf(BiTree T){//递归统计叶子节点的数目
int count;
if(T){
if(T->lchild==NULL&&T->rchild==NULL) count++;
leaf(T->lchild);
leaf(T->rchild);
}
}
*/
//先序
/*
int CountLeaf(BiTree T,int count)
{
if(T){
if ((T->lchild==NULL)&& (T->rchild==NULL))
count++; // 对叶子结点计数
count=CountLeaf( T->lchild,count);
count =CountLeaf( T->rchild,count);
}
} // CountLeaf
//后序
int Countleaf(BiTree T)
{
if(T==NULL)
return 0;
if ((T->lchild==NULL)&& (T->rchild==NULL))
return 1; // 对叶子结点计数
else
return Countleaf(T->lchild)+ Countleaf(T->rchild);
}// CountLeaf
*/
void main()
{
BiTree T;
char ch[]="ABDF###E##C##"; //测试数据 先序序列
CreateBiTree_1(&T,ch); //二选一
//CreateBiTree_2(&T); //此为符合实验要求函数
//NodeCount(T);
//Depth(T);
printf("二叉树结点的个数是%d个:\n",NodeCount(T));
printf("二叉树叶子结点的个数是%d个:\n",CountLeaf(T));
// printf("二叉树叶子结点的个数是%d个:\n",CountLeaf(T,0));
printf("二叉树的深度是%d:\n",Depth(T));
printf("后序遍历:\n");
PostOrderTraverse(T,PrintElement);
printf("\n先序遍历:\n");
PreOrderTraverse(T,PrintElement);
printf("\n非递归中序遍历方法1:\n");
InOrderTraverse_1(T,PrintElement);
printf("\n非递归中序遍历方法2:\n");
InOrderTraverse_2(T,PrintElement);
printf("\n");
}
二叉树的创建、先序遍历、中序遍历、后序遍历以及二叉树叶子结点的统计
最新推荐文章于 2022-11-20 11:22:11 发布