二叉樹基本操作的程序實現

//Bintree.h
#include<stdio.h>
#include<malloc.h>
typedef struct Binnode{//二叉樹結點結構體
    char data;
    struct Binnode *lchild;
    struct Binnode *rchild;
  };
typedef Binnode *Bintree ;

typedef struct stack{ //二叉樹結點棧
     Bintree data[100];
    int flag[100];
    int top;
  };

typedef struct queue{ //二叉樹結點隊列
    Bintree data[30];
    int front;
    int rear;
  };


  
/*******************************************/
/*          按照前序遍歷建立二叉樹         */
/*******************************************/

void Creat_Bintree(Bintree *root)
{
    char ch;
    if((ch=getchar())==' ')
    {
        *root=NULL;

    }
    else
    {
        *root=(Binnode*)malloc(sizeof(Binnode));
        (*root)->data=ch;
        Creat_Bintree(&(*root)->lchild);
        Creat_Bintree(&(*root)->rchild);
    }
}

/*******************************************/
/*          按照前序遞歸遍歷二叉樹         */
/*******************************************/

void Preorder1(Bintree t)
{
    if(t!=NULL)
    {
        printf("%c",t->data);
        Preorder1(t->lchild);
        Preorder1(t->rchild);
    }
}


/*******************************************/
/*          按照中序遞歸遍歷二叉樹         */
/*******************************************/

void Inorder1(Bintree t)
{
    if(t!=NULL)
    {
        Inorder1(t->lchild);
        printf("%c",t->data);
        Inorder1(t->rchild);
    }
}

/*******************************************/
/*          按照後序遞歸遍歷二叉樹         */
/*******************************************/

void Posorder1(Bintree t)
{
    if(t!=NULL)
    {
        Posorder1(t->lchild);
        Posorder1(t->rchild);
        printf("%c",t->data);
    }
}
/*******************************************/
/*          按照前序非遞歸遍歷二叉樹         */
/*******************************************/

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");
}

/*******************************************/
/*          按照中序非遞歸遍歷二叉樹         */
/*******************************************/

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");
}

/*******************************************/
/*        按照後序非遞歸遍歷二叉樹         */
/*******************************************/

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");
}


/*******************************************/
/*           按照層次遍歷二叉樹            */
/*******************************************/
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");
}

 

#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;
        }

    }
}
int main()
{
    Bintree t;
    Creat_Bintree(&t);
    Exchange2(t);
    Inorder2(t);
    return 0;
}

#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;
}

/*******************************************/
/*        非遞歸法求葉子結點個數             */
/*******************************************/
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)
             {

                count++;
             }
             t=t->lchild;
        }
        else
        {
            t=s.data[--s.top]->rchild;
        }
    }
    return count;
}

        
int main()
{
    int count=0;
    Bintree t;
    Creat_Bintree(&t);
    count=Leaves_Num2(t);
    printf("該二叉樹的葉子結點數爲:%d/n",count);
    return 0;
}

#include"Bintree.h"

/**********************************************/
/*                  求一棵樹的高度            */
/**********************************************/

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 ;
    }
}

int main()
{
    Bintree t ;
    Creat_Bintree( &t ) ;
    printf( "樹的高度是%d/n" , Depth( t ) ) ;
    return 0 ;
}

#include"Bintree.h"
/*******************************************************/
/*      已知一課棵二叉樹的中序和後序,建立這棵樹       */
/*******************************************************/

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;
    }
}

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);
}

#include"Bintree.h"
/*******************************************************/
/*                  判斷兩棵是否等價                   */
/*******************************************************/

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 ;
}

#include"Bintree.h"
/****************************************************/
/*            查找某個信息是否在這棵樹中            */
/****************************************************/

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);
        }
    }
}

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;
}

#include"Bintree.h"

/****************************************************/
/*                  樹的結點個數                    */
/****************************************************/
int  num_of_node(Bintree t)
{
    if(t==NULL)return 0 ;
    else return num_of_node(t->lchild)+num_of_node(t->rchild)+1;
}


