//二叉树
#include
#include
struct binode
{
int data;
struct binode* left;
struct binode* right;
};
typedef struct binode* biqnode;
struct queue
{
biqnode data;
struct queue* next;
};
typedef struct queue* pqueue;
void LevelOrder(biqnode bi);
biqnode TreeCreate(void);
void PrintLeaf(biqnode bi);
void SwapChild(biqnode bi);
void main(void)
{
biqnode tree;
printf("Creating tree:\n");
tree=TreeCreate();
system("cls");
printf("\n\nOutput tree by level:\n");
LevelOrder(tree);
system("pause");
printf("\n\nOutput leaf node:\n");
PrintLeaf(tree);
system("pause");
printf("\n\nSwap left and left child:\n");
SwapChild(tree);
printf("\nOutput tree by level:\n");
LevelOrder(tree);
system("pause");
}
void LevelOrder(biqnode bi)
{
pqueue q,qrear,qhead,t;/*申请队列*/
if(bi==NULL)/*判断树是否为空*/
{
printf("Empty Tree...\n");
return;
}
/*初始化*/
q=(pqueue)malloc(sizeof(struct queue));
qhead=q;
qrear=q;
/*把树根入队列*/
q->data=bi;
q->next=NULL;
while(1)/*把数按层存入队列*/
{
if(q->data->left)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->left;
qrear->next=NULL;
}
if(q->data->right)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->right;
qrear->next=NULL;
}
if(q->next)
q=q->next;
else
break;
}
/*按队列输出*/
q=qhead;
while(1)
{
printf("%d ",q->data->data);
if(q->next)
{
t=q;
/*释放队列*/
q=q->next;
free(t);
}
else
break;
}
}
biqnode TreeCreate(void)
{
int d;
biqnode bi;
printf("Data:(int put -1 to ignore)");
scanf("%d",&d);
if(d==-1)
return NULL;
bi=(biqnode)malloc(sizeof(struct binode));
bi->data=d;
printf("Left child of %d: ",d);
bi->left=TreeCreate();
printf("Right child of %d: ",d);
bi->right=TreeCreate();
return bi;
}
void PrintLeaf(biqnode bi)
{
pqueue q,qrear,qhead,t;/*申请队列*/
if(bi==NULL)/*判断树是否为空*/
{
printf("Empty Tree...\n");
return;
}
/*初始化*/
q=(pqueue)malloc(sizeof(struct queue));
qhead=q;
qrear=q;
/*把树根入队列*/
q->data=bi;
q->next=NULL;
while(1)/*把数按层存入队列*/
{
if(q->data->left)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->left;
qrear->next=NULL;
}
if(q->data->right)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->right;
qrear->next=NULL;
}
if(q->next)
q=q->next;
else
break;
}
/*按队列输出*/
q=qhead;
while(1)
{
if(!q->data->left&&!q->data->right)/*判断是否叶子节点*/
printf("%d ",q->data->data);
if(q->next)
{
t=q;
/*释放队列*/
q=q->next;
free(t);
}
else
break;
}
}
void SwapChild(biqnode bi)
{
pqueue q,qrear,qhead,t;/*申请队列*/
biqnode t2;
if(bi==NULL)/*判断树是否为空*/
{
printf("Empty Tree...\n");
return;
}
/*初始化*/
q=(pqueue)malloc(sizeof(struct queue));
qhead=q;
qrear=q;
/*把树根入队列*/
q->data=bi;
q->next=NULL;
while(1)/*把数按层存入队列*/
{
if(q->data->left)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->left;
qrear->next=NULL;
}
if(q->data->right)
{
t=(pqueue)malloc(sizeof(struct queue));
qrear->next=t;
qrear=t;
qrear->data=q->data->right;
qrear->next=NULL;
}
if(q->next)
q=q->next;
else
break;
}
/*按队列交换*/
q=qhead;
while(1)
{
t2=q->data->left;
q->data->left=q->data->right;
q->data->right=t2;
if(q->next)
{
t=q;
/*释放队列*/
q=q->next;
free(t);
}
else
break;
}
}
//排序
#include
#include
#include
#include
#define UINT32 unsigned long
UINT32 rand32( void );
void bubble(UINT32 *p,UINT32 size,UINT32 *compare,UINT32 *move);
void fast(UINT32 *p,UINT32 size,UINT32 *compare,UINT32 *move);
UINT32 *randnum(UINT32 size);
void output(UINT32 *p,UINT32 size);
void main(void)
{
UINT32 n,*p1,*p2,c,m;
time_t t1,t2;
printf("Input the size of array:");
scanf("%lu",&n);
if(n>=5000)
{
printf("Array size is too large, memory allocate may fail...\n");
system("pause");
}
p1=randnum(n);
p2=(UINT32*)malloc(n*sizeof(UINT32));
memmove(p2,p1,sizeof(UINT32)*n);
/*output(p1,n);*/
printf("\nSorting...\n");
c=0;
m=0;
t1=time(NULL);
bubble(p1,n,&c,&m);
t2=time(NULL);
/*output(p1,n);*/
printf("\nBubble Method:\n\tTime:%.0lf sec, Compare: %lu, Move: %lu\n",difftime(t2,t1),c,m);
system("pause");
printf("\nSorting...\n");
c=0;
m=0;
t1=time(NULL);
fast(p2,n,&c,&m);
t2=time(NULL);
/*output(p2,n);*/
printf("\Fast Method:\n\tTime:%.0lf sec, Compare: %lu, Move: %lu\n",difftime(t2,t1),c,m);
system("pause");
}
void output(UINT32 *p,UINT32 size)
{
UINT32 i;
for(i=0;i
printf("\n\n");
}
UINT32 rand32( void )
{
static char b = 0;
if(!b)
{
srand(time(NULL));
b++;
}
return ( (UINT32)(rand() << 17) + (UINT32)(rand() << 2) + (UINT32)rand() % 4);
}
UINT32 *randnum(UINT32 size)
{
UINT32 *p=NULL,i;
if(!size)
return NULL;
p=(UINT32*)malloc(size*sizeof(UINT32));
if(!p)
printf("\nError!\nMemory allocate Failed!!!\n");
for(i=0;i
return p;
}
void bubble(UINT32 *p,UINT32 size,UINT32 *compare,UINT32 *move)
{
UINT32 i,j,t;
if(!size||!p)
return;
for(i=0;i
(*compare)++;
if(p[i]>p[j])
{
(*move)++;
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
void fast(UINT32 *p,UINT32 size,UINT32 *compare,UINT32 *move)
{
UINT32 x,i,j,t;
if(!size||!p)
return;
i=0;
j=size-1;
x=0;
while(1)
{
while(p[j]>=p[x]&&i!=j)
{
j--;
(*compare)++;
}
if(i==j)
break;
(*move)++;
t=p[x];
p[x]=p[j];
p[j]=t;
x=j;
while(p[i]<=p[x]&&i!=j)
{
i++;
(*compare)++;
}
if(i==j)
break;
(*move)++;
t=p[x];
p[x]=p[i];
p[i]=t;
x=i;
}
if(x>1)
fast(p,x,compare,move);
if(size>2+x)
fast(p+x+1,size-x-1,compare,move);
}