二叉树的操作.docx
《二叉树的操作.docx》由会员分享,可在线阅读,更多相关《二叉树的操作.docx(26页珍藏版)》请在冰豆网上搜索。
二叉树的操作
#include
#include
typedefstructBinnode{//二叉树结点结构体
chardata;
structBinnode*lchild;
structBinnode*rchild;
};
typedefBinnode*Bintree;
typedefstructstack{//二叉树结点栈
Bintreedata[100];
intflag[100];
inttop;
};
typedefstructqueue{//二叉树结点队列
Bintreedata[30];
intfront;
intrear;
};
/*******************************************/
/*按照前序遍历建立二叉树*/
/*******************************************/
voidCreat_Bintree(Bintree*root)
{
charch;
if((ch=getchar())=='')
{
*root=NULL;
}
else
{
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
/*******************************************/
/*按照前序递归遍历二叉树*/
/*******************************************/
voidPreorder1(Bintreet)
{
if(t!
=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
/*******************************************/
/*按照中序递归遍历二叉树*/
/*******************************************/
voidInorder1(Bintreet)
{
if(t!
=NULL)
{
Inorder1(t->lchild);
printf("%c",t->data);
Inorder1(t->rchild);
}
}
/*******************************************/
/*按照后序递归遍历二叉树*/
/*******************************************/
voidPosorder1(Bintreet)
{
if(t!
=NULL)
{
Posorder1(t->lchild);
Posorder1(t->rchild);
printf("%c",t->data);
}
}
/*******************************************/
/*按照前序非递归遍历二叉树*/
/*******************************************/
voidPreorder2(Bintreet)//先序遍历
{
Bintreepre=t;
stacks;
s.top=0;
printf("输出前序遍历序列:
");
while(pre||s.top>0)
{
if(pre)
{
printf("%c",pre->data);
s.data[s.top++]=pre;//将PRE压栈
pre=pre->lchild;
}
else
{
pre=s.data[--s.top]->rchild;//弹栈,并将弹出来的元素的右孩子赋给PRE
}
}
printf("\n\n");
}
/*******************************************/
/*按照中序非递归遍历二叉树*/
/*******************************************/
voidInorder2(Bintreet)
{
Bintreepre=t;
stacks;
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;
}//if
}//while
printf("\n\n");
}
/*******************************************/
/*按照后序非递归遍历二叉树*/
/*******************************************/
voidPosorder2(Bintreet)
{
stacks;
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");
}
/*******************************************/
/*按照层次遍历二叉树*/
/*******************************************/
voidLevelorder(Bintreet)
{
queueq;
q.data[0]=t;
q.front=0;q.rear=1;
printf("层次遍历二叉树结果:
");
while(q.front{
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");
}
/*******************************************/
/*递归法将二叉树的左右子树互换*/
/*******************************************/
voidExchange1(Bintreet)
{
Bintreetemp;
if(t)
{
Exchange1(t->lchild);
Exchange1(t->rchild);
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
}
}
/*******************************************/
/*非递归法将二叉树的左右子树互换*/
/*******************************************/
voidExchange2(Bintreet)
{
Bintreetemp;
stacks;
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;
}
}
}
intmain()
{
Bintreet;
Creat_Bintree(&t);
Exchange2(t);
Inorder2(t);
return0;
}
/*******************************************/
/*递归法求叶子结点个数*/
/*******************************************/
intLeaves_Num1(Bintreet)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL)
{
return1;
}
returnLeaves_Num1(t->lchild)+Leaves_Num1(t->rchild);
}
return0;
}
/*******************************************/
/*非递归法求叶子结点个数*/
/*******************************************/
intLeaves_Num2(Bintreet)
{
intcount=0;
stacks;
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;
}
}
returncount;
}
intmain()
{
intcount=0;
Bintreet;
Creat_Bintree(&t);
count=Leaves_Num2(t);
printf("该二叉树的叶子结点数为:
%d\n",count);
return0;
}
/**********************************************/
/*求一棵树的高度*/
/**********************************************/
intDepth(Bintreet)
{
intlh,rh;
if(NULL==t)
{
return0;
}
else
{
lh=Depth(t->lchild);
rh=Depth(t->rchild);
return(lh>rh?
lh:
rh)+1;
}
}
intmain()
{
Bintreet;
Creat_Bintree(&t);
printf("树的高度是%d\n",Depth(t));
return0;
}
#include"Bintree.h"
/*******************************************************/
/*已知一课棵二叉树的中序和后序,建立这棵树*/
/*******************************************************/
voidIn_Pos_order(Bintree*t,char*s,char*r)
{
charLa[30],Lb[30],Ra[30],Rb[30];
inti,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{
Rb[i-len-1]=s[i];
}
Rb[i-len-1]='\0';
for(i=len;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;
}
}
intmain()
{
Bintreet;
charin[30]="ABCEFGHD",pos[30]="ABFHGEDC";//测试数据
printf("输入中序遍历序列:
");
scanf("%s",in);
printf("输入后序遍历序列:
");
scanf("%s",in);
In_Pos_order(&t,in,pos);
Preorder2(t);
}
/*******************************************************/
/*判断两棵是否等价*/
/*******************************************************/
intIs_equal(Bintreet1,Bintreet2)
{
intt=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);
}
}
}
}
returnt;
}
intmain()
{
Bintreet1,t2;
Creat_Bintree(&t1);
getchar();
Creat_Bintree(&t2);
if(Is_equal(t1,t2))
{
printf("Yes!
\n");
}
else
{
printf("No!
\n");
}
return0;
}
#include"Bintree.h"
/****************************************************/
/*查找某个信息是否在这棵树中*/
/****************************************************/
Bintreelocale_x(Bintreet,charx)
{
Bintreep;
if(t==NULL)returnNULL;
else
{
if(t->data==x)returnt;
else
{
p=locale_x(t->lchild,x);
if(p)returnp;
else
returnlocale_x(t->rchild,x);
}
}
}
intmain()
{
Bintreeroot,p;
charch;
Creat_Bintree(&root);
getchar();
printf("输入要查找的值:
");
scanf("%c",&ch);
p=locale_x(root,ch);
if(p)printf("YES!
\n");
elseprintf("NO!
\n");
return0;
}
/****************************************************/
/*树的结点个数*/
/****************************************************/
intnum_of_node(Bintreet)
{
if(t==NULL)return0;
elsereturnnum_of_node(t->lchild)+num_of_node(t->rchild)+1;
}
intmain()
{
Bintreeroot,p;
Creat_Bintree(&root);
printf("%d\n",num_of_node(root));
return0;
}
#include"Bintree.h"
/*******************************************************/
/*已知一课棵二叉树的中序和前序序列,建立这棵树*/
/*******************************************************/
voidPre_In_order(Bintree*t,char*s,char*r)
{
charLa[30],Lb[30],Ra[30],Rb[30];
inti,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{
La[i]=s[i+1];
}
La[len]='\0';//左前
for(i=len+1;i{
Rb[i-len-1]=r[i];
}
Rb[i-len-1]='\0';
for(i=len+1;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;
}
}
intmain()
{
Bintreet;
charpre[30]="ABCDEF",in[30]="CBAEDF";//测试数据
printf("输入前序遍历序列:
");
scanf("%s",pre);
printf("输入中序遍历序列:
");
scanf("%s",in);
Pre_In_order(&t,pre,in);
Posorder1(t);
}
#include
#include
typedefstructnode{
intdata;
structnode*lchild,*rchild,*next;
}hufnode;
typedefhufnode*linkhuf;
/****************************************************/
/*huffman树*/
/****************************************************/
linkhufCreat_Node(intn)//创建一单链表
{
linkhufhead,pre,p;
intx;
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;
}
}
returnhead;
}
linkhufinsert(linkhufroot,linkhufs)//将结点S插入到有序Huffmanroot中。
{
linkhufp1,p2;
if(NULL==root)root=s;
else
{
p1=NULL;
p2=root;
while(p2&&p2->datadata)
{
p1=p2;
p2=p2->next;
}
s->next=p2;
if(NULL==p1)
{
root=s;
}
else
{
p1->next=s;
}
}
returnroot;
}
voidPreorder1(linkhuft)
{
if(t!
=NULL)
{
printf("%-6d",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
voidcreathuffman(linkhuf*root)//构造Huffman树。
{
linkhufs,rl,rr;
while(*root&&(*root)->next)
{
rl=*root;
rr=(*root)->next;
*root=rr->next;
s=(l