[原创][开源]二叉树基本操作的程序实现.
<P>//Bintree.h<BR>#include<stdio.h><BR>#include<malloc.h><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)->data=ch;<BR> Creat_Bintree(&(*root)->lchild);<BR> Creat_Bintree(&(*root)->rchild);<BR> }<BR>}</P>
<P>/*******************************************/<BR>/* 按照前序递归遍历二叉树 */<BR>/*******************************************/</P>
<P>void Preorder1(Bintree t)<BR>{<BR> if(t!=NULL)<BR> {<BR> printf("%c",t->data);<BR> Preorder1(t->lchild);<BR> Preorder1(t->rchild);<BR> }<BR>}</P>
<P><BR>/*******************************************/<BR>/* 按照中序递归遍历二叉树 */<BR>/*******************************************/</P>
<P>void Inorder1(Bintree t)<BR>{<BR> if(t!=NULL)<BR> {<BR> Inorder1(t->lchild);<BR> printf("%c",t->data);<BR> Inorder1(t->rchild);<BR> }<BR>}</P>
<P>/*******************************************/<BR>/* 按照后序递归遍历二叉树 */<BR>/*******************************************/</P>
<P>void Posorder1(Bintree t)<BR>{<BR> if(t!=NULL)<BR> {<BR> Posorder1(t->lchild);<BR> Posorder1(t->rchild);<BR> printf("%c",t->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>0)<BR> {<BR> if(pre)<BR> {<BR> printf("%c",pre->data);<BR> s.data[s.top++]=pre;<BR> pre=pre->lchild;<BR> }<BR> else<BR> {<BR> pre=s.data[--s.top]->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>0)<BR> {<BR> if(pre)<BR> {<BR> s.data[s.top++]=pre;<BR> pre=pre->lchild;<BR> }<BR> else<BR> {<BR> pre=s.data[--s.top];<BR> printf("%c",pre->data);<BR> pre=pre->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->lchild;<BR> <BR> }<BR> while(s.top>=0&&s.flag[s.top]==1)<BR> {<BR> t=s.data[s.top];<BR> printf("%c",t->data);<BR> s.top--;<BR> }<BR> if(s.top>=0)<BR> {<BR> t=s.data[s.top];<BR> s.flag[s.top]=1;<BR> t=t->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<q.rear)<BR> {<BR> if(q.data[q.front])<BR> {<BR> printf("%c",q.data[q.front]->data);<BR> q.data[q.rear++]=q.data[q.front]->lchild;<BR> q.data[q.rear++]=q.data[q.front]->rchild;<BR> q.front++;<BR> }<BR> else<BR> {<BR> q.front++;<BR> }<BR> }<BR> printf("\n\n");<BR>}<BR></P> <P>#include"Bintree.h"<BR>/*******************************************/<BR>/* 递归法将二叉树的左右子树互换 */<BR>/*******************************************/<BR>void Exchange1(Bintree t)<BR>{<BR> Bintree temp;<BR> if(t)<BR> {<BR> Exchange1(t->lchild);<BR> Exchange1(t->rchild);<BR> temp=t->lchild;<BR> t->lchild=t->rchild;<BR> t->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->lchild;<BR> t->lchild=t->rchild;<BR> t->rchild=temp;<BR> t=t->lchild;<BR> }<BR> else<BR> {<BR> t=s.data[--s.top]->rchild;<BR> }</P>
<P> }<BR>}<BR>int main()<BR>{<BR> Bintree t;<BR> Creat_Bintree(&t);<BR> Exchange2(t);<BR> Inorder2(t);<BR> return 0;<BR>}<BR> <BR></P> <P>#include"Bintree.h"<BR>/*******************************************/<BR>/* 递归法求叶子结点个数 */<BR>/*******************************************/<BR>int Leaves_Num1(Bintree t)<BR>{<BR> if(t)<BR> {<BR> if(t->lchild==NULL&&t->rchild==NULL)<BR> {<BR> return 1;<BR> }<BR> return Leaves_Num1(t->lchild)+Leaves_Num1(t->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>0)<BR> {<BR> if(t)<BR> {<BR> s.data[s.top++]=t;<BR> if(t->lchild==NULL&&t->rchild==NULL)<BR> {</P>
<P> count++;<BR> }<BR> t=t->lchild;<BR> }<BR> else<BR> {<BR> t=s.data[--s.top]->rchild;<BR> }<BR> }<BR> return count;<BR>}</P>
<P> <BR>int main()<BR>{<BR> int count=0;<BR> Bintree t;<BR> Creat_Bintree(&t);<BR> count=Leaves_Num2(t);<BR> printf("该二叉树的叶子结点数为:%d\n",count);<BR> return 0;<BR>}</P>
<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->lchild ) ;<BR> rh = Depth( t->rchild ) ;<BR> return ( lh > rh ? lh : rh ) + 1 ;<BR> }<BR>}</P>
<P>int main()<BR>{<BR> Bintree t ;<BR> Creat_Bintree( &t ) ;<BR> printf( "树的高度是%d\n" , Depth( t ) ) ;<BR> return 0 ;<BR>}<BR></P> <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>0&&r[length-1]!='\0')<BR> {<BR> *t=(Binnode *)malloc(sizeof(Binnode));<BR> (*t)->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<strlen(s);i++)<BR> {<BR> Rb[i-len-1]=s[i];<BR> }<BR> Rb[i-len-1]='\0';<BR> for(i=len;i<strlen(r)-1;i++)<BR> {<BR> Lb[i-len]=r[i];<BR> }<BR> Lb[i-len]='\0';<BR> In_Pos_order(&(*t)->lchild,Ra,La);<BR> In_Pos_order(&(*t)->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(&t,in,pos);<BR> Preorder2(t);<BR>}<BR></P> <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 && NULL == t2)<BR> {<BR> t=1;<BR> }<BR> else<BR> {<BR> if(NULL !=t1 &&NULL != t2 )<BR> {<BR> if(t1->data == t2->data)<BR> {<BR> if(Is_equal(t1->lchild,t2->lchild))<BR> {<BR> t=Is_equal(t1->rchild,t2->rchild);<BR> }<BR> }<BR> }<BR> }<BR> return t;<BR>}<BR>int main()<BR>{<BR> Bintree t1,t2;<BR> Creat_Bintree(&t1);<BR> getchar();<BR> Creat_Bintree(&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> <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 -> data == x ) return t;<BR> else<BR> {<BR> p = locale_x(t->lchild,x);<BR> if(p)return p;<BR> else<BR> return locale_x(t->rchild,x);<BR> }<BR> }<BR>}</P>
<P>int main()<BR>{<BR> Bintree root,p;<BR> char ch;<BR> Creat_Bintree(&root);<BR> getchar();<BR> printf("输入要查找的值:");<BR> scanf("%c",&ch);<BR> p=locale_x(root,ch);<BR> if(p)printf( "YES!\n" ) ;<BR> else printf( "NO!\n" ) ;<BR> return 0;<BR>}</P>
<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->lchild)+num_of_node(t->rchild)+1;<BR>}</P>
<P><BR>int main()<BR>{<BR> Bintree root,p;<BR> Creat_Bintree(&root);<BR> printf("%d\n",num_of_node(root));<BR> return 0;<BR>}<BR></P> <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)->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<len;i++)<BR> {<BR> La[i]=s[i+1];<BR> }<BR> La[len]='\0'; //左前<BR> for(i=len+1;i<strlen(r);i++)<BR> {<BR> Rb[i-len-1]=r[i];<BR> }<BR> Rb[i-len-1]='\0';<BR> for(i=len+1;i<strlen(s);i++)<BR> {<BR> Lb[i-len-1]=s[i];<BR> }<BR> Lb[i-len-1]='\0';<BR> Pre_In_order(&(*t)->lchild,La,Ra);<BR> Pre_In_order(&(*t)->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(&t,pre,in);<BR> Posorder1(t);<BR>}<BR> </P>
<P><BR> <BR></P> <P>#include<stdio.h><BR>#include<malloc.h><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",&x);<BR> p=(linkhuf)malloc(sizeof(hufnode));<BR> p->data=x;<BR> p->lchild=NULL;<BR> p->rchild=NULL;<BR> if(NULL==head)<BR> {<BR> head=p;<BR> pre=head;<BR> }<BR> else<BR> {<BR> p->next=pre->next;<BR> pre->next=p;<BR> pre=pre->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&&p2->data<s->data)<BR> {<BR> p1=p2;<BR> p2=p2->next;<BR> }<BR> s->next=p2;<BR> if(NULL==p1)<BR> {<BR> root=s;<BR> }<BR> else<BR> {<BR> p1->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->data);<BR> Preorder1(t->lchild);<BR> Preorder1(t->rchild);<BR> }<BR>}<BR>void creathuffman(linkhuf *root)//构造Huffman树。<BR>{<BR> linkhuf s, rl,rr;<BR> while(*root && (*root)->next)<BR> {<BR> rl=*root;<BR> rr=(*root)->next;<BR> *root=rr->next;<BR> s=(linkhuf)malloc(sizeof(hufnode));<BR> s->next=NULL;<BR> s->data=rl->data+rr->data;<BR> s->lchild=rl;<BR> s->rchild=rr;<BR> rl->next=rr->next=NULL;<BR> *root=insert(*root,s);<BR> }<BR>}</P>
<P>int main()<BR>{<BR> linkhuf root;<BR> int n;<BR> scanf("%d",&n);<BR> root=Creat_Node(n);<BR> creathuffman(&root);<BR> Preorder1(root);<BR> printf("\n");<BR> return 0;<BR>}<BR> </P>
我顶,虽然我没看,但实在感动楼主的用苦良心,我们做数据结构实验时就写了这些,写得好烦呀,楼主早贴出来该多好。<BR> 看了一些 特别是非递归形式非常好 非常感谢 写得很好呀!!! <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->data=ch;<BR> node.data[node.rear++]=root; //用队列实现层次遍历<BR> while(node.front<node.rear)<BR> {<BR> p=node.data[node.front++];<BR> ch=getchar(); //为了简化操作,分别对左右子结点进行赋值。<BR> if(ch!=' ')//子树不空则进队列进行扩充。下同<BR> {<BR> s=(Binnode*)malloc(sizeof(Binnode));<BR> s->data=ch;<BR> p->lchild=s;<BR> node.data[node.rear++]=s;<BR> }<BR> else<BR> {<BR> p->lchild=NULL;<BR> }<BR> ch=getchar();<BR> if(ch!=' ')<BR> {<BR> s=(Binnode*)malloc(sizeof(Binnode));<BR> s->data=ch;<BR> p->rchild=s;<BR> node.data[node.rear++]=s;<BR> }<BR> else<BR> {<BR> p->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> <P>[em17]</P> 接受了你的算法思想,谢谢了啊,不过问下,递归建树,为什么要用指针的指针?不能直接用指针啊? 对结点本身是个指针.如果你要修改根结点,那就得用指针的指针才可以保存修改. 楼主写的很好,前几个看懂了,后几个不是太明白 问下楼主,比较复杂的程序是怎样想到怎么做的?看的懂,但是自己通常编不出来 练多了就可以了.有些经典的算法可以去背,不过我记性差点,至今没背好.<BR>熟能生巧.呵呵.
页:
[1]
2
