From f5dbba7ed64693900b8b3afcb15abc4115c2fc49 Mon Sep 17 00:00:00 2001 From: Han Jiang Date: Tue, 6 Jun 2017 21:41:50 +0800 Subject: [PATCH] tqh_09 --- 2017-1/tqh/08/BSTOutput.txt | 12 +- 2017-1/tqh/08/{testz.cpp => testz.c} | 47 +++++-- 2017-1/tqh/09/BSTOutput.txt | 10 ++ 2017-1/tqh/09/test.c | 186 +++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 20 deletions(-) rename 2017-1/tqh/08/{testz.cpp => testz.c} (86%) create mode 100644 2017-1/tqh/09/BSTOutput.txt create mode 100644 2017-1/tqh/09/test.c diff --git a/2017-1/tqh/08/BSTOutput.txt b/2017-1/tqh/08/BSTOutput.txt index 08eb1402..9101df63 100644 --- a/2017-1/tqh/08/BSTOutput.txt +++ b/2017-1/tqh/08/BSTOutput.txt @@ -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 \ No newline at end of file +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 diff --git a/2017-1/tqh/08/testz.cpp b/2017-1/tqh/08/testz.c similarity index 86% rename from 2017-1/tqh/08/testz.cpp rename to 2017-1/tqh/08/testz.c index fbd6a326..c22d68f6 100644 --- a/2017-1/tqh/08/testz.cpp +++ b/2017-1/tqh/08/testz.c @@ -1,9 +1,10 @@ #include #include -#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, @@ -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); } @@ -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; @@ -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 { // 继续在右子树中进行查找 @@ -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; } diff --git a/2017-1/tqh/09/BSTOutput.txt b/2017-1/tqh/09/BSTOutput.txt new file mode 100644 index 00000000..e61f353c --- /dev/null +++ b/2017-1/tqh/09/BSTOutput.txt @@ -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 diff --git a/2017-1/tqh/09/test.c b/2017-1/tqh/09/test.c new file mode 100644 index 00000000..e550dcc6 --- /dev/null +++ b/2017-1/tqh/09/test.c @@ -0,0 +1,186 @@ +#include +#include +#include + +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为数组的长度*/ +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为数组的长度 */ +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为数组的长度 */ +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; +} \ No newline at end of file