Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tqh_09 #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions 2017-1/tqh/08/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
8 3 1 6 4 5 7 10 14 19 22 30
8 3 1 6 4 5 7 10 14 13 19 22 30
7 3 1 6 4 5 10 14 13 19 22 30
7 3 1 6 4 10 14 13 19 22 30
7 3 1 6 4 10 14 13 19 22 20 30
7 3 1 4 10 14 13 19 22 20 30
8,3,1,6,4,5,7,10,14,19,22,30
8,3,1,6,4,5,7,10,14,13,19,22,30
7,3,1,6,4,5,10,14,13,19,22,30
7,3,1,6,4,10,14,13,19,22,30
7,3,1,6,4,10,14,13,19,22,20,30
7,3,1,4,10,14,13,19,22,20,30
47 changes: 33 additions & 14 deletions 2017-1/tqh/08/testz.cpp → 2017-1/tqh/08/testz.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX 20

int num[] = {8,10,14,3,1,6,4,7,5,19,22,30};
int searchnum[] = {13,8,5,20,6};
int flag;

enum
{
FALSE,
Expand All @@ -25,11 +26,20 @@ int SearchBST( BiTree T, int key, BiTree f, BiTree *p ) ;
int InsertBST( BiTree *T, int key );
Status Delete(BiTree *p);
int DeleteBST(BiTree *T, int kval);
Status MyPrint(BiTree *T);

Status PreOrderTraverse(BiTree T)
{
if(T){
printf("%d ",T->data);
if(flag == 0)
{
flag = 1;
printf("%d",T->data);
}
else
{
printf(",%d",T->data);
}
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
Expand All @@ -38,7 +48,7 @@ Status PreOrderTraverse(BiTree T)

int SearchBST( BiTree T, int key, BiTree f, BiTree *p )
{
BiTree s;
BiTree s = NULL;
if( !T )
{
*p = f;
Expand Down Expand Up @@ -132,11 +142,11 @@ int DeleteBST(BiTree *T, int kval) {
return FALSE;
}
else {
if(EQ(kval, (*T)->data)) { // �ҵ��ؼ��ֵ���key������Ԫ��
if(kval == (*T)->data) { // �ҵ��ؼ��ֵ���data������Ԫ��
Delete (T);
return TRUE;
}
else if(LT(kval, (*T)->data)) { // ����������������
else if(kval < (*T)->data) { // ����������������
return DeleteBST(&(*T)->lchild, kval);
}
else { // ����������������
Expand All @@ -145,22 +155,31 @@ int DeleteBST(BiTree *T, int kval) {
}
}

int main()
Status MyPrint(BiTree *T)
{
int j;
for(j = 0;j <5;j++)
{
if(!InsertBST(T, searchnum[j])){
DeleteBST(T,searchnum[j]);
}
flag = 0;
PreOrderTraverse(*T);
printf("\n");
}
return OK;
}

int main()
{
BiTree T=NULL;
int j;
for(j = 0;j < 12;j++)
{
InsertBST(&T, num[j]);
}
PreOrderTraverse(T);
for(j = 0;j <5;j++)
{
if(!InsertBST(&T, searchnum[j])){
DeleteBST(&T,searchnum[j]);
}
printf("\n");
PreOrderTraverse(T);
}
printf("\n");
MyPrint(&T);
return 0;
}
10 changes: 10 additions & 0 deletions 2017-1/tqh/09/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
16 17 27 28 33 45 45 48
25 54 79
5 11 24 31 36 45 45 51
28 16 44
1 17 18 21 25 32 52 55
28 14 42
4 16 28 37 39 42 45 56
23 28 51
0 9 18 26 30 35 49 52
28 18 46
186 changes: 186 additions & 0 deletions 2017-1/tqh/09/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef int KeyType;
typedef struct
{
KeyType key;
//int data;
} RecordType;
typedef enum
{
OK,
ERROR
}Status;

Status InsSort(RecordType r[], int length,int *compare,int *move);
Status ShellInsert(RecordType r[], int length, int delta,int *compare,int *move);
Status ShellSort(RecordType r[], int length,int *compare,int *move);
Status BubbleSort(RecordType r[], int length,int *compare,int *move);
int QKPass(RecordType r[], int left, int right,int *compare,int *move);
int QKPass(RecordType r[], int left, int right,int *compare,int *move);
Status SelectSort(RecordType r[], int length,int *compare,int *move);
Status MyPrint(RecordType **r,int *com,int *mov);

Status InsSort(RecordType r[], int length,int *compare,int *move)
{
int i,j;
RecordType temp;
for(i = 1; i < length; i++)
{
temp = r[i]; /* ���������¼��ŵ���ʱ������ */
j = i - 1; /* ���һ����������ı߽�λ�� */
while(++(*compare),temp.key < r[j].key) { /* Ѱ�Ҳ���λ�� */
r[j+1] = r[j];
/* ��С�������򣬴�Ԫ������ */
j = j - 1;
*move += 3;
/* ������Ԫ��������������һ��������һλ��Ԫ�ؼ������бȽ� */
}
r[j+1] = temp;
/* ���������¼���뵽������������� */
}
return OK;
}

Status ShellInsert(RecordType r[], int length, int delta,int *compare,int *move) {
int i,j;
RecordType temp;
for(i = delta; i < length; i++) { /* 1+deltaΪ��һ�������еĵڶ���Ԫ�ص��±� */
if(++(*compare) , r[i].key < r[i-delta].key) {
temp = r[i];
(*move)++;
for(j = i - delta;(++(*compare)), j >= 0 && temp.key < r[j].key; j -= delta) {
r[j+delta] = r[j];
(*move)++;
}
r[j+delta] = temp;
(*move)++;
}
}
return OK;
}

/* �Լ�¼����r��ϣ������ lengthΪ����ij���*/
Status ShellSort(RecordType r[], int length,int *compare,int *move) {
int d = length / 2;
while(d >= 1) {
ShellInsert(r, length, d,compare,move);
d = d / 2;
}
return OK;
}

/* �Լ�¼����r��ð������lengthΪ����ij��� */
Status BubbleSort(RecordType r[], int length,int *compare,int *move) {
int i,j;
int change = 1;
RecordType *temp =(RecordType*)malloc(sizeof(RecordType));
for(i = 0; i < length - 1 && change; ++i) {
change = 0;
for(j = 0; j < length - i - 1; ++j) {
if( ++(*compare) && r[j].key > r[j+1].key) {
*temp = r[j];
r[j] = r[j+1];
r[j+1] = *temp;
++(*move);
change = 1;
}
}
}
return OK;
}

/* �Լ�¼����r[low..high]�ÿ��������㷨�������� */
Status QKSort(RecordType r[],int low, int high,int *compare,int *move) {
int pivot;
if(low < high) {
pivot = QKPass(r, low, high,compare,move);
QKSort(r, low, pivot - 1,compare,move);
QKSort(r, pivot + 1, high,compare,move);
}
return OK;
}

/* �Լ�¼����r �е�r[left]��r[right]���ֽ���һ�����򣬲��õ���׼��λ�ã�ʹ�������Ľ��������֮��ǰ���ļ�¼�Ĺؼ��־���С�ڣ����ڣ��ڻ�׼��¼ */
int QKPass(RecordType r[], int left, int right,int *compare,int *move) {
int low,high;
RecordType *x =(RecordType*)malloc(sizeof(RecordType));
*x = r[left]; /* ѡ���׼��¼*/
(*move)++;
low = left;
high = right;
while(low < high) {
while(low < high && ++(*compare) && r[high].key >= (*x).key) { /* high���ҵ�����С��x.key�ļ�¼ */
high--;
}
r[low] = r[high]; /* �ҵ�С��x.key�ļ�¼������н��� */
(*move)++;
while(low < high && ++(*compare) && r[low].key < (*x).key) { /* low�������Ҳ�С��x.key�ļ�¼ */
low++;
}
r[high] = r[low]; /* �ҵ���С��x.key�ļ�¼���򽻻�*/
(*move)++;
}

r[low] = (*x); /* ����׼��¼���浽low=high��λ�� */
(*move)++;
return low; /*���ػ�׼��¼��λ��*/
}

/* �Լ�¼����r����ѡ������lengthΪ����ij��� */
Status SelectSort(RecordType r[], int length,int *compare,int *move) {
int i,j,k;
RecordType *temp =(RecordType*)malloc(sizeof(RecordType));
for(i = 0; i < length-1; ++i) {
k = i;
for(j = i+1 ; j < length; ++j) {
if(++(*compare) && r[j].key < r[k].key) {
k = j;
}
}
if(k != i) {
*temp = r[i];
r[i] = r[k];
r[k]=*temp;
(*move) += 3;
}
}
return OK;
}
Status MyPrint(RecordType (*r)[8],int *com,int *mov)
{
int i,j;
InsSort(r[0],8,com,mov);
ShellSort(r[1],8,com+1,mov+1);
BubbleSort(r[2],8,com+2,mov+2);
QKSort(r[3],0,7,com+3,mov+3);
SelectSort(r[4],8,com+4,mov+4);
for (i = 0;i < 5 ;i++)
{
for (j = 0;j < 8;j++)
{
printf("%d ",r[i][j].key);
}
printf("\n%d %d %d\n",com[i],mov[i],com[i]+mov[i]);
}
return OK;
}
int main()
{
RecordType (*r)[8] = (RecordType(*)[8])malloc(5*sizeof(RecordType[8]));
int i,j;
int *com = (int*)calloc(5,sizeof(int));
int *mov = (int*)calloc(5,sizeof(int));
srand( time(NULL) );
for (i = 0;i < 5 ;i++)
{
for(j = 0;j < 8;j++)
{
r[i][j].key = rand()%57;
}
}
MyPrint(r,com,mov);
return 0;
}