编程论坛's Archiver

nuciewth 发表于 2007-5-5 16:10

[原创][开源]二叉树基本操作的程序实现.

<P>//Bintree.h<BR>#include&lt;stdio.h&gt;<BR>#include&lt;malloc.h&gt;<BR>typedef struct Binnode{//二叉树结点结构体<BR>    char data;<BR>    struct Binnode *lchild;<BR>    struct Binnode *rchild;<BR>  };<BR>typedef Binnode *Bintree ;</P>
<P>typedef struct stack{ //二叉树结点栈<BR>     Bintree data[100];<BR>    int flag[100];<BR>    int top;<BR>  };</P>
<P>typedef struct queue{ //二叉树结点队列<BR>    Bintree data[30];<BR>    int front;<BR>    int rear;<BR>  };</P>
<P><BR>  <BR>/*******************************************/<BR>/*          按照前序遍历建立二叉树         */<BR>/*******************************************/</P>
<P>void Creat_Bintree(Bintree *root)<BR>{<BR>    char ch;<BR>    if((ch=getchar())==' ')<BR>    {<BR>        *root=NULL;</P>
<P>    }<BR>    else<BR>    {<BR>        *root=(Binnode*)malloc(sizeof(Binnode));<BR>        (*root)-&gt;data=ch;<BR>        Creat_Bintree(&amp;(*root)-&gt;lchild);<BR>        Creat_Bintree(&amp;(*root)-&gt;rchild);<BR>    }<BR>}</P>
<P>/*******************************************/<BR>/*          按照前序递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Preorder1(Bintree t)<BR>{<BR>    if(t!=NULL)<BR>    {<BR>        printf("%c",t-&gt;data);<BR>        Preorder1(t-&gt;lchild);<BR>        Preorder1(t-&gt;rchild);<BR>    }<BR>}</P>
<P><BR>/*******************************************/<BR>/*          按照中序递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Inorder1(Bintree t)<BR>{<BR>    if(t!=NULL)<BR>    {<BR>        Inorder1(t-&gt;lchild);<BR>        printf("%c",t-&gt;data);<BR>        Inorder1(t-&gt;rchild);<BR>    }<BR>}</P>
<P>/*******************************************/<BR>/*          按照后序递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Posorder1(Bintree t)<BR>{<BR>    if(t!=NULL)<BR>    {<BR>        Posorder1(t-&gt;lchild);<BR>        Posorder1(t-&gt;rchild);<BR>        printf("%c",t-&gt;data);<BR>    }<BR>}<BR>/*******************************************/<BR>/*          按照前序非递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Preorder2(Bintree t)<BR>{<BR>    Bintree pre=t;<BR>    stack s;<BR>    s.top=0;<BR>    printf("输出前序遍历序列:");<BR>    while(pre||s.top&gt;0)<BR>    {<BR>        if(pre)<BR>        {<BR>            printf("%c",pre-&gt;data);<BR>            s.data[s.top++]=pre;<BR>            pre=pre-&gt;lchild;<BR>        }<BR>        else<BR>        {<BR>            pre=s.data[--s.top]-&gt;rchild;<BR>        }<BR>    }<BR>    printf("\n\n");<BR>}</P>
<P>/*******************************************/<BR>/*          按照中序非递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Inorder2(Bintree t)<BR>{<BR>    Bintree pre=t;<BR>    stack s;<BR>    s.top=0;<BR>     printf("输出中序遍历序列:");<BR>    while(pre||s.top&gt;0)<BR>    {<BR>        if(pre)<BR>        {<BR>            s.data[s.top++]=pre;<BR>            pre=pre-&gt;lchild;<BR>        }<BR>        else<BR>        {<BR>            pre=s.data[--s.top];<BR>            printf("%c",pre-&gt;data);<BR>            pre=pre-&gt;rchild;<BR>        }<BR>    }<BR>    printf("\n\n");<BR>}</P>
<P>/*******************************************/<BR>/*        按照后序非递归遍历二叉树         */<BR>/*******************************************/</P>
<P>void Posorder2(Bintree t)<BR>{<BR>    stack s;<BR>    s.top=-1;<BR>    printf("输出后序遍历序列:");<BR>    while(t!=NULL||s.top!=-1)<BR>    {<BR>        while(t)<BR>        {<BR>            s.top++;<BR>            s.flag[s.top]=0;<BR>            s.data[s.top]=t;<BR>            t=t-&gt;lchild;<BR>           <BR>        }<BR>        while(s.top&gt;=0&amp;&amp;s.flag[s.top]==1)<BR>        {<BR>            t=s.data[s.top];<BR>            printf("%c",t-&gt;data);<BR>            s.top--;<BR>        }<BR>        if(s.top&gt;=0)<BR>        {<BR>            t=s.data[s.top];<BR>            s.flag[s.top]=1;<BR>            t=t-&gt;rchild;<BR>        }<BR>        else<BR>        {<BR>            t=NULL;<BR>        }<BR>    }<BR>    printf("\n\n");<BR>}</P>
<P><BR>/*******************************************/<BR>/*           按照层次遍历二叉树            */<BR>/*******************************************/<BR>void Levelorder(Bintree t)<BR>{<BR>    queue q;<BR>    q.data[0]=t;<BR>    q.front=0;q.rear=1;<BR>    printf("层次遍历二叉树结果:");<BR>    while(q.front&lt;q.rear)<BR>    {<BR>        if(q.data[q.front])<BR>        {<BR>            printf("%c",q.data[q.front]-&gt;data);<BR>            q.data[q.rear++]=q.data[q.front]-&gt;lchild;<BR>            q.data[q.rear++]=q.data[q.front]-&gt;rchild;<BR>            q.front++;<BR>        }<BR>        else<BR>        {<BR>            q.front++;<BR>        }<BR>    }<BR>    printf("\n\n");<BR>}<BR></P>

nuciewth 发表于 2007-5-5 16:11

<P>#include"Bintree.h"<BR>/*******************************************/<BR>/*      递归法将二叉树的左右子树互换       */<BR>/*******************************************/<BR>void Exchange1(Bintree t)<BR>{<BR>    Bintree temp;<BR>    if(t)<BR>    {<BR>        Exchange1(t-&gt;lchild);<BR>        Exchange1(t-&gt;rchild);<BR>        temp=t-&gt;lchild;<BR>        t-&gt;lchild=t-&gt;rchild;<BR>        t-&gt;rchild=temp;<BR>    }<BR>}<BR>/*******************************************/<BR>/*      非递归法将二叉树的左右子树互换     */<BR>/*******************************************/<BR>void Exchange2(Bintree t)<BR>{<BR>    Bintree temp;<BR>    stack s;<BR>    s.top=0;<BR>    while(t||s.top)<BR>    {<BR>        if(t)<BR>        {<BR>            s.data[s.top++]=t;<BR>            temp=t-&gt;lchild;<BR>            t-&gt;lchild=t-&gt;rchild;<BR>            t-&gt;rchild=temp;<BR>            t=t-&gt;lchild;<BR>        }<BR>        else<BR>        {<BR>            t=s.data[--s.top]-&gt;rchild;<BR>        }</P>
<P>    }<BR>}<BR>int main()<BR>{<BR>    Bintree t;<BR>    Creat_Bintree(&amp;t);<BR>    Exchange2(t);<BR>    Inorder2(t);<BR>    return 0;<BR>}<BR>    <BR></P>

