-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path11-散列3 QQ帐户的申请与登陆 散列法、排序法和AVL树查找法 三种方法对比实现.c
436 lines (387 loc) · 10 KB
/
11-散列3 QQ帐户的申请与登陆 散列法、排序法和AVL树查找法 三种方法对比实现.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* 对应课程讨论11.3
* 对该问题使用了散列方法、排序方法和AVL树查找三种方法进行了比较
* 其中排序方法和AVL树查找法测试点1和2均超时,说明对于本题,后两种方法性能远差于散列方法
* 排序方法中,查询某一元素是否存在使用了二分查找,而每加入一个新用户都要进行一次qsort快排
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#define QQSMALLEST 4
#define QQLENGTH 10
#define PWLENGTH 16
#define MAXD 5
#define MAXTABLESIZE 1000000
#define NOTFOUND -1
typedef struct UserInfoNode UserInfo;
struct UserInfoNode {
char qq[QQLENGTH + 1];
char password[PWLENGTH + 1];
};
typedef UserInfo ElementType;
/* ——————HashTable相关定义开始—————— */
typedef int Index;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef struct LHNode *PtrToLHNode;
struct LHNode {
PtrToLNode Next;
};
typedef PtrToLHNode List;
typedef struct TblNode *HashTable;
struct TblNode {
int TableSize;
List Heads;
};
int NextPrime(int N); // 返回大于N且不超过MAXTABLESIZE的最小素数
int Hash(char *Key, int p);
HashTable CreateTable(int TableSize);
Position Find(HashTable H, ElementType Key);
bool Insert(HashTable H, ElementType Key);
void DestoryTable(HashTable H);
/* ——————HashTable相关定义结束—————— */
/* ——————排序处理相关函数定义开始—————— */
int comp(const void *a, const void *b);
int binarySearch(UserInfo *arr, int user_Num, UserInfo *info); // 查找采用二分法
bool Sort_Item(UserInfo *arr, int *user_Num, UserInfo *info); // 采用排序方法使加入的新元素有序
/* ——————排序处理相关函数定义结束—————— */
/* ——————AVL树相关定义开始—————— */
typedef struct TreeNode *AVLTree;
struct TreeNode {
UserInfo info;
AVLTree Left, Right;
};
int getHeight( AVLTree T );
AVLTree RR( AVLTree T );
AVLTree LL( AVLTree T );
AVLTree RL( AVLTree T );
AVLTree LR( AVLTree T );
AVLTree Search(AVLTree T, UserInfo *info); // 未找到返回NULL
AVLTree InsertAVL( AVLTree T, UserInfo *info ); // 插入成功返回新的树根结点,否则返回NULL
void FreeTree( AVLTree T );
/* ——————AVL树相关定义开始—————— */
void HandleInput(); // 处理输入
void HashTableHandle(int N); // 使用HashTable来处理
void HashTableCheck(HashTable H, char flag, UserInfo *info); // 对每一行指令进行处理
void SortHandle(int N); // 使用排序方法来处理,测试点1、2不通过,运行超时
int SortCheck(UserInfo *arr, int user_Num, char flag, UserInfo *info); // 对每一行指令进行二分查找处理,返回当前用户数
void AVLHandle(int N); // 使用AVL树来处理
AVLTree AVLCheck(AVLTree T, char flag, UserInfo *info); // 返回执行完该指令后新的AVL树根节点
int main()
{
HandleInput();
return 0;
}
int NextPrime( int N )
{
int i, p;
if (N < 2) return 2;
else p = (N%2)? N+2 : N+1; /*从大于N的下一个奇数开始 */
while( p <= MAXTABLESIZE ) {
for( i=(int)sqrt(p); i>1; i-- )
if ( !(p%i) ) break; /* p不是素数 */
if ( i==1 ) break; /* for正常结束,说明p是素数 */
else p += 2; /* 否则试探下一个奇数 */
}
return p;
}
int Hash(char *Key, int P)
{
int tmp;
if (strlen(Key) == QQSMALLEST)
tmp = atoi(Key);
else
tmp = atoi(Key + strlen(Key) - MAXD);
return tmp % P;
}
HashTable CreateTable(int TableSize)
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = NextPrime(TableSize);
H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
for (i = 0; i < H->TableSize; ++i) {
H->Heads[i].Next = NULL;
}
return H;
}
Position Find(HashTable H, ElementType Key)
{
Position P;
Index Pos;
Pos = Hash(Key.qq, H->TableSize);
P = H->Heads[Pos].Next;
while(P && strcmp(P->Data.qq, Key.qq))
P = P->Next;
return P;
}
bool Insert(HashTable H, ElementType Key)
{
Position P, NewCell;
Index Pos;
P = Find(H, Key);
if (!P) {
NewCell = (Position)malloc(sizeof(struct LNode));
strcpy(NewCell->Data.qq, Key.qq);
strcpy(NewCell->Data.password, Key.password);
Pos = Hash(Key.qq, H->TableSize);
NewCell->Next = H->Heads[Pos].Next;
H->Heads[Pos].Next = NewCell;
return true;
}
else
return false;
}
void DestoryTable(HashTable H)
{
int i;
Position P, Tmp;
for (i = 0; i < H->TableSize; ++i) {
P = H->Heads[i].Next;
while (P) {
Tmp = P->Next;
free(P);
P = Tmp;
}
}
free(H->Heads);
free(H);
}
int comp(const void *a, const void *b)
{
char *str1, *str2;
int size1, size2;
str1 = ((UserInfo *)a)->qq; str2 = ((UserInfo *)b)->qq;
size1 = strlen(str1); size2 = strlen(str2);
if (size1 == size2)
return strcmp(str1, str2);
else
return size1 < size2 ? -1 : 1;
}
int binarySearch(UserInfo *arr, int user_Num, UserInfo *info)
{
int low, mid, high, ans;
low = 0; high = user_Num;
while (low <= high) {
mid = (low + high) >> 1;
ans = comp(arr + mid, info);
if (ans > 0)
high = mid - 1;
else if (ans < 0)
low = mid + 1;
else return mid;
}
return NOTFOUND;
}
bool Sort_Item(UserInfo *arr, int *user_Num, UserInfo *info)
{
int pos;
pos = binarySearch(arr, *user_Num, info);
if (pos == -1) {
strcpy(arr[*user_Num].qq, (*info).qq);
strcpy(arr[*user_Num].password, (*info).password);
++(*user_Num);
qsort(arr, *user_Num, sizeof(UserInfo), comp);
return true;
}
return false;
}
int getHeight( AVLTree T )
{
if (!T) return 0;
int lHeight, rHeight;
lHeight = getHeight(T->Left);
rHeight = getHeight(T->Right);
return (lHeight > rHeight ? lHeight : rHeight) + 1;
}
AVLTree RR( AVLTree T )
{
AVLTree tmp;
tmp = T->Right;
T->Right = tmp->Left;
tmp->Left = T;
return tmp;
}
AVLTree LL( AVLTree T )
{
AVLTree tmp;
tmp = T->Left;
T->Left = tmp->Right;
tmp->Right = T;
return tmp;
}
AVLTree RL( AVLTree T )
{
AVLTree tmp1, tmp2;
tmp1 = T->Right;
tmp2 = tmp1->Left;
tmp1->Left = tmp2->Right;
T->Right = tmp2->Left;
tmp2->Left = T;
tmp2->Right = tmp1;
return tmp2;
}
AVLTree LR( AVLTree T )
{
AVLTree tmp1, tmp2;
tmp1 = T->Left;
tmp2 = tmp1->Right;
tmp1->Right = tmp2->Left;
T->Left = tmp2->Right;
tmp2->Left = tmp1;
tmp2->Right = T;
return tmp2;
}
AVLTree Search(AVLTree T, UserInfo *info)
{
int ans;
if (!T) return NULL;
else {
ans = comp(&(T->info), info);
if (ans == 0) return T;
else if (ans > 0) return Search(T->Left, info);
else return Search(T->Right, info);
}
}
AVLTree InsertAVL( AVLTree T, UserInfo *info )
{
int ans;
if (!T) {
T = (AVLTree)malloc(sizeof(struct TreeNode));
strcpy(T->info.qq, info->qq);
strcpy(T->info.password, info->password);
T->Left = T->Right = NULL;
}
else {
ans = comp(info, &(T->info));
if (ans > 0) {
T->Right = InsertAVL(T->Right, info);
if (getHeight(T->Left) - getHeight(T->Right) == -2) {
if (comp(info, &(T->Right->info)) > 0) T = RR(T);
else T = RL(T);
}
}
else if (ans < 0) {
T->Left = InsertAVL(T->Left, info);
if (getHeight(T->Left) - getHeight(T->Right) == 2) {
if (comp(info, &(T->Left->info)) < 0) T = LL(T);
else T = LR(T);
}
}
}
return T;
}
void FreeTree( AVLTree T )
{
if (T->Left) FreeTree(T->Left);
if (T->Right) FreeTree(T->Right);
free(T);
}
void HandleInput()
{
int N;
scanf("%d", &N);
HashTableHandle(N);
}
void HashTableHandle(int N)
{
int i; char flag;
UserInfo info;
HashTable H;
H = CreateTable(N);
for (i = 0; i < N; ++i) {
scanf("\n%c %s %s", &flag, info.qq, info.password);
HashTableCheck(H, flag, &info);
}
DestoryTable(H);
}
void HashTableCheck(HashTable H, char flag, UserInfo *info)
{
Position pos;
if (flag == 'N') {
if (Insert(H, *info)) printf("New: OK");
else printf("ERROR: Exist");
}
else {
pos = Find(H, *info);
if (pos) {
if (strcmp(info->password, pos->Data.password))
printf("ERROR: Wrong PW");
else printf("Login: OK");
}
else printf("ERROR: Not Exist");
}
printf("\n");
}
void SortHandle(int N)
{
int i, user_Num; char flag;
UserInfo info, *arr;
user_Num = 0;
arr = (UserInfo *)malloc(N * sizeof(UserInfo));
for (i = 0; i < N; ++i) {
scanf("\n%c %s %s", &flag, info.qq, info.password);
user_Num = SortCheck(arr, user_Num, flag, &info);
}
free(arr);
}
int SortCheck(UserInfo *arr, int user_Num, char flag, UserInfo *info)
{
int pos;
if (flag == 'N') {
if (Sort_Item(arr, &user_Num, info)) printf("New: OK");
else printf("ERROR: Exist");
}
else {
pos = binarySearch(arr, user_Num, info);
if (pos != -1) {
if (strcmp(info->password, arr[pos].password))
printf("ERROR: Wrong PW");
else printf("Login: OK");
}
else printf("ERROR: Not Exist");
}
printf("\n");
return user_Num;
}
void AVLHandle(int N)
{
int i; char flag;
UserInfo info;
AVLTree T;
T = NULL;
for (i = 0; i < N; ++i) {
scanf("\n%c %s %s", &flag, info.qq, info.password);
T = AVLCheck(T, flag, &info);
}
FreeTree(T);
}
AVLTree AVLCheck(AVLTree T, char flag, UserInfo *info)
{
AVLTree pos;
pos = Search(T, info);
if (flag == 'N') {
if (!pos) {
T = InsertAVL(T, info);
printf("New: OK");
}
else printf("ERROR: Exist");
}
else {
if (pos) {
if (strcmp(info->password, pos->info.password))
printf("ERROR: Wrong PW");
else printf("Login: OK");
}
else printf("ERROR: Not Exist");
}
printf("\n");
return T;
}