您的位置:首页 > 其它

二叉树的递归遍历与非递归遍历

2015-07-11 16:13 344 查看
二叉树的存储结构采用二叉链表,

递归先序遍历:

(1)访问根节点;

(2)先序遍历左子树,在遍历过程中总是先访问根节点;

(3)先序遍历右子树,在遍历过程中总是先访问根节点。

递归中序遍历:

(1)中序遍历左子树,遍历结束再访问根节点;

(2)访问根节点;

(3)中序遍历右子树,仍先遍历左子树,完成后访问根,最后遍历右子树;

递归先序遍历:左子树遍历完成,再遍历右子树,完成后再访问根;

(1)后序遍历左子树;

(2)后序遍历右子树;

(3)访问根节点。

#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
//#include<string.h>
#define NULL 0
#define Stack_Size 1000
#define NUM 20
typedef int DataType;
typedef struct BiTNode{
DataType data;
struct BiTNode *LChild;
struct BiTNode *RChild;
}BiTNode, *BiTree;
typedef int StackElementType;
typedef struct{
StackElementType elem[Stack_Size];
int top;
}SeqStack;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/**
*初始化二叉树
*置左子树为空
*置右子树为空
*/

/******************************创建一颗二叉树********************************/
int SetTree(BiTree *H){//递归创建一颗二叉树,输入值为-1/回车,结束创建
DataType value;
scanf("%d",&value);
if (value == -1){
(*H) = NULL;
}
else{
(*H) = (BiTree)malloc(sizeof(BiTNode));
(*H)->data = value;//将value的值赋给根节点数据域
SetTree(&((*H)->LChild));//先序遍历左子树
SetTree(&((*H)->RChild));//先序遍历右子树
}
return 0;
}
/****************************************************************************/

/*******************************递归先序遍历*********************************/
void PreOrder(BiTree H){
if (H!=NULL){
printf("%d ",H->data);//访问根节点
PreOrder(H->LChild);//先序遍历左子树
PreOrder(H->RChild);//先序遍历右子树
}
}
/****************************************************************************/

/*******************************递归中序遍历*********************************/
void InOrder(BiTree H){
if (H!=NULL){
InOrder(H->LChild);//中序遍历左子树
printf("%d ",H->data);//访问根节点
InOrder(H->RChild);//中序遍历右子树
}
}
/****************************************************************************/

/*********************************递归后序遍历*******************************/
void PostOrder(BiTree H){
if (H!=NULL){
PostOrder(H->LChild);//后序遍历左子树
PostOrder(H->RChild);//后序遍历右子树
printf("%d ",H->data);//访问根节点
}
}
/****************************************************************************/

/*******************************非递归先序遍历*******************************/
void PreOrder_Stack(BiTree H){
BiTNode *p,*s[Stack_Size];
int top;
p=H;
top=0;
do{
while(p!=NULL){
printf("%d ",p->data);//打印左子树
if(p->RChild!=NULL){//左子树不为空将右子树指针存入栈中
s[top++]=p->RChild;//右子树地址入栈
}
p=p->LChild;//继续搜索左子树
}
if(top>=0){
p=s[--top];//右子树指针出栈
}
}while(top>=0);
}
/****************************************************************************/

/********************************非递归中序遍历******************************/
void InOrder_Stack(BiTree H){
BiTNode *p,*s[Stack_Size];
int top;
p=H;
top=0;
do{
while(p!=NULL){
s[top]=p;//所遇结点p进栈
p=p->LChild;//继续搜索p的左子树
top++;
}
if(top>0){
top=top-1;
p=s[top];//右子树指针出栈
printf("%d ",p->data);//访问根节点
p=p->RChild;//继续搜索p的右子树
}
}while(top>0);
}
/****************************************************************************/

/******************************非递归后序遍历********************************/
void PostOrder_Stack(BiTree H){
BiTNode *p,*s[Stack_Size];
int top,s1[Stack_Size],b=0;
p=H;
top=0;
do{
while(p!=NULL){
s[top]=p;//所遇结点p首次进栈
s1[top++]=0;
p=p->LChild;//遍历p的左子树
}
if(top>=0){
b=s1[--top];
p=s[top];
if(b==0){
s[top]=p;//p结点第二次进栈
s1[top++]=1;
p=p->RChild;//遍历p的右子树
}
else{
printf("%d ",p->data);//访问根节点
p=NULL;
}
}
}while(top>0);
}
/****************************************************************************/

void menu(){
printf("\t****************************************************************\t\n");
printf("\t    0-创建二叉树\t\t**\t\t1-递归前序遍历\n");
printf("\t----------------------------------------------------------------\t\n");
printf("\t    2-递归中序遍历\t\t**\t\t3-递归后序遍历\n");
printf("\t----------------------------------------------------------------\t\n");
printf("\t    4-非递归先序遍历  \t\t**\t\t5-非递归中序遍历\n");
printf("\t----------------------------------------------------------------\t\n");
printf("\t    6-非递归后序遍历\t\t**\t\t7-退出\n");
printf("\t****************************************************************\t\n");
}
void getFunction(int number,BiTree H){
int resetnumber=0,deletenumber=0,insertnumber=0,getnumber=0;
int count=0;
while(number>=0&&number<=8){
switch (number)
{
case 0:{
printf("请创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n--------------------------------------------------------------------------------");
break;
}
case 1:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("递归前序遍历序列:\n");
PreOrder(H);
}
else{
printf("递归前序遍历序列:\n");
PreOrder(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 2:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("递归中序遍历序列:\n");
InOrder(H);
}
else{
printf("递归中序遍历序列:\n");
InOrder(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 3:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("递归后序遍历序列:\n");
PostOrder(H);
}
else{
printf("递归后序遍历序列:\n");
PostOrder(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 4:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("非递归先序遍历序列:\n");
PreOrder_Stack(H);
}
else{
printf("非递归先序遍历序列:\n");
PreOrder_Stack(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 5:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("非递归中序遍历序列:\n");
InOrder_Stack(H);
}
else{
printf("非递归中序遍历序列:\n");
InOrder_Stack(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 6:{
if(H==NULL){
printf("\n请先创建一颗二叉树,输入-1结束:");
SetTree(&H);
printf("\n");
printf("非递归后序遍历序列:\n");
PostOrder_Stack(H);
}
else{
printf("非递归后序遍历序列:\n");
PostOrder_Stack(H);
}
printf("\n\n--------------------------------------------------------------------------------");
break;
}
case 7:printf("谢谢使用!\n\n");
printf("--------------------------------------------------------------------------------\n");
exit(0);
break;
}
count++;
if(count==2){
menu();
count=0;
}
//menu();
printf("请输入序号0-7:");
scanf("%d",&number);
printf("\n");
//printf("\n");
}
}
int main(){
BiTree H;
int number=0;
H= (BiTree)malloc(sizeof(BiTNode));
H=NULL;
menu();
printf("请输入0-7进行操作:");
scanf("%d",&number);
printf("\n");
getFunction(number,H);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: