Binary Tree ANSI C Implement  (1)//Bintree.h
Binary Tree ANSI C Implement  (1)
#include<stdio.h>
Binary Tree ANSI C Implement  (1)#include<malloc.h>

Binary Tree ANSI C Implement  (1)
typedef struct Binnode{//二叉树结点结构体
Binary Tree ANSI C Implement  (1)    char data;
Binary Tree ANSI C Implement  (1)    struct Binnode 
*lchild;
Binary Tree ANSI C Implement  (1)    struct Binnode 
*rchild;
Binary Tree ANSI C Implement  (1)  };
Binary Tree ANSI C Implement  (1)typedef Binnode 
*Bintree ; 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)typedef struct stack{ 
//二叉树结点栈
Binary Tree ANSI C Implement  (1)     Bintree data[
100];
Binary Tree ANSI C Implement  (1)    
int flag[100];
Binary Tree ANSI C Implement  (1)    
int top;
Binary Tree ANSI C Implement  (1)  }; 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)typedef struct queue{ 
//二叉树结点队列
Binary Tree ANSI C Implement  (1)    Bintree data[
30];
Binary Tree ANSI C Implement  (1)    
int front;
Binary Tree ANSI C Implement  (1)    
int rear;
Binary Tree ANSI C Implement  (1)  }; 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)  
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照前序遍历建立二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Creat_Bintree(Bintree 
*root)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    char ch;
Binary Tree ANSI C Implement  (1)    
if((ch=getchar())==' ')
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
*root=NULL; 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
*root=(Binnode*)malloc(sizeof(Binnode));
Binary Tree ANSI C Implement  (1)        (
*root)->data=ch;
Binary Tree ANSI C Implement  (1)        Creat_Bintree(
&(*root)->lchild);
Binary Tree ANSI C Implement  (1)        Creat_Bintree(
&(*root)->rchild);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照前序递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Preorder1(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
if(t!=NULL)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
printf("%c",t->data);
Binary Tree ANSI C Implement  (1)        Preorder1(t
->lchild);
Binary Tree ANSI C Implement  (1)        Preorder1(t
->rchild);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照中序递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Inorder1(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
if(t!=NULL)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        Inorder1(t
->lchild);
Binary Tree ANSI C Implement  (1)        
printf("%c",t->data);
Binary Tree ANSI C Implement  (1)        Inorder1(t
->rchild);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照后序递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Posorder1(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
if(t!=NULL)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        Posorder1(t
->lchild);
Binary Tree ANSI C Implement  (1)        Posorder1(t
->rchild);
Binary Tree ANSI C Implement  (1)        
printf("%c",t->data);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)}
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照前序非递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Preorder2(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree pre
=t;
Binary Tree ANSI C Implement  (1)    stack s;
Binary Tree ANSI C Implement  (1)    s
.top=0;
Binary Tree ANSI C Implement  (1)    
printf("输出前序遍历序列:");
Binary Tree ANSI C Implement  (1)    
while(pre||s.top>0)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(pre)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            
printf("%c",pre->data);
Binary Tree ANSI C Implement  (1)            s
.data[s.top++]=pre;
Binary Tree ANSI C Implement  (1)            pre
=pre->lchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            pre
=s.data[--s.top]->rchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
printf("\n\n");
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*          按照中序非递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Inorder2(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree pre
=t;
Binary Tree ANSI C Implement  (1)    stack s;
Binary Tree ANSI C Implement  (1)    s
.top=0;
Binary Tree ANSI C Implement  (1)     
printf("输出中序遍历序列:");
Binary Tree ANSI C Implement  (1)    
while(pre||s.top>0)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(pre)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            s
.data[s.top++]=pre;
Binary Tree ANSI C Implement  (1)            pre
=pre->lchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            pre
=s.data[--s.top];
Binary Tree ANSI C Implement  (1)            
printf("%c",pre->data);
Binary Tree ANSI C Implement  (1)            pre
=pre->rchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
printf("\n\n");
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*        按照后序非递归遍历二叉树         */
Binary Tree ANSI C Implement  (1)
/*******************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void Posorder2(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    stack s;
Binary Tree ANSI C Implement  (1)    s
.top=-1;
Binary Tree ANSI C Implement  (1)    
printf("输出后序遍历序列:");
Binary Tree ANSI C Implement  (1)    
while(t!=NULL||s.top!=-1)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
while(t)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            s
.top++;
Binary Tree ANSI C Implement  (1)            s
.flag[s.top]=0;
Binary Tree ANSI C Implement  (1)            s
.data[s.top]=t;
Binary Tree ANSI C Implement  (1)            t
=t->lchild;
Binary Tree ANSI C Implement  (1)           
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
while(s.top>=0&&s.flag[s.top]==1)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            t
=s.data[s.top];
Binary Tree ANSI C Implement  (1)            
printf("%c",t->data);
Binary Tree ANSI C Implement  (1)            s
.top--;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
if(s.top>=0)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            t
=s.data[s.top];
Binary Tree ANSI C Implement  (1)            s
.flag[s.top]=1;
Binary Tree ANSI C Implement  (1)            t
=t->rchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            t
=NULL;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
printf("\n\n");
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*           按照层次遍历二叉树            */
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)void Levelorder(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    queue q;
Binary Tree ANSI C Implement  (1)    q
.data[0]=t;
Binary Tree ANSI C Implement  (1)    q
.front=0;q.rear=1;
Binary Tree ANSI C Implement  (1)    
printf("层次遍历二叉树结果:");
Binary Tree ANSI C Implement  (1)    
while(q.front<q.rear)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(q.data[q.front])
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            
printf("%c",q.data[q.front]->data);
Binary Tree ANSI C Implement  (1)            q
.data[q.rear++]=q.data[q.front]->lchild;
Binary Tree ANSI C Implement  (1)            q
.data[q.rear++]=q.data[q.front]->rchild;
Binary Tree ANSI C Implement  (1)            q
.front++;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            q
.front++;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
printf("\n\n");
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h"
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*      递归法将二叉树的左右子树互换       */
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)void Exchange1(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree temp;
Binary Tree ANSI C Implement  (1)    
if(t)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        Exchange1(t
->lchild);
Binary Tree ANSI C Implement  (1)        Exchange1(t
->rchild);
Binary Tree ANSI C Implement  (1)        temp
=t->lchild;
Binary Tree ANSI C Implement  (1)        t
->lchild=t->rchild;
Binary Tree ANSI C Implement  (1)        t
->rchild=temp;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)}
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*      非递归法将二叉树的左右子树互换     */
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)void Exchange2(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree temp;
Binary Tree ANSI C Implement  (1)    stack s;
Binary Tree ANSI C Implement  (1)    s
.top=0;
Binary Tree ANSI C Implement  (1)    
while(t||s.top)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(t)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            s
.data[s.top++]=t;
Binary Tree ANSI C Implement  (1)            temp
=t->lchild;
Binary Tree ANSI C Implement  (1)            t
->lchild=t->rchild;
Binary Tree ANSI C Implement  (1)            t
->rchild=temp;
Binary Tree ANSI C Implement  (1)            t
=t->lchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            t
=s.data[--s.top]->rchild;
Binary Tree ANSI C Implement  (1)        } 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)}
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree t;
Binary Tree ANSI C Implement  (1)    Creat_Bintree(
&t);
Binary Tree ANSI C Implement  (1)    Exchange2(t);
Binary Tree ANSI C Implement  (1)    Inorder2(t);
Binary Tree ANSI C Implement  (1)    
return 0;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)  
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h"
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*        递归法求叶子结点个数             */
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
int Leaves_Num1(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
if(t)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(t->lchild==NULL&&t->rchild==NULL)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            
return 1;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
return Leaves_Num1(t->lchild)+Leaves_Num1(t->rchild);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
return 0;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
/*        非递归法求叶子结点个数             */
Binary Tree ANSI C Implement  (1)
/*******************************************/
Binary Tree ANSI C Implement  (1)
int Leaves_Num2(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
int count=0;
Binary Tree ANSI C Implement  (1)    stack s;
Binary Tree ANSI C Implement  (1)    s
.top=0;
Binary Tree ANSI C Implement  (1)    
while(t||s.top>0)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(t)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)             s
.data[s.top++]=t;
Binary Tree ANSI C Implement  (1)             
if(t->lchild==NULL&&t->rchild==NULL)
Binary Tree ANSI C Implement  (1)             { 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)                count
++;
Binary Tree ANSI C Implement  (1)             }
Binary Tree ANSI C Implement  (1)             t
=t->lchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            t
=s.data[--s.top]->rchild;
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
return count;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)        
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
int count=0;
Binary Tree ANSI C Implement  (1)    Bintree t;
Binary Tree ANSI C Implement  (1)    Creat_Bintree(
&t);
Binary Tree ANSI C Implement  (1)    count
=Leaves_Num2(t);
Binary Tree ANSI C Implement  (1)    
printf("该二叉树的叶子结点数为:%d\n",count);
Binary Tree ANSI C Implement  (1)    
return 0;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h" 
Binary Tree ANSI C Implement  (1)