int main()
{
    Bintree root,p;
    Creat_Bintree(&root);
    printf("%d/n",num_of_node(root));
    return 0;
}

#include"Bintree.h"

/*******************************************************/
/*     已知一課棵二叉樹的中序和前序序列,建立這棵樹    */
/*******************************************************/

void Pre_In_order(Bintree *t,char *s,char *r)
{
    char La[30],Lb[30],Ra[30],Rb[30];
    int i,len;
    if(s[0]!='/0')
    {
        *t=(Binnode *)malloc(sizeof(Binnode));
        (*t)->data=s[0];
        for(i=0;r[i]!=s[0];i++)
        {
            Ra[i]=r[i];
        }
        len=i;
        Ra[len]='/0'; //左中
        for(i=0;i<len;i++)
        {
            La[i]=s[i+1];
        }
        La[len]='/0'; //左前
        for(i=len+1;i<strlen(r);i++)
        {
            Rb[i-len-1]=r[i];
        }
        Rb[i-len-1]='/0';
        for(i=len+1;i<strlen(s);i++)
        {
            Lb[i-len-1]=s[i];
        }
        Lb[i-len-1]='/0';
        Pre_In_order(&(*t)->lchild,La,Ra);
        Pre_In_order(&(*t)->rchild,Lb,Rb);
    }
    else
    {
        *t=NULL;
    }
}

int main()
{
    Bintree t;
    char pre[30]="ABCDEF",in[30]="CBAEDF";//測試數據
    printf("輸入前序遍歷序列:");
    scanf("%s",pre);
    printf("輸入中序遍歷序列:");
    scanf("%s",in);
    Pre_In_order(&t,pre,in);
    Posorder1(t);
}

#include<stdio.h>
#include<malloc.h>
typedef struct node{
    int data;
    struct node *lchild,*rchild,*next;
  }hufnode;

typedef hufnode *linkhuf;
/****************************************************/
/*                      huffman樹                   */
/****************************************************/
linkhuf Creat_Node(int n) //創建一單鏈表
{
    linkhuf head,pre,p;
    int x;
    head=NULL;
    while(n--)
    {
        scanf("%d",&x);
        p=(linkhuf)malloc(sizeof(hufnode));
        p->data=x;
        p->lchild=NULL;
        p->rchild=NULL;
        if(NULL==head)
        {
            head=p;
            pre=head;
        }
        else
        {
            p->next=pre->next;
            pre->next=p;
            pre=pre->next;
        }
    }
    return head;
}
linkhuf insert(linkhuf root , linkhuf s)//將結點S插入到有序Huffman root中。
{
    linkhuf p1,p2;
    if(NULL == root ) root=s;
    else
    {
        p1=NULL;
        p2=root;
        while(p2&&p2->data<s->data)
        {
            p1=p2;
            p2=p2->next;
        }
        s->next=p2;
        if(NULL==p1)
        {
            root=s;
        }
        else
        {
            p1->next=s;
        }
    }
    return root;
}


void Preorder1(linkhuf t)
{
    if(t!=NULL)
    {
        printf("%-6d",t->data);
        Preorder1(t->lchild);
        Preorder1(t->rchild);
    }
}
void creathuffman(linkhuf *root)//構造Huffman樹。
{
    linkhuf s, rl,rr;
    while(*root && (*root)->next)
    {
        rl=*root;
        rr=(*root)->next;
        *root=rr->next;
        s=(linkhuf)malloc(sizeof(hufnode));
        s->next=NULL;
        s->data=rl->data+rr->data;
        s->lchild=rl;
        s->rchild=rr;
        rl->next=rr->next=NULL;
        *root=insert(*root,s);
    }
}

int main()
{
    linkhuf root;
    int n;
    scanf("%d",&n);
    root=Creat_Node(n);
    creathuffman(&root);
    Preorder1(root);
    printf("/n");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章