nuciewth 发表于 2007-5-5 16:11

<P>#include"Bintree.h"<BR>/*******************************************/<BR>/*        递归法求叶子结点个数             */<BR>/*******************************************/<BR>int Leaves_Num1(Bintree t)<BR>{<BR>    if(t)<BR>    {<BR>        if(t-&gt;lchild==NULL&amp;&amp;t-&gt;rchild==NULL)<BR>        {<BR>            return 1;<BR>        }<BR>        return Leaves_Num1(t-&gt;lchild)+Leaves_Num1(t-&gt;rchild);<BR>    }<BR>    return 0;<BR>}</P>
<P>/*******************************************/<BR>/*        非递归法求叶子结点个数             */<BR>/*******************************************/<BR>int Leaves_Num2(Bintree t)<BR>{<BR>    int count=0;<BR>    stack s;<BR>    s.top=0;<BR>    while(t||s.top&gt;0)<BR>    {<BR>        if(t)<BR>        {<BR>             s.data[s.top++]=t;<BR>             if(t-&gt;lchild==NULL&amp;&amp;t-&gt;rchild==NULL)<BR>             {</P>
<P>                count++;<BR>             }<BR>             t=t-&gt;lchild;<BR>        }<BR>        else<BR>        {<BR>            t=s.data[--s.top]-&gt;rchild;<BR>        }<BR>    }<BR>    return count;<BR>}</P>
<P>        <BR>int main()<BR>{<BR>    int count=0;<BR>    Bintree t;<BR>    Creat_Bintree(&amp;t);<BR>    count=Leaves_Num2(t);<BR>    printf("该二叉树的叶子结点数为:%d\n",count);<BR>    return 0;<BR>}</P>

