二叉树的基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
} BTNode;
void CreateBTree(BTNode * &b,char *str)
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while (ch!='\0')
{
switch(ch)
{
case '(':top++;St[top]=p;k=1; break;
case ')':top--;break;
case ',':k=2; break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if (b==NULL)
b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode *&b)
{ if (b!=NULL)
{ DestroyBTree(b->lchild);
DestroyBTree(b->rchild);
free(b);
}
}
BTNode *FindNode(BTNode *b,ElemType x)
{
BTNode *p;
if (b==NULL)
return NULL;
else if (b->data==x)
return b;
else
{
p=FindNode(b->lchild,x);
if (p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}
}
BTNode *LchildNode(BTNode *p)
{
return p->lchild;
}
BTNode *RchildNode(BTNode *p)
{
return p->rchild;
}
int BTHeight(BTNode *b)
{
int lchildh,rchildh;
if (b==NULL) return(0);
else
{
lchildh=BTHeight(b->lchild);
rchildh=BTHeight(b->rchild);
return (lchildh>rchildh)? (lchildh+1):(rchildh+1);
}
}
void DispBTree(BTNode *b)
{
if (b!=NULL)
{ printf("%c",b->data);
if (b->lchild!=NULL || b->rchild!=NULL)
{ printf("(");
DispBTree(b->lchild);
if (b->rchild!=NULL) printf(",");
DispBTree(b->rchild);
printf(")");
}
}
}
构造二叉树的算法
#include "btree.cpp"
BTNode *CreateBT1(char *pre,char *in,int n)
{
BTNode *s;
char *p;
int k;
if (n<=0) return NULL;
s=(BTNode *)malloc(sizeof(BTNode));
s->data=*pre;
for (p=in;p<in+n;p++)
if (*p==*pre)
break;
k=p-in;
s->lchild=CreateBT1(pre+1,in,k);
s->rchild=CreateBT1(pre+k+1,p+1,n-k-1);
return s;
}
BTNode *CreateBT2(char *post,char *in,int n)
{
BTNode *s;
char r,*p;
int k;
if (n<=0) return NULL;
r=*(post+n-1);
s=(BTNode *)malloc(sizeof(BTNode));
s->data=r;
for (p=in;p<in+n;p++)
if (*p==r)
break;
k=p-in;
s->lchild=CreateBT2(post,in,k);
s->rchild=CreateBT2(post+k,p+1,n-k-1);
return s;
}
int main()
{
ElemType pre[]="ABDGCEF",in[]="DGBAECF",post[]="GDBEFCA";
BTNode *b1,*b2;
b1=CreateBT1(pre,in,7);
printf("b1:");DispBTree(b1);printf("\n");
b2=CreateBT2(post,in,7);
printf("b2:");DispBTree(b2);printf("\n");
DestroyBTree(b1);
DestroyBTree(b2);
return 1;
}
层次遍历算法
#include "btree.cpp"
#define MaxSize 100
typedef struct
{ BTNode *data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,BTNode *e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,BTNode *&e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
void LevelOrder(BTNode *b)
{
BTNode *p;
SqQueue *qu;
InitQueue(qu);
enQueue(qu,b);
while (!QueueEmpty(qu))
{
deQueue(qu,p);
printf("%c ",p->data);
if (p->lchild!=NULL)
enQueue(qu,p->lchild);
if (p->rchild!=NULL)
enQueue(qu,p->rchild);
}
}
int main()
{
BTNode *b;
CreateBTree(b,"A(B(D(,G)),C(E,F))");
printf("b:");DispBTree(b);printf("\n");
printf("层次遍历序列:");LevelOrder(b);printf("\n");
DestroyBTree(b);
return 1;
}
先序、中序和后序递归遍历算法
#include "btree.cpp"
void PreOrder(BTNode *b)
{
if (b!=NULL)
{
printf("%c ",b->data);
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
void InOrder(BTNode *b)
{
if (b!=NULL)
{
InOrder(b->lchild);
printf("%c ",b->data);
InOrder(b->rchild);
}
}
void PostOrder(BTNode *b)
{
if (b!=NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c ",b->data);
}
}
int main()
{
BTNode *b;
CreateBTree(b,"A(B(D(,G)),C(E,F))");
printf("b:");DispBTree(b);printf("\n");
printf("先序遍历序列:");PreOrder(b);printf("\n");
printf("中序遍历序列:");InOrder(b);printf("\n");
printf("后序遍历序列:");PostOrder(b);printf("\n");
DestroyBTree(b);
return 1;
}
先序、中序和后序非递归遍历算法
#include "btree.cpp"
typedef struct
{ BTNode *data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *&s)
{ s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,BTNode *e)
{ if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,BTNode *&e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,BTNode *&e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
void PreOrder1(BTNode *b)
{
BTNode *p;
SqStack *st;
InitStack(st);
Push(st,b);
while (!StackEmpty(st))
{
Pop(st,p);
printf("%c ",p->data);
if (p->rchild!=NULL)
Push(st,p->rchild);
if (p->lchild!=NULL)
Push(st,p->lchild);
}
printf("\n");
DestroyStack(st);
}
void PreOrder2(BTNode *b)
{
BTNode *p;
SqStack *st;
InitStack(st);
p=b;
while (!StackEmpty(st) || p!=NULL)
{
while (p!=NULL)
{
printf("%c ",p->data);
Push(st,p);
p=p->lchild;
}
if (!StackEmpty(st))
{
Pop(st,p);
p=p->rchild;
}
}
printf("\n");
DestroyStack(st);
}
void InOrder1(BTNode *b)
{
BTNode *p;
SqStack *st;
InitStack(st);
if (b!=NULL)
{
p=b;
while (!StackEmpty(st) || p!=NULL)
{
while (p!=NULL)
{
Push(st,p);
p=p->lchild;
}
if (!StackEmpty(st))
{
Pop(st,p);
printf("%c ",p->data);
p=p->rchild;
}
}
printf("\n");
}
DestroyStack(st);
}
void PostOrder1(BTNode *b)
{
BTNode *p,*r;
bool flag;
SqStack *st;
InitStack(st);
p=b;
do
{
while (p!=NULL)
{
Push(st,p);
p=p->lchild;
}
r=NULL;
flag=true;
while (!StackEmpty(st) && flag)
{
GetTop(st,p);
if (p->rchild==r)
{
printf("%c ",p->data);
Pop(st,p);
r=p;
}
else
{
p=p->rchild;
flag=false;
}
}
} while (!StackEmpty(st));
printf("\n");
DestroyStack(st);
}
int main()
{
BTNode *b;
CreateBTree(b,"A(B(D(,G)),C(E,F))");
printf("b:");DispBTree(b);printf("\n");
printf("先序遍历序列1:");PreOrder1(b);
printf("先序遍历序列2:");PreOrder2(b);
printf("中序遍历序列:");InOrder1(b);
printf("后序遍历序列:");PostOrder1(b);
DestroyBTree(b);
return 1;
}
中序线索二叉树的算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
int ltag,rtag;
struct node *lchild;
struct node *rchild;
} TBTNode;
void CreateTBTree(TBTNode * &b,char *str)
{
TBTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while (ch!='\0')
{
switch(ch)
{
case '(':top++;St[top]=p;k=1; break;
case ')':top--;break;
case ',':k=2; break;
default:p=(TBTNode *)malloc(sizeof(TBTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if (b==NULL)
b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void DispTBTree(TBTNode *b)
{
if (b!=NULL)
{
printf("%c",b->data);
if (b->lchild!=NULL || b->rchild!=NULL)
{
printf("(");
DispTBTree(b->lchild);
if (b->rchild!=NULL) printf(",");
DispTBTree(b->rchild);
printf(")");
}
}
}
TBTNode *pre;
void Thread(TBTNode *&p)
{
if (p!=NULL)
{
Thread(p->lchild);
if (p->lchild==NULL)
{
p->lchild=pre;
p->ltag=1;
}
else p->ltag=0;
if (pre->rchild==NULL)
{
pre->rchild=p;
pre->rtag=1;
}
else pre->rtag=0;
pre=p;
Thread(p->rchild);
}
}
TBTNode *CreateThread(TBTNode *b)
{
TBTNode *root;
root=(TBTNode *)malloc(sizeof(TBTNode));
root->ltag=0;root->rtag=1;
root->rchild=b;
if (b==NULL)
root->lchild=root;
else
{
root->lchild=b;
pre=root;
Thread(b);
pre->rchild=root;
pre->rtag=1;
root->rchild=pre;
}
return root;
}
void DestroyTBTree1(TBTNode *&b)
{ if (b->ltag==0)
DestroyTBTree1(b->lchild);
if (b->rtag==0)
DestroyTBTree1(b->rchild);
free(b);
}
void DestroyTBTree(TBTNode *&tb)
{
DestroyTBTree1(tb->lchild);
free(tb);
}
void ThInOrder(TBTNode *tb)
{
TBTNode *p=tb->lchild;
while (p!=tb)
{
while (p->ltag==0) p=p->lchild;
printf("%c ",p->data);
while (p->rtag==1 && p->rchild!=tb)
{
p=p->rchild;
printf("%c ",p->data);
}
p=p->rchild;
}
}
int main()
{
TBTNode *b,*tb;
CreateTBTree(b,"A(B(D(,G)),C(E,F))");
printf(" 二叉树:");DispTBTree(b);printf("\n");
tb=CreateThread(b);
printf(" 线索中序序列:");ThInOrder(tb);printf("\n");
DestroyTBTree(tb);
return 1;
}
并查集的算法
#include <stdio.h>
#define MaxSize 100
#define N 10
#define M 7
#define Q 3
typedef struct node
{
int data;
int rank;
int parent;
} UFSTree;
void MAKE_SET(UFSTree t[],int n)
{
int i;
for (i=1;i<=n;i++)
{
t[i].data=i;
t[i].rank=0;
t[i].parent=i;
}
}
int FIND_SET(UFSTree t[],int x)
{
if (x!=t[x].parent)
return(FIND_SET(t,t[x].parent));
else
return(x);
}
void UNION(UFSTree t[],int x,int y)
{
x=FIND_SET(t,x);
y=FIND_SET(t,y);
if (t[x].rank>t[y].rank)
t[y].parent=x;
else
{
t[x].parent=y;
if (t[x].rank==t[y].rank)
t[y].rank++;
}
}
int main()
{
int i,x,y;
UFSTree t[MaxSize];
int rel[M][2]={{2,4},{5,7},{1,3},{8,9},{1,2},{5,6},{2,3}};
int ans[Q][2]={{3,4},{7,10},{8,9}};
MAKE_SET(t,N);
for (i=0;i<M;i++)
UNION(t,rel[i][0],rel[i][1]);
printf("data rank parent\n");
for (i=1;i<=N;i++)
printf("%4d%5d%6d\n",t[i].data,t[i].rank,t[i].parent);
printf("\n");
printf("各询问的结果:\n");
for (i=0;i<Q;i++)
{
x=FIND_SET(t,ans[i][0]);
y=FIND_SET(t,ans[i][1]);
if (x==y)
printf(" (%d,%d):Yes\n",ans[i][0],ans[i][1]);
else
printf(" (%d,%d):No\n",ans[i][0],ans[i][1]);
}
return 1;
}
构造哈夫曼树和哈夫曼编码的算法
#include <stdio.h>
#include <string.h>
#define N 50
#define M 2*N-1
typedef struct
{
char data[5];
double weight;
int parent;
int lchild;
int rchild;
} HTNode;
typedef struct
{
char cd[N];
int start;
} HCode;
void CreateHT(HTNode ht[],int n0)
{ int i,k,lnode,rnode;
double min1,min2;
for (i=0;i<2*n0-1;i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for (i=n0;i<=2*n0-2;i++)
{ min1=min2=32767;
lnode=rnode=-1;
for (k=0;k<=i-1;k++)
if (ht[k].parent==-1)
{ if (ht[k].weight<min1)
{ min2=min1;rnode=lnode;
min1=ht[k].weight;lnode=k;
}
else if (ht[k].weight<min2)
{ min2=ht[k].weight;rnode=k; }
}
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;ht[i].rchild=rnode;
ht[lnode].parent=i;ht[rnode].parent=i;
}
}
void CreateHCode(HTNode ht[],HCode hcd[],int n0)
{ int i,f,c;
HCode hc;
for (i=0;i<n0;i++)
{ hc.start=n0;c=i;
f=ht[i].parent;
while (f!=-1)
{ if (ht[f].lchild==c)
hc.cd[hc.start--]='0';
else
hc.cd[hc.start--]='1';
c=f;f=ht[f].parent;
}
hc.start++;
hcd[i]=hc;
}
}
void DispHCode(HTNode ht[],HCode hcd[],int n0)
{
int i,k;
double sum=0,m=0;
int j;
printf(" 输出哈夫曼编码:\n");
for (i=0;i<n0;i++)
{
j=0;
printf(" %s:\t",ht[i].data);
for (k=hcd[i].start;k<=n0;k++)
{
printf("%c",hcd[i].cd[k]);
j++;
}
m+=ht[i].weight;
sum+=ht[i].weight*j;
printf("\n");
}
printf("\n 平均长度=%g\n",1.0*sum/m);
}
int main()
{
int n=8,i;
char *str[]={"a","b","c","d","e","f","g","h"};
double fnum[]={0.07,0.19,0.02,0.06,0.32,0.03,0.21,0.1};
HTNode ht[M];
HCode hcd[N];
for (i=0;i<n;i++)
{
strcpy(ht[i].data,str[i]);
ht[i].weight=fnum[i];
}
printf("\n");
CreateHT(ht,n);
CreateHCode(ht,hcd,n);
DispHCode(ht,hcd,n);
printf("\n");
return 1;
}