{数据结构}二叉树的先序建立,中序非遍历输出,后序遍历输出,按层次输出,复制二叉树,算二叉树的深度,结点个数,叶子节点个数

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSIZE 100
typedef struct BiTNode{
	char data;
	struct BiTNode *lchild; 
	struct BiTNode *rchild;
}BiTNode,*BiTree;


typedef struct SQueue
{
    BiTree data[MAXSIZE];
    int front,rear;
}SQueue,*Queue;

typedef struct{
	int top;
	int StackSize;
	BiTree *T;
}SqStack;

void InitStack(SqStack *S){
	S->top=-1;
	S->StackSize=MAXSIZE;
	S->T=(BiTree*)malloc(sizeof(BiTree)*S->StackSize);
} 

int StackEmpty(SqStack S){
	return S.top<0?1:0;
}


void Push(SqStack *S,BiTree T){
	if(S->top==S->StackSize-1)
		exit(1);
	else
		S->T[++(S->top)]=T;
		
}

void Pop(SqStack *S,BiTree *T){
	*T=S->T[S->top];
	S->top--;
}
void InOrder(BiTree T){
	SqStack S;
	BiTree p=T;
	
	InitStack(&S); 
	while(p||!(StackEmpty(S))){
		if(p){
			Push(&S,p);
			p=p->lchild;
		}
		else{
			Pop(&S,&p);
			printf("%c",p->data);
			p=p->rchild;
		}
	}
}

//
定义栈
//typedef struct {
//	BiTree *stack;
//	int top;
//	int stacksize;
//}SqStack;
//
初始化栈
//void initstack(SqStack *s)
//{
//	s->stacksize=MAXSIZE;
//	s->top = -1;
//	s->stack = (BiTree*)malloc(sizeof(BiTree)*(s->stacksize));
//}
//
判断栈是否为空
//int StackEmpty(SqStack *s)
//{
//	if (s->top < 0) return 1;
//	return 0;
//}
栈的push操作
//void StackPush(SqStack *s,BiTree T)
//{
//	if (s->top == s->stacksize - 1)
//		exit(1);
//	else{
//		s->stack[++(s->top)] = T;
//	}
//}
栈的top操作:返回栈顶指针
//BiTree StackTop(SqStack *s)
//{
//	if (StackEmpty(s) == 1)
//		exit(1);
//	else{
//		return s->stack[s->top];
//	}
//}
栈的pop操作:删除栈顶数据
//void StackPop(SqStack *s)
//{
//	if (StackEmpty(s) == 1)
//		exit(1);
//	else{
//		s->top--;
//	}
//}
中序遍历
//int inorder(BiTree T){
//	SqStack s;
//	initstack(&s);
//	BiTree p = T;
//
//	while (p || !StackEmpty(&s)){
//		if (p){
//			StackPush(&s, p);
//			p = p->lchild;
//		}
//		else{
//			p = StackTop(&s);
//			StackPop(&s);
//
//			printf("%c",p->data);//打印数据
//			p = p->rchild;
//		}
//	}
//	return 1;
//}

//先序创建
void CreateBiTree(BiTree &T){
	char ch=getchar();
	if(ch=='#'){
		T=NULL;
	}
	else{
		T=(BiTree)malloc(sizeof(BiTNode));
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild);
	}
} 

//后序打印
void PastOrderBiTree(BiTree T){
	if(T){
		PastOrderBiTree(T->lchild);
		PastOrderBiTree(T->rchild);
		printf("%c",T->data);
	}
} 

void InitQueue(Queue Q)
{
    Q -> front = Q -> rear = 0;
}

int IsEmptyQueue(Queue Q)
{
    return Q -> rear == Q -> front?1:0;
}

void EnQueue(BiTree T,Queue Q)
{
    if((Q -> rear+1) % MAXSIZE == Q -> front)
    {
        printf("Queue is fulled!\n");
        return  ;
    }
    Q->rear=(Q->rear+1)%MAXSIZE;
    Q->data[Q->rear]=T;
}

BiTree Gethead(Queue Q)
{
    if(IsEmptyQueue(Q)) return 0;
    BiTree T;
    T=Q ->data[(Q ->front+1) % MAXSIZE];
    Q ->front=(Q ->front+1) % MAXSIZE;
    return T;
}

void LevelOrder(BiTree T)//层次遍历 
{
    SQueue Q ;
    BiTree S ;
    if(!T) return ;
    InitQueue(&Q);
    EnQueue(T,&Q);
    while(!IsEmptyQueue(&Q))
    {
        S=Gethead(&Q);
        printf(" %c",S->data);
        if(S->lchild!=NULL) EnQueue(S->lchild,&Q);
        if(S->rchild!=NULL) EnQueue(S->rchild,&Q);
    }
}

//复制树
void copy(BiTree T,BiTree *NewT){
	if(T==NULL)
		*NewT=NULL;
	else{
		*NewT=(BiTree)malloc(sizeof(BiTNode));
		(*NewT)->data=T->data;
		copy(T->lchild,&((*NewT)->lchild));
		copy(T->rchild,&((*NewT)->rchild));		
	}
} 

//计算叶子节点深度
int Depth(BiTree T){
	int m,n;
	if(T==NULL)
		return 0;
	else{
		m=Depth(T->lchild);
		n=Depth(T->rchild);
		return m>n?m+1:n+1;
	}
} 

//节点的个数
int NodeCount(BiTree T){
	if(T==NULL)
		return 0;
	else
		return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
} 

//叶子节点个数
int LeafCount(BiTree T){
	if(T==NULL)
		return 0;
	if(T->lchild==NULL&&T->rchild==NULL)
		return 1;
	else
		return LeafCount(T->lchild)+LeafCount(T->rchild);
} 
int main(){
	BiTree T,NewT;
	CreateBiTree(T);
	PastOrderBiTree(T);
	printf("\n");
	InOrder(T);
	printf("\n");
	LevelOrder(T);
	copy(T,&NewT);
	printf("\n");
	LevelOrder(NewT);
	printf("\n%d %d %d",Depth(T),NodeCount(T),LeafCount(T));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值