nuciewth 发表于 2007-5-5 16:11

<P>#include"Bintree.h"</P>
<P>/**********************************************/<BR>/*                  求一棵树的高度            */<BR>/**********************************************/</P>
<P>int Depth(Bintree t)<BR>{<BR>    int  lh , rh ;<BR>    if( NULL == t )<BR>    {<BR>        return 0 ;<BR>    }<BR>    else<BR>    {<BR>        lh = Depth( t-&gt;lchild ) ;<BR>        rh = Depth( t-&gt;rchild ) ;<BR>        return ( lh &gt; rh ? lh : rh ) + 1 ;<BR>    }<BR>}</P>
<P>int main()<BR>{<BR>    Bintree t ;<BR>    Creat_Bintree( &amp;t ) ;<BR>    printf( "树的高度是%d\n" , Depth( t ) ) ;<BR>    return 0 ;<BR>}<BR></P>

nuciewth 发表于 2007-5-5 16:13

<P>#include"Bintree.h"<BR>/*******************************************************/<BR>/*      已知一课棵二叉树的中序和后序,建立这棵树       */<BR>/*******************************************************/</P>
<P>void In_Pos_order(Bintree *t,char *s,char *r)<BR>{<BR>    char La[30],Lb[30],Ra[30],Rb[30];<BR>    int i,len,length=strlen(r);<BR>    if(length&gt;0&amp;&amp;r[length-1]!='\0')<BR>    {<BR>        *t=(Binnode *)malloc(sizeof(Binnode));<BR>        (*t)-&gt;data=r[length-1];<BR>        for(i=0;s[i]!=r[length-1];i++)<BR>        {<BR>            Ra[i]=s[i];<BR>            La[i]=r[i];<BR>        }<BR>        len=i;<BR>        Ra[len]='\0'; //左中<BR>        La[len]='\0'; //左后<BR>        for(i=len+1;i&lt;strlen(s);i++)<BR>        {<BR>            Rb[i-len-1]=s[i];<BR>        }<BR>        Rb[i-len-1]='\0';<BR>        for(i=len;i&lt;strlen(r)-1;i++)<BR>        {<BR>            Lb[i-len]=r[i];<BR>        }<BR>        Lb[i-len]='\0';<BR>        In_Pos_order(&amp;(*t)-&gt;lchild,Ra,La);<BR>        In_Pos_order(&amp;(*t)-&gt;rchild,Rb,Lb);<BR>    }<BR>    else<BR>    {<BR>        *t=NULL;<BR>    }<BR>}</P>
<P>int main()<BR>{<BR>    Bintree t;<BR>    char in[30]="ABCEFGHD",pos[30]="ABFHGEDC";//测试数据<BR>    printf("输入中序遍历序列:");<BR>    scanf("%s",in);<BR>    printf("输入后序遍历序列:");<BR>    scanf("%s",in);<BR>    In_Pos_order(&amp;t,in,pos);<BR>    Preorder2(t);<BR>}<BR></P>

nuciewth 发表于 2007-5-5 16:13

<P>#include"Bintree.h"<BR>/*******************************************************/<BR>/*                  判断两棵是否等价                   */<BR>/*******************************************************/</P>
<P>int Is_equal( Bintree t1 , Bintree t2 )<BR>{<BR>    int t=0;<BR>    if(NULL == t1 &amp;&amp; NULL == t2)<BR>    {<BR>        t=1;<BR>    }<BR>    else<BR>    {<BR>        if(NULL !=t1 &amp;&amp;NULL != t2 )<BR>        {<BR>            if(t1-&gt;data == t2-&gt;data)<BR>            {<BR>                if(Is_equal(t1-&gt;lchild,t2-&gt;lchild))<BR>                {<BR>                    t=Is_equal(t1-&gt;rchild,t2-&gt;rchild);<BR>                }<BR>            }<BR>        }<BR>    }<BR>    return t;<BR>}<BR>int main()<BR>{<BR>    Bintree t1,t2;<BR>    Creat_Bintree(&amp;t1);<BR>    getchar();<BR>    Creat_Bintree(&amp;t2);<BR>    if(Is_equal(t1,t2))<BR>    {<BR>         printf( "Yes!\n") ;<BR>    }<BR>    else<BR>    {<BR>        printf( "No!\n" ) ;<BR>    }<BR>    <BR>    return 0 ;<BR>}<BR></P>

nuciewth 发表于 2007-5-5 16:15

<P>#include"Bintree.h"<BR>/****************************************************/<BR>/*            查找某个信息是否在这棵树中            */<BR>/****************************************************/</P>
<P>Bintree locale_x(Bintree t,char x)<BR>{<BR>    Bintree p;<BR>    if(t==NULL) return NULL;<BR>    else<BR>    {<BR>        if( t -&gt; data == x ) return t;<BR>        else<BR>        {<BR>            p = locale_x(t-&gt;lchild,x);<BR>            if(p)return p;<BR>            else<BR>            return locale_x(t-&gt;rchild,x);<BR>        }<BR>    }<BR>}</P>
<P>int main()<BR>{<BR>    Bintree root,p;<BR>    char ch;<BR>    Creat_Bintree(&amp;root);<BR>    getchar();<BR>    printf("输入要查找的值:");<BR>    scanf("%c",&amp;ch);<BR>    p=locale_x(root,ch);<BR>    if(p)printf( "YES!\n" ) ;<BR>    else printf( "NO!\n" ) ;<BR>    return 0;<BR>}</P>

nuciewth 发表于 2007-5-5 16:16

<P>#include"Bintree.h"</P>
<P>/****************************************************/<BR>/*                  树的结点个数                    */<BR>/****************************************************/<BR>int  num_of_node(Bintree t)<BR>{<BR>    if(t==NULL)return 0 ;<BR>    else return num_of_node(t-&gt;lchild)+num_of_node(t-&gt;rchild)+1;<BR>}</P>
<P><BR>int main()<BR>{<BR>    Bintree root,p;<BR>    Creat_Bintree(&amp;root);<BR>    printf("%d\n",num_of_node(root));<BR>    return 0;<BR>}<BR></P>

nuciewth 发表于 2007-5-5 16:17

<P>#include"Bintree.h"</P>
<P>/*******************************************************/<BR>/*     已知一课棵二叉树的中序和前序序列,建立这棵树    */<BR>/*******************************************************/</P>
<P>void Pre_In_order(Bintree *t,char *s,char *r)<BR>{<BR>    char La[30],Lb[30],Ra[30],Rb[30];<BR>    int i,len;<BR>    if(s[0]!='\0')<BR>    {<BR>        *t=(Binnode *)malloc(sizeof(Binnode));<BR>        (*t)-&gt;data=s[0];<BR>        for(i=0;r[i]!=s[0];i++)<BR>        {<BR>            Ra[i]=r[i];<BR>        }<BR>        len=i;<BR>        Ra[len]='\0'; //左中<BR>        for(i=0;i&lt;len;i++)<BR>        {<BR>            La[i]=s[i+1];<BR>        }<BR>        La[len]='\0'; //左前<BR>        for(i=len+1;i&lt;strlen(r);i++)<BR>        {<BR>            Rb[i-len-1]=r[i];<BR>        }<BR>        Rb[i-len-1]='\0';<BR>        for(i=len+1;i&lt;strlen(s);i++)<BR>        {<BR>            Lb[i-len-1]=s[i];<BR>        }<BR>        Lb[i-len-1]='\0';<BR>        Pre_In_order(&amp;(*t)-&gt;lchild,La,Ra);<BR>        Pre_In_order(&amp;(*t)-&gt;rchild,Lb,Rb);<BR>    }<BR>    else<BR>    {<BR>        *t=NULL;<BR>    }<BR>}</P>
<P>int main()<BR>{<BR>    Bintree t;<BR>    char pre[30]="ABCDEF",in[30]="CBAEDF";//测试数据<BR>    printf("输入前序遍历序列:");<BR>    scanf("%s",pre);<BR>    printf("输入中序遍历序列:");<BR>    scanf("%s",in);<BR>    Pre_In_order(&amp;t,pre,in);<BR>    Posorder1(t);<BR>}<BR>    </P>
<P><BR>    <BR></P>

nuciewth 发表于 2007-5-5 16:19

<P>#include&lt;stdio.h&gt;<BR>#include&lt;malloc.h&gt;<BR>typedef struct node{<BR>    int data;<BR>    struct node *lchild,*rchild,*next;<BR>  }hufnode;</P>
<P>typedef hufnode *linkhuf;<BR>/****************************************************/<BR>/*                      huffman树                   */<BR>/****************************************************/<BR>linkhuf Creat_Node(int n) //创建一单链表<BR>{<BR>    linkhuf head,pre,p;<BR>    int x;<BR>    head=NULL;<BR>    while(n--)<BR>    {<BR>        scanf("%d",&amp;x);<BR>        p=(linkhuf)malloc(sizeof(hufnode));<BR>        p-&gt;data=x;<BR>        p-&gt;lchild=NULL;<BR>        p-&gt;rchild=NULL;<BR>        if(NULL==head)<BR>        {<BR>            head=p;<BR>            pre=head;<BR>        }<BR>        else<BR>        {<BR>            p-&gt;next=pre-&gt;next;<BR>            pre-&gt;next=p;<BR>            pre=pre-&gt;next;<BR>        }<BR>    }<BR>    return head;<BR>}<BR>linkhuf insert(linkhuf root , linkhuf s)//将结点S插入到有序Huffman root中。<BR>{<BR>    linkhuf p1,p2;<BR>    if(NULL == root ) root=s;<BR>    else<BR>    {<BR>        p1=NULL;<BR>        p2=root;<BR>        while(p2&amp;&amp;p2-&gt;data&lt;s-&gt;data)<BR>        {<BR>            p1=p2;<BR>            p2=p2-&gt;next;<BR>        }<BR>        s-&gt;next=p2;<BR>        if(NULL==p1)<BR>        {<BR>            root=s;<BR>        }<BR>        else<BR>        {<BR>            p1-&gt;next=s;<BR>        }<BR>    }<BR>    return root;<BR>}</P>
<P><BR>void Preorder1(linkhuf t)<BR>{<BR>    if(t!=NULL)<BR>    {<BR>        printf("%-6d",t-&gt;data);<BR>        Preorder1(t-&gt;lchild);<BR>        Preorder1(t-&gt;rchild);<BR>    }<BR>}<BR>void creathuffman(linkhuf *root)//构造Huffman树。<BR>{<BR>    linkhuf s, rl,rr;<BR>    while(*root &amp;&amp; (*root)-&gt;next)<BR>    {<BR>        rl=*root;<BR>        rr=(*root)-&gt;next;<BR>        *root=rr-&gt;next;<BR>        s=(linkhuf)malloc(sizeof(hufnode));<BR>        s-&gt;next=NULL;<BR>        s-&gt;data=rl-&gt;data+rr-&gt;data;<BR>        s-&gt;lchild=rl;<BR>        s-&gt;rchild=rr;<BR>        rl-&gt;next=rr-&gt;next=NULL;<BR>        *root=insert(*root,s);<BR>    }<BR>}</P>
<P>int main()<BR>{<BR>    linkhuf root;<BR>    int n;<BR>    scanf("%d",&amp;n);<BR>    root=Creat_Node(n);<BR>    creathuffman(&amp;root);<BR>    Preorder1(root);<BR>    printf("\n");<BR>    return 0;<BR>}<BR>    </P>

fyi1106 发表于 2007-5-5 16:26

我顶,虽然我没看,但实在感动楼主的用苦良心,我们做数据结构实验时就写了这些,写得好烦呀,楼主早贴出来该多好。<BR>

lishiyong110 发表于 2007-5-5 22:48

看了一些  特别是非递归形式非常好  非常感谢

kongbei 发表于 2007-5-7 08:52

写得很好呀!!!

nuciewth 发表于 2007-5-12 20:04

<P>/************************************************************/<BR>/*                  按层次顺序建立一棵二叉树                */<BR>/************************************************************/<BR>#include"Bintree.h"</P>
<P>Bintree Level_Creat()<BR>{<BR>    Bintree root,p,s;<BR>    queue node;<BR>    node.front=node.rear=0;<BR>    char ch;<BR>    ch=getchar();<BR>    if(ch==' ')<BR>    {<BR>        return NULL;<BR>    }<BR>    root=(Binnode*)malloc(sizeof(Binnode)); //生成根结点<BR>    root-&gt;data=ch;<BR>    node.data[node.rear++]=root; //用队列实现层次遍历<BR>    while(node.front&lt;node.rear)<BR>    {<BR>        p=node.data[node.front++];<BR>        ch=getchar(); //为了简化操作,分别对左右子结点进行赋值。<BR>        if(ch!=' ')//子树不空则进队列进行扩充。下同<BR>        {<BR>            s=(Binnode*)malloc(sizeof(Binnode));<BR>            s-&gt;data=ch;<BR>            p-&gt;lchild=s;<BR>            node.data[node.rear++]=s;<BR>        }<BR>        else<BR>        {<BR>            p-&gt;lchild=NULL;<BR>        }<BR>        ch=getchar();<BR>        if(ch!=' ')<BR>        {<BR>            s=(Binnode*)malloc(sizeof(Binnode));<BR>            s-&gt;data=ch;<BR>            p-&gt;rchild=s;<BR>            node.data[node.rear++]=s;<BR>        }<BR>        else<BR>        {<BR>            p-&gt;rchild=NULL;<BR>        }<BR>    }<BR>    return root;<BR>}<BR>int main()<BR>{<BR>    Bintree root;<BR>    root=Level_Creat();<BR>    Inorder1(root);//测试,中序遍历<BR>    return 0;<BR>}<BR></P>

Gislover 发表于 2007-5-15 10:21

<P>[em17]</P>

yuesheng 发表于 2007-5-15 11:46

  接受了你的算法思想,谢谢了啊,不过问下,递归建树,为什么要用指针的指针?不能直接用指针啊?

nuciewth 发表于 2007-5-15 12:32

对结点本身是个指针.如果你要修改根结点,那就得用指针的指针才可以保存修改.

yuji512512 发表于 2007-5-16 08:46

楼主写的很好,前几个看懂了,后几个不是太明白

yuesheng 发表于 2007-5-20 21:52

问下楼主,比较复杂的程序是怎样想到怎么做的?看的懂,但是自己通常编不出来

nuciewth 发表于 2007-5-21 21:51

练多了就可以了.有些经典的算法可以去背,不过我记性差点,至今没背好.<BR>熟能生巧.呵呵.

页: [1] 2

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.