C语言使用递归以及非递归对二叉树的先、中和后序遍历以及层次遍历

数据类型定义

typedef  char TElemType;
typedef  int Status;
typedef struct BiTNode {
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

二叉树创建

Status CreateBiTree(BiTree *T) {
	char ch;
	ch=getchar();
	if (ch=='#') //#代表空指针
	(*T)=Null; else {
		(*T)=(BiTree) malloc(sizeof(BiTNode));
		//申请结点
		(*T)->data=ch;
		//生成根结点 
		CreateBiTree(&(*T)->lchild);
		//构造左子树
		CreateBiTree(&(*T)->rchild) ;
		//构造右子树
	}
	return 1;
}

输入方式:ABC##DE#G##F###

按先序次序输入二叉树中结点的值,用‘#’表示空树,对每一个结点应当确定其左右子树的值(为空时必须用特定的空字符占位)

建立如图的二叉树

先序遍历

递归

void PreOrder(BiTree T) {
	if (T) {
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
		PreOrder(T->lchild);
		//先序遍历左子树
		PreOrder(T->rchild);
		//先序遍历右子树
	}
}

非递归

void Norec_PreOrder(BiTree T) {
	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		while(stackTop != -1 || t) {
			while(t) {
				printf(" %c", t->data);
				stack[++stackTop] = t;
				t = t->lchild;
			}
			if(stackTop != -1) {
				t = stack[stackTop--];
				t = t->rchild;
			}
		} 
	}
}

中序遍历

递归

void InOrder(BiTree T) {
	if (T) {

		InOrder(T->lchild);
		//先序遍历左子树
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
		InOrder(T->rchild);
		//先序遍历右子树
	}
}

非递归

void Norec_InOrder(BiTree T) {
	
	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		while(stackTop != -1 || t) {
			while(t) {
				
				stack[++stackTop] = t;
				t = t->lchild;
			}
			if(stackTop != -1) {
				t = stack[stackTop--];
				printf(" %c", t->data);
				t = t->rchild;
			}
		} 
	}
}

后序遍历

递归

void PostOrder(BiTree T) {
	if (T) {
		
		PostOrder(T->lchild);
		//先序遍历左子树
		PostOrder(T->rchild);
		//先序遍历右子树
		
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
	}

} 

非递归

void Norec_PostOrder(BiTree T) {

	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		BiTree tem = Null;
		
		while(stackTop != -1 || t) {
			if(t) {
				
				stack[++stackTop] = t;
				t = t->lchild;
//				printf("stackTop : %d , value:%c",stackTop,t->data);
				
			} else {
				t = stack[stackTop];
				if(t->rchild&&t->rchild!=tem) {
					t = t->rchild;
				} else {
					stackTop--;
					printf(" %c", t->data);
					tem = t;
					t = Null;
				}
				
			}
			
		} 
	}
} 

层次遍历

void LevleOrder(BiTree T) {
	//层次遍历二叉树T,从第一层开始,每层从左到右
	BiTree Queue[MAX],b;
	//用一维数组表示队列
	int front,rear;
	// front和rear分别表示队首和队尾指针
	front=rear=0;
	if(T) {
		//若树非空
		Queue[rear++]=T;
		//根结点入队列
		while (front!=rear) {
			//当队列非空
			b=Queue[front++];
			//队首元素出队列,并访问这个结点
			printf("%2c",b->data);
			if (b->lchild!=Null) 		//左子树非空,则入队列
			Queue[rear++]=b->lchild;
			if (b->rchild!=Null)		 //右子树非空,则入队列
			Queue[rear++]=b->rchild;
		}
	}
}
//LevelOrder

求二叉树的深度

int depth(BiTree T) {
	//求二叉树的深度
	int dep1,dep2;
	if (T==Null) 
		return 0; else {
		dep1=depth(T->lchild);
		dep2=depth(T->rchild);
		return dep1>dep2?dep1+1:dep2+1;
	}
}
//depth

完整代码

#include "stdio.h"
#include "stdlib.h"
#include<stdlib.h>
#define MAX 20
#define Null 0

typedef  char TElemType;
typedef  int Status;
typedef struct BiTNode {
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;



Status CreateBiTree(BiTree *T) {
	char ch;
	ch=getchar();
	if (ch=='#') //#代表空指针
	(*T)=Null; else {
		(*T)=(BiTree) malloc(sizeof(BiTNode));
		//申请结点
		(*T)->data=ch;
		//生成根结点 
		CreateBiTree(&(*T)->lchild);
		//构造左子树
		CreateBiTree(&(*T)->rchild) ;
		//构造右子树
	}
	return 1;
}

// 递归 先序遍历 
void PreOrder(BiTree T) {
	if (T) {
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
		PreOrder(T->lchild);
		//先序遍历左子树
		PreOrder(T->rchild);
		//先序遍历右子树
	}
}

void Norec_PreOrder(BiTree T) {
	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		while(stackTop != -1 || t) {
			while(t) {
				printf(" %c", t->data);
				stack[++stackTop] = t;
				t = t->lchild;
			}
			if(stackTop != -1) {
				t = stack[stackTop--];
				t = t->rchild;
			}
		} 
	}
}