Binary Tree ANSI C Implement  (1)
/**********************************************/
Binary Tree ANSI C Implement  (1)
/*                  求一棵树的高度            */
Binary Tree ANSI C Implement  (1)
/**********************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
int Depth(Bintree t)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
int  lh , rh ;
Binary Tree ANSI C Implement  (1)    
if( NULL == t )
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
return 0 ;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        lh 
= Depth( t->lchild ) ;
Binary Tree ANSI C Implement  (1)        rh 
= Depth( t->rchild ) ;
Binary Tree ANSI C Implement  (1)        
return ( lh > rh ? lh : rh ) + 1 ;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree t ;
Binary Tree ANSI C Implement  (1)    Creat_Bintree( 
&t ) ;
Binary Tree ANSI C Implement  (1)    
printf"树的高度是%d\n" , Depth( t ) ) ;
Binary Tree ANSI C Implement  (1)    
return 0 ;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)  
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h"
Binary Tree ANSI C Implement  (1)
/*******************************************************/
Binary Tree ANSI C Implement  (1)
/*      已知一课棵二叉树的中序和后序,建立这棵树       */
Binary Tree ANSI C Implement  (1)
/*******************************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)void In_Pos_order(Bintree 
*t,char *s,char *r)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    char La[
30],Lb[30],Ra[30],Rb[30];
Binary Tree ANSI C Implement  (1)    
int i,len,length=strlen(r);
Binary Tree ANSI C Implement  (1)    
if(length>0&&r[length-1]!='\0')
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
*t=(Binnode *)malloc(sizeof(Binnode));
Binary Tree ANSI C Implement  (1)        (
*t)->data=r[length-1];
Binary Tree ANSI C Implement  (1)        
for(i=0;s[i]!=r[length-1];i++)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            Ra[i]
=s[i];
Binary Tree ANSI C Implement  (1)            La[i]
=r[i];
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        len
=i;
Binary Tree ANSI C Implement  (1)        Ra[len]
='\0'//左中
Binary Tree ANSI C Implement  (1)        La[len]
='\0'//左后
Binary Tree ANSI C Implement  (1)        
for(i=len+1;i<strlen(s);i++)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            Rb[i
-len-1]=s[i];
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        Rb[i
-len-1]='\0';
Binary Tree ANSI C Implement  (1)        
for(i=len;i<strlen(r)-1;i++)
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            Lb[i
-len]=r[i];
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)        Lb[i
-len]='\0';
Binary Tree ANSI C Implement  (1)        In_Pos_order(
&(*t)->lchild,Ra,La);
Binary Tree ANSI C Implement  (1)        In_Pos_order(
&(*t)->rchild,Rb,Lb);
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
*t=NULL;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree t;
Binary Tree ANSI C Implement  (1)    char in[
30]="ABCEFGHD",pos[30]="ABFHGEDC";//测试数据
Binary Tree ANSI C Implement  (1)    
printf("输入中序遍历序列:");
Binary Tree ANSI C Implement  (1)    scanf(
"%s",in);
Binary Tree ANSI C Implement  (1)    
printf("输入后序遍历序列:");
Binary Tree ANSI C Implement  (1)    scanf(
"%s",in);
Binary Tree ANSI C Implement  (1)    In_Pos_order(
&t,in,pos);
Binary Tree ANSI C Implement  (1)    Preorder2(t);
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h"
Binary Tree ANSI C Implement  (1)
/*******************************************************/
Binary Tree ANSI C Implement  (1)
/*                  判断两棵是否等价                   */
Binary Tree ANSI C Implement  (1)
/*******************************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
int Is_equal( Bintree t1 , Bintree t2 )
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    
int t=0;
Binary Tree ANSI C Implement  (1)    
if(NULL == t1 && NULL == t2)
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        t
=1;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if(NULL !=t1 &&NULL != t2 )
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            
if(t1->data == t2->data)
Binary Tree ANSI C Implement  (1)            {
Binary Tree ANSI C Implement  (1)                
if(Is_equal(t1->lchild,t2->lchild))
Binary Tree ANSI C Implement  (1)                {
Binary Tree ANSI C Implement  (1)                    t
=Is_equal(t1->rchild,t2->rchild);
Binary Tree ANSI C Implement  (1)                }
Binary Tree ANSI C Implement  (1)            }
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
return t;
Binary Tree ANSI C Implement  (1)}
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree t1
,t2;
Binary Tree ANSI C Implement  (1)    Creat_Bintree(
&t1);
Binary Tree ANSI C Implement  (1)    getchar();
Binary Tree ANSI C Implement  (1)    Creat_Bintree(
&t2);
Binary Tree ANSI C Implement  (1)    
if(Is_equal(t1,t2))
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)         
printf"Yes!\n") ;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
printf"No!\n" ) ;
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)    
Binary Tree ANSI C Implement  (1)    
return 0 ;
Binary Tree ANSI C Implement  (1)}
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)  
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
#include"Bintree.h"
Binary Tree ANSI C Implement  (1)
/****************************************************/
Binary Tree ANSI C Implement  (1)
/*            查找某个信息是否在这棵树中            */
Binary Tree ANSI C Implement  (1)
/****************************************************/ 
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)Bintree locale_x(Bintree t
,char x)
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree p;
Binary Tree ANSI C Implement  (1)    
if(t==NULL) return NULL;
Binary Tree ANSI C Implement  (1)    
else
Binary Tree ANSI C Implement  (1)    {
Binary Tree ANSI C Implement  (1)        
if( t -> data == x ) return t;
Binary Tree ANSI C Implement  (1)        
else
Binary Tree ANSI C Implement  (1)        {
Binary Tree ANSI C Implement  (1)            p 
= locale_x(t->lchild,x);
Binary Tree ANSI C Implement  (1)            
if(p)return p;
Binary Tree ANSI C Implement  (1)            
else
Binary Tree ANSI C Implement  (1)            
return locale_x(t->rchild,x);
Binary Tree ANSI C Implement  (1)        }
Binary Tree ANSI C Implement  (1)    }
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
int main()
Binary Tree ANSI C Implement  (1){
Binary Tree ANSI C Implement  (1)    Bintree root
,p;
Binary Tree ANSI C Implement  (1)    char ch;
Binary Tree ANSI C Implement  (1)    Creat_Bintree(
&root);
Binary Tree ANSI C Implement  (1)    getchar();
Binary Tree ANSI C Implement  (1)    
printf("输入要查找的值:");
Binary Tree ANSI C Implement  (1)    scanf(
"%c",&ch);
Binary Tree ANSI C Implement  (1)    p
=locale_x(root,ch);
Binary Tree ANSI C Implement  (1)    
if(p)printf"YES!\n" ) ;
Binary Tree ANSI C Implement  (1)    
else printf"NO!\n" ) ;
Binary Tree ANSI C Implement  (1)    
return 0;
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)
Binary Tree ANSI C Implement  (1)

相关文章:

  • 2021-11-30
  • 2021-08-19
  • 2022-12-23
  • 2021-10-15
  • 2021-05-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-13
相关资源
相似解决方案