请教一道C语言的数据结构题目
1.实现二叉树的创建三种递归遍历算法/*实现二叉树的三种递归遍历算法*/#include"head.h"#include"stdio.h"#include"stdlib....
1. 实现二叉树的创建三种递归遍历算法
/*实现二叉树的三种递归遍历算法*/
#include"head.h"
#include "stdio.h"
#include "stdlib.h"
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status PrintElement(TElemType e)
{
printf("%c",e);
return OK;
}
Status CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c",&ch);
if(ch==' ') *T=NULL;
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
if(!*T) exit(OVERFLOW);
_____________________________________
_____________________________________
_____________________________________
}
return OK;
}
Status PreOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(Visit(T->data))
if(PreOrderTraverse(T->lchild,Visit))
if(PreOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}else return OK;
}
Status InOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
}
Status BackOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
}
void main()
{
BiTree T=NULL;
printf("建树\n");
CreateBiTree(&T);
printf("先序遍历\n");
PreOrderTraverse(T,PrintElement);
printf("\n");
printf("中序遍历\n");
InOrderTraverse(T,PrintElement);
printf("\n");
printf("后序遍历\n");
BackOrderTraverse(T,PrintElement);
} 展开
/*实现二叉树的三种递归遍历算法*/
#include"head.h"
#include "stdio.h"
#include "stdlib.h"
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status PrintElement(TElemType e)
{
printf("%c",e);
return OK;
}
Status CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c",&ch);
if(ch==' ') *T=NULL;
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
if(!*T) exit(OVERFLOW);
_____________________________________
_____________________________________
_____________________________________
}
return OK;
}
Status PreOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(Visit(T->data))
if(PreOrderTraverse(T->lchild,Visit))
if(PreOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}else return OK;
}
Status InOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
}
Status BackOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
_____________________________________
}
void main()
{
BiTree T=NULL;
printf("建树\n");
CreateBiTree(&T);
printf("先序遍历\n");
PreOrderTraverse(T,PrintElement);
printf("\n");
printf("中序遍历\n");
InOrderTraverse(T,PrintElement);
printf("\n");
printf("后序遍历\n");
BackOrderTraverse(T,PrintElement);
} 展开
展开全部
Status CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c",&ch);
if(ch==' ') *T=NULL;
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
if(!*T) exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
Status InOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(PreOrderTraverse(T->lchild,Visit));
if(Visit(T->data))
if(PreOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}else return OK;
}
Status BackOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(PreOrderTraverse(T->lchild,Visit));
if(PreOrderTraverse(T->rchild,Visit));
if(Visit(T->data))return OK;
return ERROR;
}else return OK;
}
{
TElemType ch;
scanf("%c",&ch);
if(ch==' ') *T=NULL;
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
if(!*T) exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
Status InOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(PreOrderTraverse(T->lchild,Visit));
if(Visit(T->data))
if(PreOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}else return OK;
}
Status BackOrderTraverse(BiTree T,Status(* Visit)(TElemType))
{
if(T){
if(PreOrderTraverse(T->lchild,Visit));
if(PreOrderTraverse(T->rchild,Visit));
if(Visit(T->data))return OK;
return ERROR;
}else return OK;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询