二叉树的建立与遍历
2014-12-04 23:00
246 查看
1.从键盘输入前序序列建立二叉树
#include<stdio.h>
#include<malloc.h>
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiNode;
BiNode *Create(BiNode *root)
{
char ch;
root=(BiNode *)malloc(sizeof(BiNode));
printf("Input the data:");
scanf("%c",&ch);
getchar();
root->data=ch;
if(root->data=='#') return root;
else
{
root->lchild=NULL;
root->rchild=NULL;
root->lchild=Create(root->lchild);
root->rchild=Create(root->rchild);
}
return root;
}
void Pre(BiNode *root)
{
if(root->data=='#') return;
else
{
printf("%c",root->data);
Pre(root->lchild);
Pre(root->rchild);
}
}
void Pin(BiNode *root)
{
if(root->data=='#') return;
else
{
Pin(root->lchild);
printf("%c",root->data);
Pin(root->rchild);
}
}
void Post(BiNode *root)
{
if(root->data=='#') return;
else
{
Post(root->lchild);
Post(root->rchild);
printf("%c",root->data);
}
}
int main()
{
BiNode *root;
root=NULL;
root=Create(root);
printf("前序序列为:");
Pre(root);
printf("\n中序序列为:");
Pin(root);
printf("\n后序序列为:");
Post(root);
return 0;
}
2.已知前序和中序序列建立二叉树
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char pre[]="ABCDEFG"; //前序序列
char pin[]="CBEDAFG"; //中序序列
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiNode;
int pos(char pre[],char pin[],int i1,int i2,int k);
BiNode *Creat(BiNode *root,int i1,int i2,int k);
void PreOrder(BiNode *root);
void PinOrder(BiNode *root);
void PostOrder(BiNode *root);
int main()
{
int i1=0,i2=0; //i1表示前序序列下标,i2表示中序序列下标
int k=strlen(pre); //k表示序列长度
BiNode *root=NULL;
root=Creat(root,i1,i2,k);
printf("前序序列为:");
PreOrder(root);
printf("\n中序序列为:");
PinOrder(root);
printf("\n后序序列为:");
PostOrder(root);
return 0;
}
int pos(char pre[],char pin[],int i1,int i2,int k)
{
int i;
for(i=i2;i<k;i++)
{
if(pin[i]==pre[i1]) break;
}
return i;
}
BiNode *Creat(BiNode *root,int i1,int i2,int k)
{
int m,leftlen,rightlen; //leftlen表示左子树长度,rightlen表示右子树长度
if(k<=0) root=NULL; //递归结束条件
else
{
root=(BiNode *)malloc(sizeof(BiNode));
root->data=pre[i1];
m=pos(pre,pin,i1,i2,k); //查找根结点在中序序列的位置
leftlen=m-i2;
rightlen=k-(leftlen+1);
root->lchild=Creat(root->lchild,i1+1,i1,leftlen);
root->rchild=Creat(root->rchild,i1+leftlen+1,m+1,rightlen);
}
return root;
}
void PreOrder(BiNode *root) //前序遍历
{
if(root==NULL) return;
else
{
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void PinOrder(BiNode *root) //中序遍历
{
if(root==NULL) return;
else
{
PinOrder(root->lchild);
printf("%c",root->data);
PinOrder(root->rchild);
}
}
void PostOrder(BiNode *root) //后序遍历
{
if(root==NULL) return;
else
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c",root->data);
}
}
#include<stdio.h>
#include<malloc.h>
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiNode;
BiNode *Create(BiNode *root)
{
char ch;
root=(BiNode *)malloc(sizeof(BiNode));
printf("Input the data:");
scanf("%c",&ch);
getchar();
root->data=ch;
if(root->data=='#') return root;
else
{
root->lchild=NULL;
root->rchild=NULL;
root->lchild=Create(root->lchild);
root->rchild=Create(root->rchild);
}
return root;
}
void Pre(BiNode *root)
{
if(root->data=='#') return;
else
{
printf("%c",root->data);
Pre(root->lchild);
Pre(root->rchild);
}
}
void Pin(BiNode *root)
{
if(root->data=='#') return;
else
{
Pin(root->lchild);
printf("%c",root->data);
Pin(root->rchild);
}
}
void Post(BiNode *root)
{
if(root->data=='#') return;
else
{
Post(root->lchild);
Post(root->rchild);
printf("%c",root->data);
}
}
int main()
{
BiNode *root;
root=NULL;
root=Create(root);
printf("前序序列为:");
Pre(root);
printf("\n中序序列为:");
Pin(root);
printf("\n后序序列为:");
Post(root);
return 0;
}
2.已知前序和中序序列建立二叉树
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char pre[]="ABCDEFG"; //前序序列
char pin[]="CBEDAFG"; //中序序列
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiNode;
int pos(char pre[],char pin[],int i1,int i2,int k);
BiNode *Creat(BiNode *root,int i1,int i2,int k);
void PreOrder(BiNode *root);
void PinOrder(BiNode *root);
void PostOrder(BiNode *root);
int main()
{
int i1=0,i2=0; //i1表示前序序列下标,i2表示中序序列下标
int k=strlen(pre); //k表示序列长度
BiNode *root=NULL;
root=Creat(root,i1,i2,k);
printf("前序序列为:");
PreOrder(root);
printf("\n中序序列为:");
PinOrder(root);
printf("\n后序序列为:");
PostOrder(root);
return 0;
}
int pos(char pre[],char pin[],int i1,int i2,int k)
{
int i;
for(i=i2;i<k;i++)
{
if(pin[i]==pre[i1]) break;
}
return i;
}
BiNode *Creat(BiNode *root,int i1,int i2,int k)
{
int m,leftlen,rightlen; //leftlen表示左子树长度,rightlen表示右子树长度
if(k<=0) root=NULL; //递归结束条件
else
{
root=(BiNode *)malloc(sizeof(BiNode));
root->data=pre[i1];
m=pos(pre,pin,i1,i2,k); //查找根结点在中序序列的位置
leftlen=m-i2;
rightlen=k-(leftlen+1);
root->lchild=Creat(root->lchild,i1+1,i1,leftlen);
root->rchild=Creat(root->rchild,i1+leftlen+1,m+1,rightlen);
}
return root;
}
void PreOrder(BiNode *root) //前序遍历
{
if(root==NULL) return;
else
{
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void PinOrder(BiNode *root) //中序遍历
{
if(root==NULL) return;
else
{
PinOrder(root->lchild);
printf("%c",root->data);
PinOrder(root->rchild);
}
}
void PostOrder(BiNode *root) //后序遍历
{
if(root==NULL) return;
else
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c",root->data);
}
}
相关文章推荐
- 数据结构(C语言实现) - 二叉树的基本操作(建立,遍历,结点数,叶子结点数,高度,按树状打印,输出叶子结点等)
- C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)
- 二叉树的建立与遍历(C语言简单实现)
- 二叉树的建立、遍历,以及给定二叉树前序遍历和中序遍历重建二叉树问题。
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- 二叉树的建立和遍历
- 12.遍历二叉树与二叉树的建立
- 二叉树的建立及遍历
- 数据结构之二叉树(遍历、建立、深度)
- 二叉树的建立(非递归建立与定义建立)与基本操作(广度和深度遍历,求叶子树高)实现
- 二叉树的建立与遍历(递推)
- 树的遍历 有后序遍历和中序遍历建立二叉树,并输出层次遍历
- Binary Tree_build & travserse(二叉树建立,遍历)
- 二叉树的建立与遍历
- 二叉树的建立,以及非递归遍历
- 树之二叉树的建立与四种遍历(前序,中序, 后序, 层次)及树的深度
- 二叉树的建立与遍历详解 菜鸟都能看懂的教程
- C++二叉树的建立与遍历
- 数据结构实验(严蔚敏版)二叉树的建立与遍历
- 数据结构试验-试验7二叉树的建立与遍历