diff --git "a/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" "b/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" new file mode 100644 index 00000000..2342fec4 Binary files /dev/null and "b/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" differ diff --git "a/2017-1/ywy/Hash\350\241\250/Hash.c" "b/2017-1/ywy/Hash\350\241\250/Hash.c" new file mode 100644 index 00000000..6910aeeb --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/Hash.c" @@ -0,0 +1,167 @@ +#include"Hash.h" +bool SuS(int n) +{ + int i; + if(n==2) + { + return TRUE; + } + for (i = 2; i < n; i++) + { + if(n%i==0) + { + return FALSE; + } + } + return TRUE; +} +Status InitHashTable(HashTable*H,int size) +{ + int i; + ElemType temp; + H->count = 0; + H->maxsize = size; + H->elem = (ElemType*)malloc(sizeof(ElemType)*size); + if (!(H->elem)) + { + return error; + } + for (i = 0; i < size; i++) + { + H->elem[i].key = UNKEY; + H->elem[i].val = UNVAL; + } + srand((unsigned)time(NULL)); + for (i = 0; H->count%d\n", H.elem[*add].key, H.elem[*add].val); + (*add)++; + (*count)++; + } + else + break; + } + + if (Equal(key, H.elem[*add].key)) + { + return SUCCESS; + } + else + { + return UNSUCCESS; + } +} +Status InsertHashTable(HashTable*H, ElemType e) +{ + int add, count; + if (SearchHashTable(*H, e.key, &add, &count) == SUCCESS) + { + return DUPLICATE; + } + else { + if (add == H->maxsize || count > H->maxsize / 2) + { + ReHash(H); + } + H->elem[add] = e; + H->count++; + printf("插入[%d]:%d->%d", add, H->elem[add].key, H->elem[add].val); + printf("碰撞次数:%d\n", count); + return ok; + } + +} +Status ReHash(HashTable *H) +{ + int i, n; + HashTable temp; + if (Empty(*H)) + { + return error; + } + n = 2 * H->maxsize; + while (!SuS(n)) + { + n++; + } + temp.maxsize = n; + temp.count = H->count; + temp.elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) + { + temp.elem[i].key = UNKEY; + temp.elem[i].val = UNVAL; + } + + for (i = 0; i < H->maxsize; i++) { + temp.elem[i].key = H->elem[i].key; + temp.elem[i].val = H->elem[i].val; + } + + H->maxsize = temp.maxsize; + + H->count = temp.count; + + H->elem = (ElemType *)malloc(sizeof(ElemType)*n); + + for (i = 0; i < n; i++) + { + H->elem[i].key = temp.elem[i].key; + H->elem[i].val = temp.elem[i].val; + } + + free(temp.elem); + return ok; +} +Status PrintHash(HashTable H) +{ + int i; + if(Empty(H)) + { + return error; + } + printf("print hash\n"); + for (i = 0; i < H.maxsize; i++) + { + printf("[%d]: %d->%d\n", i, H.elem[i].key, H.elem[i].val); + } + return ok; +} + + + + + diff --git "a/2017-1/ywy/Hash\350\241\250/Hash.h" "b/2017-1/ywy/Hash\350\241\250/Hash.h" new file mode 100644 index 00000000..973542ca --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/Hash.h" @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include +#include +#define SUCCESS 1 +#define UNSUCCESS 0 +#define DUPLICATE -1 +#define UNKEY -1 +#define UNVAL 0 +//#define HASHSIZE 30 +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType +{ + KeyType key;//关键字 + ValueType val;//值 +#ifdef CHAINED_HASH + struct _ElemType*next; +#endif +}ElemType; +typedef struct +{ + ElemType*elem;//存储哈希表元素 + int count; + int maxsize; +}HashTable; +typedef enum +{ + ok, + error +}Status; + +typedef enum +{ + FALSE, + TRUE +}bool; +Status PrintHash(HashTable H); +Status InitHashTable(HashTable*H,int size); +Status InsertHashTable(HashTable*H, ElemType e); +bool Empty(HashTable H); +bool Equal(KeyType a, KeyType b); +bool SuS(int n); +int SearchHashTable(HashTable H, KeyType key, int*add, int*count); +Status ReHash(HashTable *H); \ No newline at end of file diff --git "a/2017-1/ywy/Hash\350\241\250/test.c" "b/2017-1/ywy/Hash\350\241\250/test.c" new file mode 100644 index 00000000..c11d59eb --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/test.c" @@ -0,0 +1,30 @@ +#include"Hash.h" +int main() + +{ + int size; + int i; + KeyType key; + HashTable H; + size = 11;// + srand((unsigned)time(NULL)); + + InitHashTable(&H, size); + PrintHash(H); + int add = 0; + int count = 0; + for (i = 0; i < H.count; i++) + { + key = rand() % size; + if(SearchHashTable(H,key,&add,&count)==SUCCESS) + { + printf("find %d ", key); + printf("address %d: \n", add); + } + else + { + printf("not find %d: \n", key); + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/ywy/\345\233\276/2017-05-24.png" "b/2017-1/ywy/\345\233\276/2017-05-24.png" new file mode 100644 index 00000000..fe7c1bfd Binary files /dev/null and "b/2017-1/ywy/\345\233\276/2017-05-24.png" differ diff --git "a/2017-1/ywy/\345\233\276/TU.c" "b/2017-1/ywy/\345\233\276/TU.c" new file mode 100644 index 00000000..c4145190 --- /dev/null +++ "b/2017-1/ywy/\345\233\276/TU.c" @@ -0,0 +1,293 @@ +#include +#include +#define INFINITY -1 +#define MXAVERTEX_NUM 10//最大顶点个数 +#define VRType int +#define VertexType int +#define InfoType int +#define QElemType int +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef enum +{ + true, + false +}bool; +//-------图的邻接矩阵存储-----------// +typedef struct Arcell +{ + VRType adj;//顶点关系,1或0 + InfoType *info;//弧相关信息的指针 +}Arcell, AdjMatrix[MXAVERTEX_NUM][MXAVERTEX_NUM]; +typedef struct +{ + VertexType vexs[MXAVERTEX_NUM]; //顶点向量 + AdjMatrix arcs; //邻接矩阵 + int vexnum, arcnum; //图的当前顶点和弧数 +}MGraph; +//--------队列---------// +typedef struct QNode +{ + QElemType data; + struct QNode *next; + struct QNode *pre; +}QNode, *QueuePtr; +typedef struct LinkQueue +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; +/*=========队列的基本操作=========*/ +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return ERROR; + } + Q->front->next = Q->rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue*Q, QElemType e) +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + p->pre = Q->front; + Q->rear->next = p; + Q->rear = p; + return OK; + } +} + +Status DeQueue(LinkQueue*Q, QElemType*e) +{ + if (Q->front == Q->rear) + { + return ERROR; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +} + +bool QueueEmpty(LinkQueue*Q) +{ + if (Q->front == Q->rear) + { + return true; + } + else + { + return false; + } +} + +Status DestroyQueue(LinkQueue*Q) +{ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +/*=========图的基本操作=========*/ + +int LocateVex(MGraph *G, int v1)//返回顶点v1的位置 +{ + int i; + int n = -1; + + for (i = 0; i <= G->vexnum; i++) + { + if (G->vexs[i] == v1) + { + n= i; + break; + } + } + if (n != -1) + { + return n; + } + return 0; +} +Status InsertArc(MGraph *G, int v1, int v2)//v1、v2间添加弧 +{ + int i, j; + i = LocateVex(G, v1); + j= LocateVex(G, v2); + if (i != 0 && j != 0) + { + G->arcs[i][j].adj = 1;//两点之间有连线,弧值为1; + G->arcs[j][i] = G->arcs[i][j]; + return OK; + } + else + { + return ERROR; + } +} +//构建图 +Status CreateUDN(MGraph *G) +{ + int i; + int j; + //根据用例直接赋值。 + G->vexnum = 9; + G->arcnum = 12; + for (i = 1; i <= G->vexnum; i++) + { + G->vexs[i] = i;//构建顶点向量; + } + for (i = 0; i <= G->vexnum; i++)//初始化邻接矩阵; + { + for (j = 0; j <= G->vexnum; j++) + { + G->arcs[i][j].adj = INFINITY; + G->arcs[i][j].info = NULL; + } + } + //构建邻接矩阵; + InsertArc(G, 1, 2); + InsertArc(G, 1, 3); + InsertArc(G, 1, 4); + InsertArc(G, 1, 7); + InsertArc(G, 2, 3); + InsertArc(G, 4, 5); + InsertArc(G, 4, 6); + InsertArc(G, 5, 6); + InsertArc(G, 6, 8); + InsertArc(G, 7, 8); + InsertArc(G, 7, 9); + InsertArc(G, 8, 9); + + return OK; +} + +//找出第一个邻接点 +int FirstAdjVex(MGraph *G, int u) +{ + int i; + for (i = 1; i <= G->vexnum; i++) + { + if (G->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} + +//找出下一个邻接点 +int NextAdjvex(MGraph *G, int u, int w) +{ + int i; + for (i = w + 1; i <= G->vexnum; i++) + { + if (G->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} +//广度优先遍历图,求两点a,b间的最短路径; +Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b) +{ + + int v; + int u = 0; + int w = 0; + int m = 0; + int n = 0; + bool visited[MXAVERTEX_NUM]; + for (v = 1; v <= G->vexnum; v++) + { + visited[v] = false; //标记数组,标记图中已访问的点 + } + + EnQueue(Q, a); //a先入队列; + while (QueueEmpty(Q) != true) + { + DeQueue(Q, &u); + for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjvex(G, u, w)) + { + if (visited[w] == false)//判断w是否已经访问过 + { + visited[w] = true; + EnQueue(Q, w); + } + if (w == b) + { + break; + } + } + if (w == b) + { + break; + } + } + return OK; +} + +Status print(LinkQueue *Q, int a) +{ + if (Q->rear->data == a) + { + printf("%d->%d\n", a, a); + return OK; + } + + int i = 0; + int j; + int num[MXAVERTEX_NUM] = { 0 }; + while (Q->rear->data != a)//倒序进入数组 + { + num[i] = Q->rear->data; + Q->rear = Q->rear->pre; + i++; + } + printf("%d", a); + for (j = i - 1; j >= 0; j--) + { + printf("->%d", num[j]); + } + + + printf("\n"); + return OK; +} + +int main() +{ + int i, j; + MGraph Graph; + CreateUDN(&Graph); + for (i = 1; i <= Graph.vexnum; i++) + { + for (j = 1; j <= Graph.vexnum; j++) + { + LinkQueue Q; + InitQueue(&Q); + printf("%d<->%d: ", i, j); + BFSTraverse(&Graph, &Q, i, j); + print(&Q, i); + DestroyQueue(&Q); + + } + } +} \ No newline at end of file diff --git "a/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" "b/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" new file mode 100644 index 00000000..ffd77b64 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217/Sort.c" "b/2017-1/ywy/\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..3e9560de --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,179 @@ +#include"Sort.h" +void swap(SqList*L, int i, int j) +{ + RedType temp = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = temp; +} + +void ShellSort(SqList*L) +{ + int i, j; + int count1 = 0, count2 = 0; + int rise = L->length; + do + { + rise = rise / 5 + 1; + for (i = rise + 1; i <= L->length; i++) + { + count1++; + if (LT(L->r[i].key, L->r[i - rise].key)) + { + L->r[0] = L->r[i]; + count2++; + for (j = i - rise; LT(L->r[0].key, L->r[j].key) && j > 0; j = j - rise) + { + count1++; + L->r[j + rise] = L->r[j]; + count2++; + } + count1++; + L->r[j + rise] = L->r[0]; + count2++; + } + } + } while (rise > 1); + printf("希尔排序为: "); + for (i = 2; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +void InsertSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 2; i <= L->length; i++) + { + ++count1; + if (LT(L->r[i].key, L->r[i - 1].key)) + { + L->r[0] = L->r[i]; + count2++; + //L->r[i] = L->r[i - 1]; + for (j = i - 1; LT(L->r[0].key, L->r[j].key); --j) + { + count1++; + count2++; + L->r[j + 1] = L->r[j]; + } + count1++; + L->r[j + 1] = L->r[0]; + count2++; + } + } + printf("插入排序后为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +void BubbleSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + for (j = i+1; j < L->length; j++) + { + count1++; + if ( LT(L->r[j].key, L->r[i].key)) + { + swap(L,i,j); + count2 += 3; + } + } + } + printf("冒泡排序后为:"); + for (i = 1; ilength; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +int Partition(SqList*L, int low, int high,int*count1,int*count2) +{ + int pivotkey; + //L->r[0] = L->r[low]; + pivotkey = L->r[low].key; + while (low < high) + { + while (L->r[high].key >= pivotkey && low < high) + { + (*count1)++; + high--; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + while (L->r[low].key <= pivotkey && low < high) + { + (*count1)++; + low++; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + } + return low; +} + +void QSort(SqList*L, int low, int high,int*count1,int*count2) +{ + int pivot; + if (low < high) + { + pivot = Partition(L, low, high, count1, count2); + QSort(L, low, pivot - 1, count1, count2); + QSort(L, pivot + 1, high, count1,count2); + } +} +void QuickSort(SqList*L) +{ + int i; + int count1 = 0, count2 = 0; + QSort(L, 1, L->length, &count1, &count2); + printf("快速排序后为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); +} +void SelectSort(SqList*L) +{ + int i, j,min; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + min = i; + for (j = i + 1; j <= L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[min].key)) + { + min = j; + } + } + if (i != min) + { + swap(L, i, min); + count2 += 3; + } + } + printf("\n简单选择排序为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} diff --git "a/2017-1/ywy/\346\216\222\345\272\217/Sort.h" "b/2017-1/ywy/\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..e2b9681d --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +typedef int KeyType; +typedef int InfoType; +#define maxsize 20 +#define len 10 +#define LT(a,b) ((a)<(b)) +typedef struct +{ + KeyType key; + InfoType otherinfo; +}RedType; +typedef struct +{ + RedType r[maxsize+1];//r[0]作为暂存空间 + int length; +}SqList; +void swap(SqList*L, int i, int j); +void ShellSort(SqList*L); +void InsertSort(SqList*L); +void BubbleSort(SqList*L); +void QuickSort(SqList*L); +void QSort(SqList*L, int low, int high, int*count1, int*count2); +int Partition(SqList*L, int low, int high,int*count1,int*count2); +void SelectSort(SqList*L); \ No newline at end of file diff --git "a/2017-1/ywy/\346\216\222\345\272\217/test.c" "b/2017-1/ywy/\346\216\222\345\272\217/test.c" new file mode 100644 index 00000000..574fec1e --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/test.c" @@ -0,0 +1,26 @@ +#include"Sort.h" +//#include"Sort.c" +int main() +{ + SqList L, L1, L2, L3, L4; + L.length = len; + int i = 0; + + for (i = 1; i <= len; i++) + { + L.r[i].key = 10 - i;; + } + printf("\n原顺序为:"); + for (i = 1; i +#include + +//二叉查找树结点描述 +typedef int KeyType; +typedef int ElemType; +int flag; +typedef struct Node +{ + ElemType data; //关键字 + struct Node * left; //左孩子指针 + struct Node * right; //右孩子指针 + struct Node * parent; //指向父节点指针 +}Node, *PNode; + +//往二叉查找树中插入结点 +//插入的话,可能要改变根结点的地址,所以传的是二级指针 +void inseart(PNode * T, KeyType key) +{ + //初始化插入结点 + PNode p = (PNode)malloc(sizeof(Node)); + p->data = key; + p->left = p->right = p->parent = NULL; + //空树时,直接作为根结点 + if ((*T) == NULL) { + *T = p; + return; + } + //插入到当前结点(*root)的左孩子 + if ((*T)->left == NULL && (*T)->data > key) { + p->parent = (*T); + (*T)->left = p; + return; + } + //插入到当前结点(*root)的右孩子 + if ((*T)->right == NULL && (*T)->data < key) { + p->parent = (*T); + (*T)->right = p; + return; + } + if ((*T)->data > key) + inseart(&(*T)->left, key); + else if ((*T)->data < key) + inseart(&(*T)->right, key); + else + return; +} + +//查找元素,找到返回关键字的结点指针,没找到返回NULL +PNode search(PNode T, KeyType key) +{ + if (T == NULL) + return NULL; + if (key > T->data) //查找右子树 + return search(T->right, key); + else if (key < T->data) //查找左子树 + return search(T->left, key); + else + return T; +} + +//查找最小关键字,空树时返回NULL +PNode searchMin(PNode T) +{ + if (T == NULL) + return NULL; + if (T->left == NULL) + return T; + else //一直往左孩子找,直到没有左孩子的结点 + return searchMin(T->left); +} + +//查找最大关键字,空树时返回NULL +PNode searchMax(PNode T) +{ + if (T == NULL) + return NULL; + if (T->right == NULL) + return T; + else //一直往右孩子找,直到没有右孩子的结点 + return searchMax(T->right); +} + +//查找某个结点的前驱 +PNode searchPredecessor(PNode p) +{ + //空树 + if (p == NULL) + return p; + //有左子树、左子树中最大的那个 + if (p->left) + return searchMax(p->left); + //无左子树,查找某个结点的右子树遍历完了 + else { + if (p->parent == NULL) + return NULL; + //向上寻找前驱 + while (p) { + if (p->parent->right == p) + break; + p = p->parent; + } + return p->parent; + } +} + +//查找某个结点的后继 +PNode searchSuccessor(PNode p) +{ + //空树 + if (p == NULL) + return p; + //有右子树、右子树中最小的那个 + if (p->right) + return searchMin(p->right); + //无右子树,查找某个结点的左子树遍历完了 + else { + if (p->parent == NULL) + return NULL; + //向上寻找后继 + while (p) { + if (p->parent->left == p) + break; + p = p->parent; + } + return p->parent; + } +} + +//根据关键字删除某个结点,删除成功返回1,否则返回0 +//如果把根结点删掉,那么要改变根结点的地址,所以传二级指针 +int deleteNode(PNode* T, KeyType key) +{ + PNode q; + //查找到要删除的结点 + PNode p = search(*T, key); + KeyType temp; //暂存后继结点的值 + //没查到此关键字 + if (!p) + return 0; + //1.被删结点是叶子结点,直接删除 + if (p->left == NULL && p->right == NULL) { + //只有一个元素,删完之后变成一颗空树 + if (p->parent == NULL) { + free(p); + (*T) = NULL; + } + else { + //删除的结点是父节点的左孩子 + if (p->parent->left == p) + p->parent->left = NULL; + else //删除的结点是父节点的右孩子 + p->parent->right = NULL; + free(p); + } + } + + //2.被删结点只有左子树 + else if (p->left && !(p->right)) { + p->left->parent = p->parent; + //如果删除是父结点,要改变父节点指针 + if (p->parent == NULL) + *T = p->left; + //删除的结点是父节点的左孩子 + else if (p->parent->left == p) + p->parent->left = p->left; + else //删除的结点是父节点的右孩子 + p->parent->right = p->left; + free(p); + } + //3.被删结点只有右孩子 + else if (p->right && !(p->left)) { + p->right->parent = p->parent; + //如果删除是父结点,要改变父节点指针 + if (p->parent == NULL) + *T = p->right; + //删除的结点是父节点的左孩子 + else if (p->parent->left == p) + p->parent->left = p->right; + else //删除的结点是父节点的右孩子 + p->parent->right = p->right; + free(p); + } + //4.被删除的结点既有左孩子,又有右孩子 + //该结点的后继结点肯定无左子树(参考上面查找后继结点函数) + //删掉后继结点,后继结点的值代替该结点 + else { + //找到要删除结点的后继 + q = searchSuccessor(p); + temp = q->data; + //删除后继结点 + deleteNode(T, q->data); + p->data= temp; + } + return 1; +} + +//创建一棵二叉查找树 +void create(PNode* T, KeyType *keyArray, int length) +{ + int i; + //逐个结点插入二叉树中 + for (i = 0; idata,pfile); + pre(T->left,pfile); + pre(T->right,pfile); + } + //printf("\n"); +} + + +int main(void) +{ + int i; + char c = '\n'; + FILE*pfile; + pfile = fopen(" BSTOutput.txt","a"); + PNode T = NULL; + KeyType nodeArray[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + create(&T, nodeArray, 12); + pre(T,pfile); + fwrite(&c, sizeof(c), 1, pfile); + KeyType searArray[5] = { 13, 8, 5, 20, 6 }; + for (i = 0; i < 5; i++) + { + if (deleteNode(&T, searArray[i]) == 0) + { + inseart(&T, searArray[i]); + } + flag = 0; + //pre(T,pfile); + fwrite(&c, sizeof(c), 1, pfile); + } + pre(T, pfile); + fclose(pfile); + pfile = NULL; + return 0; +} + diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..c20b6e55 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" new file mode 100644 index 00000000..c523c090 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" new file mode 100644 index 00000000..120aaf0d --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" @@ -0,0 +1,521 @@ +锘#include + +#include + + + +//浜屽弶鏌ユ壘鏍戠粨鐐规弿杩 + +typedef int KeyType; + +typedef int ElemType; + +int flag; + +typedef struct Node + +{ + + ElemType data; //鍏抽敭瀛 + + struct Node * left; //宸﹀瀛愭寚閽 + + struct Node * right; //鍙冲瀛愭寚閽 + + struct Node * parent; //鎸囧悜鐖惰妭鐐规寚閽 + +}Node, *PNode; + + + +//寰浜屽弶鏌ユ壘鏍戜腑鎻掑叆缁撶偣 + +//鎻掑叆鐨勮瘽锛屽彲鑳借鏀瑰彉鏍圭粨鐐圭殑鍦板潃锛屾墍浠ヤ紶鐨勬槸浜岀骇鎸囬拡 + +void inseart(PNode * T, KeyType key) + +{ + + //鍒濆鍖栨彃鍏ョ粨鐐 + + PNode p = (PNode)malloc(sizeof(Node)); + + p->data = key; + + p->left = p->right = p->parent = NULL; + + //绌烘爲鏃讹紝鐩存帴浣滀负鏍圭粨鐐 + + if ((*T) == NULL) { + + *T = p; + + return; + + } + + //鎻掑叆鍒板綋鍓嶇粨鐐癸紙*root锛夌殑宸﹀瀛 + + if ((*T)->left == NULL && (*T)->data > key) { + + p->parent = (*T); + + (*T)->left = p; + + return; + + } + + //鎻掑叆鍒板綋鍓嶇粨鐐癸紙*root锛夌殑鍙冲瀛 + + if ((*T)->right == NULL && (*T)->data < key) { + + p->parent = (*T); + + (*T)->right = p; + + return; + + } + + if ((*T)->data > key) + + inseart(&(*T)->left, key); + + else if ((*T)->data < key) + + inseart(&(*T)->right, key); + + else + + return; + +} + + + +//鏌ユ壘鍏冪礌,鎵惧埌杩斿洖鍏抽敭瀛楃殑缁撶偣鎸囬拡锛屾病鎵惧埌杩斿洖NULL + +PNode search(PNode T, KeyType key) + +{ + + if (T == NULL) + + return NULL; + + if (key > T->data) //鏌ユ壘鍙冲瓙鏍 + + return search(T->right, key); + + else if (key < T->data) //鏌ユ壘宸﹀瓙鏍 + + return search(T->left, key); + + else + + return T; + +} + + + +//鏌ユ壘鏈灏忓叧閿瓧,绌烘爲鏃惰繑鍥濶ULL + +PNode searchMin(PNode T) + +{ + + if (T == NULL) + + return NULL; + + if (T->left == NULL) + + return T; + + else //涓鐩村線宸﹀瀛愭壘锛岀洿鍒版病鏈夊乏瀛╁瓙鐨勭粨鐐 + + return searchMin(T->left); + +} + + + +//鏌ユ壘鏈澶у叧閿瓧,绌烘爲鏃惰繑鍥濶ULL + +PNode searchMax(PNode T) + +{ + + if (T == NULL) + + return NULL; + + if (T->right == NULL) + + return T; + + else //涓鐩村線鍙冲瀛愭壘锛岀洿鍒版病鏈夊彸瀛╁瓙鐨勭粨鐐 + + return searchMax(T->right); + +} + + + +//鏌ユ壘鏌愪釜缁撶偣鐨勫墠椹 + +PNode searchPredecessor(PNode p) + +{ + + //绌烘爲 + + if (p == NULL) + + return p; + + //鏈夊乏瀛愭爲銆佸乏瀛愭爲涓渶澶х殑閭d釜 + + if (p->left) + + return searchMax(p->left); + + //鏃犲乏瀛愭爲,鏌ユ壘鏌愪釜缁撶偣鐨勫彸瀛愭爲閬嶅巻瀹屼簡 + + else { + + if (p->parent == NULL) + + return NULL; + + //鍚戜笂瀵绘壘鍓嶉┍ + + while (p) { + + if (p->parent->right == p) + + break; + + p = p->parent; + + } + + return p->parent; + + } + +} + + + +//鏌ユ壘鏌愪釜缁撶偣鐨勫悗缁 + +PNode searchSuccessor(PNode p) + +{ + + //绌烘爲 + + if (p == NULL) + + return p; + + //鏈夊彸瀛愭爲銆佸彸瀛愭爲涓渶灏忕殑閭d釜 + + if (p->right) + + return searchMin(p->right); + + //鏃犲彸瀛愭爲,鏌ユ壘鏌愪釜缁撶偣鐨勫乏瀛愭爲閬嶅巻瀹屼簡 + + else { + + if (p->parent == NULL) + + return NULL; + + //鍚戜笂瀵绘壘鍚庣户 + + while (p) { + + if (p->parent->left == p) + + break; + + p = p->parent; + + } + + return p->parent; + + } + +} + + + +//鏍规嵁鍏抽敭瀛楀垹闄ゆ煇涓粨鐐,鍒犻櫎鎴愬姛杩斿洖1,鍚﹀垯杩斿洖0 + +//濡傛灉鎶婃牴缁撶偣鍒犳帀锛岄偅涔堣鏀瑰彉鏍圭粨鐐圭殑鍦板潃锛屾墍浠ヤ紶浜岀骇鎸囬拡 + +int deleteNode(PNode* T, KeyType key) + +{ + + PNode q; + + //鏌ユ壘鍒拌鍒犻櫎鐨勭粨鐐 + + PNode p = search(*T, key); + + KeyType temp; //鏆傚瓨鍚庣户缁撶偣鐨勫 + + //娌℃煡鍒版鍏抽敭瀛 + + if (!p) + + return 0; + + //1.琚垹缁撶偣鏄彾瀛愮粨鐐癸紝鐩存帴鍒犻櫎 + + if (p->left == NULL && p->right == NULL) { + + //鍙湁涓涓厓绱狅紝鍒犲畬涔嬪悗鍙樻垚涓棰楃┖鏍 + + if (p->parent == NULL) { + + free(p); + + (*T) = NULL; + + } + + else { + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + if (p->parent->left == p) + + p->parent->left = NULL; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = NULL; + + free(p); + + } + + } + + + + //2.琚垹缁撶偣鍙湁宸﹀瓙鏍 + + else if (p->left && !(p->right)) { + + p->left->parent = p->parent; + + //濡傛灉鍒犻櫎鏄埗缁撶偣锛岃鏀瑰彉鐖惰妭鐐规寚閽 + + if (p->parent == NULL) + + *T = p->left; + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + else if (p->parent->left == p) + + p->parent->left = p->left; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = p->left; + + free(p); + + } + + //3.琚垹缁撶偣鍙湁鍙冲瀛 + + else if (p->right && !(p->left)) { + + p->right->parent = p->parent; + + //濡傛灉鍒犻櫎鏄埗缁撶偣锛岃鏀瑰彉鐖惰妭鐐规寚閽 + + if (p->parent == NULL) + + *T = p->right; + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + else if (p->parent->left == p) + + p->parent->left = p->right; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = p->right; + + free(p); + + } + + //4.琚垹闄ょ殑缁撶偣鏃㈡湁宸﹀瀛愶紝鍙堟湁鍙冲瀛 + + //璇ョ粨鐐圭殑鍚庣户缁撶偣鑲畾鏃犲乏瀛愭爲(鍙傝冧笂闈㈡煡鎵惧悗缁х粨鐐瑰嚱鏁) + + //鍒犳帀鍚庣户缁撶偣,鍚庣户缁撶偣鐨勫间唬鏇胯缁撶偣 + + else { + + //鎵惧埌瑕佸垹闄ょ粨鐐圭殑鍚庣户 + + q = searchSuccessor(p); + + temp = q->data; + + //鍒犻櫎鍚庣户缁撶偣 + + deleteNode(T, q->data); + + p->data = temp; + + } + + return 1; + +} + + + +//鍒涘缓涓妫典簩鍙夋煡鎵炬爲 + +void create(PNode* T, KeyType *keyArray, int length) + +{ + + int i; + + //閫愪釜缁撶偣鎻掑叆浜屽弶鏍戜腑 + + for (i = 0; idata, pfile); + + pre(T->left, pfile); + + pre(T->right, pfile); + + } + + //printf("\n"); + +} + + + + + +int main(void) + +{ + + int i; + + char c = '\n'; + + FILE*pfile; + + pfile = fopen(" BSTOutput.txt", "a"); + + PNode T = NULL; + + KeyType nodeArray[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + + create(&T, nodeArray, 12); + + pre(T, pfile); + + fwrite(&c, sizeof(c), 1, pfile); + + KeyType searArray[5] = { 13, 8, 5, 20, 6 }; + + for (i = 0; i < 5; i++) + + { + + if (deleteNode(&T, searArray[i]) == 0) + + { + + inseart(&T, searArray[i]); + + } + + flag = 0; + + //pre(T,pfile); + + fwrite(&c, sizeof(c), 1, pfile); + + } + + pre(T, pfile); + + fclose(pfile); + + pfile = NULL; + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" new file mode 100644 index 00000000..a42e035d Binary files /dev/null and "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" differ diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..319bcc09 --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,179 @@ +#include"Sort.h" +void swap(SqList*L, int i, int j) +{ + RedType temp = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = temp; +} + +void ShellSort(SqList*L) +{ + int i, j; + int count1 = 0, count2 = 0; + int rise = L->length; + do + { + rise = rise / 5 + 1; + for (i = rise + 1; i <= L->length; i++) + { + count1++; + if (LT(L->r[i].key, L->r[i - rise].key)) + { + L->r[0] = L->r[i]; + count2++; + for (j = i - rise; LT(L->r[0].key, L->r[j].key) && j > 0; j = j - rise) + { + count1++; + L->r[j + rise] = L->r[j]; + count2++; + } + count1++; + L->r[j + rise] = L->r[0]; + count2++; + } + } + } while (rise > 1); + printf("希尔排序为: "); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +void InsertSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 2; i <= L->length; i++) + { + ++count1; + if (LT(L->r[i].key, L->r[i - 1].key)) + { + L->r[0] = L->r[i]; + count2++; + //L->r[i] = L->r[i - 1]; + for (j = i - 1; LT(L->r[0].key, L->r[j].key); --j) + { + count1++; + count2++; + L->r[j + 1] = L->r[j]; + } + count1++; + L->r[j + 1] = L->r[0]; + count2++; + } + } + printf("插入排序后为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +void BubbleSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + for (j = i + 1; j < L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[i].key)) + { + swap(L, i, j); + count2 += 3; + } + } + } + printf("冒泡排序后为:"); + for (i = 1; ilength; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +int Partition(SqList*L, int low, int high, int*count1, int*count2) +{ + int pivotkey; + //L->r[0] = L->r[low]; + pivotkey = L->r[low].key; + while (low < high) + { + while (L->r[high].key >= pivotkey && low < high) + { + (*count1)++; + high--; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + while (L->r[low].key <= pivotkey && low < high) + { + (*count1)++; + low++; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + } + return low; +} + +void QSort(SqList*L, int low, int high, int*count1, int*count2) +{ + int pivot; + if (low < high) + { + pivot = Partition(L, low, high, count1, count2); + QSort(L, low, pivot - 1, count1, count2); + QSort(L, pivot + 1, high, count1, count2); + } +} +void QuickSort(SqList*L) +{ + int i; + int count1 = 0, count2 = 0; + QSort(L, 1, L->length, &count1, &count2); + printf("快速排序后为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); +} +void SelectSort(SqList*L) +{ + int i, j, min; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + min = i; + for (j = i + 1; j <= L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[min].key)) + { + min = j; + } + } + if (i != min) + { + swap(L, i, min); + count2 += 3; + } + } + printf("\n简单选择排序为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..b36753ec --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +typedef int KeyType; +typedef int InfoType; +#define maxsize 20 +#define len 10 +#define LT(a,b) ((a)<(b)) +typedef struct +{ + KeyType key; + InfoType otherinfo; +}RedType; +typedef struct +{ + RedType r[maxsize + 1]; + int length; +}SqList; +void swap(SqList*L, int i, int j); +void ShellSort(SqList*L); +void InsertSort(SqList*L); +void BubbleSort(SqList*L); +void QuickSort(SqList*L); +void QSort(SqList*L, int low, int high, int*count1, int*count2); +int Partition(SqList*L, int low, int high, int*count1, int*count2); +void SelectSort(SqList*L); diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" new file mode 100644 index 00000000..a59eb1d7 --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" @@ -0,0 +1,51 @@ +锘#include"Sort.h" + +//#include"Sort.c" + +int main() + +{ + + SqList L, L1, L2, L3, L4; + + L.length = len; + + int i = 0; + + srand((unsigned)time(NULL)); + + for (i = 1; i <= len; i++) + + { + + L.r[i].key =rand()%15; + + } + + printf("\n鍘熷簭锛"); + + for (i = 1; i