// 递归 中序遍历 
void InOrder(BiTree T) {
	if (T) {

		InOrder(T->lchild);
		//先序遍历左子树
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
		InOrder(T->rchild);
		//先序遍历右子树
	}
}

void Norec_InOrder(BiTree T) {
	
	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		while(stackTop != -1 || t) {
			while(t) {
				
				stack[++stackTop] = t;
				t = t->lchild;
			}
			if(stackTop != -1) {
				t = stack[stackTop--];
				printf(" %c", t->data);
				t = t->rchild;
			}
		} 
	}
}

void PostOrder(BiTree T) {
	if (T) {
		
		PostOrder(T->lchild);
		//先序遍历左子树
		PostOrder(T->rchild);
		//先序遍历右子树
		
		printf("%2c",T->data);
		//访问根结点,此处简化为输出根结点的数据值
	}

} 

void Norec_PostOrder(BiTree T) {

	if (T) {
		BiTree stack[10];
		int stackTop = -1;
		BiTree t = T;
		BiTree tem = Null;
		
		while(stackTop != -1 || t) {
			if(t) {
				
				stack[++stackTop] = t;
				t = t->lchild;
//				printf("stackTop : %d , value:%c",stackTop,t->data);
				
			} else {
				t = stack[stackTop];
				if(t->rchild&&t->rchild!=tem) {
					t = t->rchild;
				} else {
					stackTop--;
					printf(" %c", t->data);
					tem = t;
					t = Null;
				}
				
			}
			
		} 
	}
} 



void LevleOrder(BiTree T) {
	//层次遍历二叉树T,从第一层开始,每层从左到右
	BiTree Queue[MAX],b;
	//用一维数组表示队列
	int front,rear;
	// front和rear分别表示队首和队尾指针
	front=rear=0;
	if(T) {
		//若树非空
		Queue[rear++]=T;
		//根结点入队列
		while (front!=rear) {
			//当队列非空
			b=Queue[front++];
			//队首元素出队列,并访问这个结点
			printf("%2c",b->data);
			if (b->lchild!=Null) 		//左子树非空,则入队列
			Queue[rear++]=b->lchild;
			if (b->rchild!=Null)		 //右子树非空,则入队列
			Queue[rear++]=b->rchild;
		}
	}
}
//LevelOrder


int depth(BiTree T) {
	//求二叉树的深度
	int dep1,dep2;
	if (T==Null) 
		return 0; else {
		dep1=depth(T->lchild);
		dep2=depth(T->rchild);
		return dep1>dep2?dep1+1:dep2+1;
	}
}
//depth



void main() {
	printf("-----------------------------\n");
	BiTree T=Null;
	printf("\n请输入一颗二叉树~:\n");
	CreateBiTree(&T);
	printf("\n一颗二叉树创建完成~:\n");
	printf("\n");

	
	
	while(1) {
		printf("\n-----------------------------\n");
		int num;
		printf("请输入序号1-8:\n");
		printf("1. 先序非递归遍历\n");
		printf("2. 先序递归遍历\n");
		printf("3. 中序非递归遍历\n");
		printf("4. 中序递归遍历\n");
		printf("5. 后序非递归遍历\n");
		printf("6. 后序递归遍历\n");
		printf("7. 层次遍历\n");
		printf("8. 树的深度\n");
		printf("9. 退出\n");
		printf("\n-----------------------------\n");
		scanf("%d",&num);
		
		switch(num) {
			case 1:printf("\n先序非递归遍历结果为:\n");Norec_PreOrder(T);printf("\n");break; 
			case 2:printf("\n先序递归遍历结果为:\n");PreOrder(T);printf("\n");break;
			
			case 3:printf("\n中序非递归遍历结果为:\n");Norec_InOrder(T);printf("\n");break;
			case 4:printf("\n中序递归遍历结果为:\n");InOrder(T);printf("\n");break; 
					
			case 5:printf("\n后序非递归遍历结果为:\n");Norec_PostOrder(T);printf("\n");break; 
			case 6:printf("\n后序递归遍历结果为:\n");PostOrder(T);printf("\n");break; 
			
			case 7:printf("\n层次遍历的结果为:\n");LevleOrder(T);printf("\n");break; 
			case 8:printf("\n数的深度为:%d\n",depth(T));printf("\n");break; 
			case 9:printf("\n886\n");exit(0); 

	
	}
}
} 

C++版本的代码

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值