//Bintree.h
#include<stdio.h>
#include<malloc.h>
typedef struct Binnode{//二叉树结点结构体
char data;
struct Binnode *lchild;
struct Binnode *rchild;
};
typedef Binnode *Bintree ;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
typedef struct stack{ //二叉树结点栈
Bintree data[100];
int flag[100];
int top;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
typedef struct queue{ //二叉树结点队列
Bintree data[30];
int front;
int rear;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照前序遍历建立二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Creat_Bintree(Bintree *root)
{
char ch;
if((ch=getchar())==' ')
{
*root=NULL;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
}
else
{
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照前序递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Preorder1(Bintree t)
{
if(t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照中序递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Inorder1(Bintree t)
{
if(t!=NULL)
{
Inorder1(t->lchild);
printf("%c",t->data);
Inorder1(t->rchild);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照后序递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Posorder1(Bintree t)
{
if(t!=NULL)
{
Posorder1(t->lchild);
Posorder1(t->rchild);
printf("%c",t->data);
}
}
/*******************************************/
/* 按照前序非递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Preorder2(Bintree t)
{
Bintree pre=t;
stack s;
s.top=0;
printf("输出前序遍历序列:");
while(pre||s.top>0)
{
if(pre)
{
printf("%c",pre->data);
s.data[s.top++]=pre;
pre=pre->lchild;
}
else
{
pre=s.data[--s.top]->rchild;
}
}
printf("\n\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照中序非递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Inorder2(Bintree t)
{
Bintree pre=t;
stack s;
s.top=0;
printf("输出中序遍历序列:");
while(pre||s.top>0)
{
if(pre)
{
s.data[s.top++]=pre;
pre=pre->lchild;
}
else
{
pre=s.data[--s.top];
printf("%c",pre->data);
pre=pre->rchild;
}
}
printf("\n\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照后序非递归遍历二叉树 */
/*******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Posorder2(Bintree t)
{
stack s;
s.top=-1;
printf("输出后序遍历序列:");
while(t!=NULL||s.top!=-1)
{
while(t)
{
s.top++;
s.flag[s.top]=0;
s.data[s.top]=t;
t=t->lchild;
}
while(s.top>=0&&s.flag[s.top]==1)
{
t=s.data[s.top];
printf("%c",t->data);
s.top--;
}
if(s.top>=0)
{
t=s.data[s.top];
s.flag[s.top]=1;
t=t->rchild;
}
else
{
t=NULL;
}
}
printf("\n\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 按照层次遍历二叉树 */
/*******************************************/
void Levelorder(Bintree t)
{
queue q;
q.data[0]=t;
q.front=0;q.rear=1;
printf("层次遍历二叉树结果:");
while(q.front<q.rear)
{
if(q.data[q.front])
{
printf("%c",q.data[q.front]->data);
q.data[q.rear++]=q.data[q.front]->lchild;
q.data[q.rear++]=q.data[q.front]->rchild;
q.front++;
}
else
{
q.front++;
}
}
printf("\n\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
/*******************************************/
/* 递归法将二叉树的左右子树互换 */
/*******************************************/
void Exchange1(Bintree t)
{
Bintree temp;
if(t)
{
Exchange1(t->lchild);
Exchange1(t->rchild);
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
}
}
/*******************************************/
/* 非递归法将二叉树的左右子树互换 */
/*******************************************/
void Exchange2(Bintree t)
{
Bintree temp;
stack s;
s.top=0;
while(t||s.top)
{
if(t)
{
s.data[s.top++]=t;
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
t=t->lchild;
}
else
{
t=s.data[--s.top]->rchild;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
}
}
int main()
{
Bintree t;
Creat_Bintree(&t);
Exchange2(t);
Inorder2(t);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
/*******************************************/
/* 递归法求叶子结点个数 */
/*******************************************/
int Leaves_Num1(Bintree t)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL)
{
return 1;
}
return Leaves_Num1(t->lchild)+Leaves_Num1(t->rchild);
}
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/*******************************************/
/* 非递归法求叶子结点个数 */
/*******************************************/
int Leaves_Num2(Bintree t)
{
int count=0;
stack s;
s.top=0;
while(t||s.top>0)
{
if(t)
{
s.data[s.top++]=t;
if(t->lchild==NULL&&t->rchild==NULL)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
count++;
}
t=t->lchild;
}
else
{
t=s.data[--s.top]->rchild;
}
}
return count;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
int count=0;
Bintree t;
Creat_Bintree(&t);
count=Leaves_Num2(t);
printf("该二叉树的叶子结点数为:%d\n",count);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/**********************************************/
/* 求一棵树的高度 */
/**********************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int Depth(Bintree t)
{
int lh , rh ;
if( NULL == t )
{
return 0 ;
}
else
{
lh = Depth( t->lchild ) ;
rh = Depth( t->rchild ) ;
return ( lh > rh ? lh : rh ) + 1 ;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
Bintree t ;
Creat_Bintree( &t ) ;
printf( "树的高度是%d\n" , Depth( t ) ) ;
return 0 ;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
/*******************************************************/
/* 已知一课棵二叉树的中序和后序,建立这棵树 */
/*******************************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void In_Pos_order(Bintree *t,char *s,char *r)
{
char La[30],Lb[30],Ra[30],Rb[30];
int i,len,length=strlen(r);
if(length>0&&r[length-1]!='\0')
{
*t=(Binnode *)malloc(sizeof(Binnode));
(*t)->data=r[length-1];
for(i=0;s[i]!=r[length-1];i++)
{
Ra[i]=s[i];
La[i]=r[i];
}
len=i;
Ra[len]='\0'; //左中
La[len]='\0'; //左后
for(i=len+1;i<strlen(s);i++)
{
Rb[i-len-1]=s[i];
}
Rb[i-len-1]='\0';
for(i=len;i<strlen(r)-1;i++)
{
Lb[i-len]=r[i];
}
Lb[i-len]='\0';
In_Pos_order(&(*t)->lchild,Ra,La);
In_Pos_order(&(*t)->rchild,Rb,Lb);
}
else
{
*t=NULL;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
Bintree t;
char in[30]="ABCEFGHD",pos[30]="ABFHGEDC";//测试数据
printf("输入中序遍历序列:");
scanf("%s",in);
printf("输入后序遍历序列:");
scanf("%s",in);
In_Pos_order(&t,in,pos);
Preorder2(t);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
/*******************************************************/
/* 判断两棵是否等价 */
/*******************************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int Is_equal( Bintree t1 , Bintree t2 )
{
int t=0;
if(NULL == t1 && NULL == t2)
{
t=1;
}
else
{
if(NULL !=t1 &&NULL != t2 )
{
if(t1->data == t2->data)
{
if(Is_equal(t1->lchild,t2->lchild))
{
t=Is_equal(t1->rchild,t2->rchild);
}
}
}
}
return t;
}
int main()
{
Bintree t1,t2;
Creat_Bintree(&t1);
getchar();
Creat_Bintree(&t2);
if(Is_equal(t1,t2))
{
printf( "Yes!\n") ;
}
else
{
printf( "No!\n" ) ;
}
return 0 ;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Bintree.h"
/****************************************************/
/* 查找某个信息是否在这棵树中 */
/****************************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Bintree locale_x(Bintree t,char x)
{
Bintree p;
if(t==NULL) return NULL;
else
{
if( t -> data == x ) return t;
else
{
p = locale_x(t->lchild,x);
if(p)return p;
else
return locale_x(t->rchild,x);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
Bintree root,p;
char ch;
Creat_Bintree(&root);
getchar();
printf("输入要查找的值:");
scanf("%c",&ch);
p=locale_x(root,ch);
if(p)printf( "YES!\n" ) ;
else printf( "NO!\n" ) ;
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-08-07 11:14
RayG
阅读(
351)
评论()
编辑
收藏
举报