forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,047 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
#define TRUE 1 | ||
#define FALSE 0 | ||
#define OK 1 | ||
#define ERROR 0 | ||
#define OVERFLOW -1 | ||
#define SUCCESS 1 | ||
#define UNSUCCESS 0 | ||
|
||
#define dataNum 5 | ||
int i = 0; | ||
int dep = 0; | ||
char data[dataNum] = { 'A', 'B', 'C', 'D', 'E' }; | ||
|
||
typedef int Status; | ||
typedef char TElemType; | ||
|
||
typedef struct BiTNode | ||
{ | ||
TElemType data; | ||
struct BiTNode *lchild, *rchild; | ||
}BiTNode, *BiTree; | ||
|
||
void InitBiTree(BiTree &T); //创建一颗空二叉树 | ||
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R); //创建一颗二叉树T,其中根节点的值为e,L和R分别作为左子树和右子树 | ||
void DestroyBiTree(BiTree &T); //销毁二叉树 | ||
Status BiTreeEmpty(BiTree T); //对二叉树判空。若为空返回TRUE,否则FALSE | ||
Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R); //将一颗二叉树T分解成根、左子树、右子树三部分 | ||
Status ReplaceLeft(BiTree &T, BiTree <); //替换左子树。若T非空,则用LT替换T的左子树,并用LT返回T的原有左子树 | ||
Status ReplaceRight(BiTree &T, BiTree &RT); //替换右子树。若T非空,则用RT替换T的右子树,并用RT返回T的原有右子树 | ||
|
||
int Leaves(BiTree T); | ||
int Depth(BiTree T); | ||
|
||
Status visit(TElemType e); | ||
void UnionBiTree(BiTree &Ttemp); | ||
|
||
|
||
//InitBiTree空二叉树是只有一个BiTree指针?还是有一个结点但结点域为空? | ||
void InitBiTree(BiTree &T) | ||
{ | ||
T = NULL; | ||
} | ||
|
||
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R) | ||
{ | ||
BiTree t; | ||
t = (BiTree)malloc(sizeof(BiTNode)); | ||
if (NULL == t) return NULL; | ||
t->data = e; | ||
t->lchild = L; | ||
t->rchild = R; | ||
return t; | ||
} | ||
|
||
Status visit(TElemType e) | ||
{ | ||
printf("%c", e); | ||
return OK; | ||
} | ||
|
||
|
||
int Leaves(BiTree T) //对二叉树T求叶子结点数目 | ||
{ | ||
int l = 0, r = 0; | ||
|
||
if (NULL == T) return 0; | ||
if (NULL == T->lchild && NULL == T->rchild) return 1; | ||
|
||
//问题分解,2个子问题 | ||
|
||
|
||
//求左子树叶子数目 | ||
l = Leaves(T->lchild); | ||
|
||
//求右子树叶子数目 | ||
r = Leaves(T->rchild); | ||
|
||
//组合 | ||
return r + l; | ||
} | ||
|
||
int depTraverse(BiTree T) //层次遍历:dep是个全局变量,高度 | ||
{ | ||
if (NULL == T) return ERROR; | ||
|
||
dep = (depTraverse(T->lchild) > depTraverse(T->rchild)) ? depTraverse(T->lchild) : depTraverse(T->rchild); | ||
|
||
return dep + 1; | ||
} | ||
|
||
|
||
void levTraverse(BiTree T, Status(*visit)(TElemType e), int lev) //高度遍历:lev是局部变量,层次 | ||
{ | ||
if (NULL == T) return; | ||
|
||
visit(T->data); | ||
printf("的层次是%d\n", lev); | ||
|
||
levTraverse(T->lchild, visit, ++lev); | ||
levTraverse(T->rchild, visit, lev); | ||
|
||
} | ||
|
||
void InOrderTraverse(BiTree T, Status(*visit)(TElemType e), int &num) //num是个全局变量 | ||
{ | ||
if (NULL == T) return; | ||
visit(T->data); | ||
if (NULL == T->lchild && NULL == T->rchild) { printf("是叶子结点"); num++; } | ||
else printf("不是叶子结点"); | ||
printf("\n"); | ||
InOrderTraverse(T->lchild, visit, num); | ||
InOrderTraverse(T->rchild, visit, num); | ||
} | ||
|
||
Status BiTreeEmpty(BiTree T) | ||
{ | ||
if (NULL == T) return TRUE; | ||
return FALSE; | ||
} | ||
|
||
Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R) | ||
{ | ||
if (NULL == T) return ERROR; | ||
L = T->lchild; | ||
R = T->rchild; | ||
T->lchild = NULL; | ||
T->rchild = NULL; | ||
return OK; | ||
} | ||
|
||
Status ReplaceLeft(BiTree &T, BiTree <) | ||
{ | ||
BiTree temp; | ||
if (NULL == T) return ERROR; | ||
temp = T->lchild; | ||
T->lchild = LT; | ||
LT = temp; | ||
return OK; | ||
} | ||
|
||
Status ReplaceRight(BiTree &T, BiTree &RT) | ||
{ | ||
BiTree temp; | ||
if (NULL == T) return ERROR; | ||
temp = T->rchild; | ||
T->rchild = RT; | ||
RT = temp; | ||
return OK; | ||
} | ||
|
||
void UnionBiTree(BiTree &Ttemp) | ||
{ | ||
BiTree L = NULL, R = NULL; | ||
L = MakeBiTree(data[i++], NULL, NULL); | ||
R = MakeBiTree(data[i++], NULL, NULL); | ||
ReplaceLeft(Ttemp, L); | ||
ReplaceRight(Ttemp, R); | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
|
||
BiTree T = NULL, Ttemp = NULL; | ||
|
||
InitBiTree(T); | ||
if (TRUE == BiTreeEmpty(T)) printf("初始化T为空\n"); | ||
else printf("初始化T不为空\n"); | ||
|
||
T = MakeBiTree(data[i++], NULL, NULL); | ||
|
||
Ttemp = T; | ||
UnionBiTree(Ttemp); | ||
|
||
Ttemp = T->lchild; | ||
UnionBiTree(Ttemp); | ||
|
||
|
||
Status(*visit1)(TElemType); | ||
visit1 = visit; | ||
int num = 0; | ||
InOrderTraverse(T, visit1, num); | ||
printf("叶子结点是 %d\n", num); | ||
|
||
printf("叶子结点是 %d\n", Leaves(T)); | ||
|
||
int lev = 1; | ||
levTraverse(T, visit1, lev); | ||
|
||
printf("高度是 %d\n", depTraverse(T)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
#define SUCCESS 1 | ||
#define UNSUCCESS 0 | ||
#define OVERFLOW -1 | ||
#define OK 1 | ||
#define ERROR -1 | ||
|
||
typedef int Status; | ||
typedef int KeyType; | ||
|
||
typedef struct{ | ||
KeyType key; | ||
}RcdType; | ||
|
||
typedef struct{ | ||
RcdType *rcd; | ||
int size; | ||
int count; | ||
int *tag; | ||
}HashTable; | ||
|
||
int hashsize[] = { 11, 31, 61, 127, 251, 503 }; | ||
int index = 0; | ||
|
||
Status InitHashTable(HashTable &H, int size){ | ||
int i; | ||
H.rcd = (RcdType *)malloc(sizeof(RcdType)*size); | ||
H.tag = (int *)malloc(sizeof(int)*size); | ||
if (NULL == H.rcd || NULL == H.tag) return OVERFLOW; | ||
for (i = 0; i< size; i++) H.tag[i] = 0; | ||
H.size = size; | ||
H.count = 0; | ||
return OK; | ||
} | ||
|
||
int Hash(KeyType key, int m){ | ||
return (3 * key) % m; | ||
} | ||
|
||
void collision(int &p, int m){ //线性探测 | ||
p = (p + 1) % m; | ||
} | ||
|
||
Status SearchHash(HashTable H, KeyType key, int &p, int &c) { | ||
p = Hash(key, H.size); | ||
int h = p; | ||
c = 0; | ||
while ((1 == H.tag[p] && H.rcd[p].key != key) || -1 == H.tag[p]){ | ||
collision(p, H.size); c++; | ||
} | ||
|
||
if (1 == H.tag[p] && key == H.rcd[p].key) return SUCCESS; | ||
else return UNSUCCESS; | ||
|
||
} | ||
|
||
void printHash(HashTable H) //打印哈希表 | ||
{ | ||
int i; | ||
printf("key : "); | ||
for (i = 0; i < H.size; i++) | ||
printf("%3d ", H.rcd[i].key); | ||
printf("\n"); | ||
printf("tag : "); | ||
for (i = 0; i < H.size; i++) | ||
printf("%3d ", H.tag[i]); | ||
printf("\n\n"); | ||
} | ||
|
||
Status InsertHash(HashTable &H, KeyType key); //对函数的声明 | ||
|
||
//重构 | ||
Status recreateHash(HashTable &H){ | ||
RcdType *orcd; | ||
int *otag, osize, i; | ||
orcd = H.rcd; | ||
otag = H.tag; | ||
osize = H.size; | ||
|
||
InitHashTable(H, hashsize[index++]); | ||
//把所有元素,按照新哈希函数放到新表中 | ||
for (i = 0; i < osize; i++){ | ||
if (1 == otag[i]){ | ||
InsertHash(H, orcd[i].key); | ||
} | ||
} | ||
} | ||
|
||
Status InsertHash(HashTable &H, KeyType key){ | ||
int p, c; | ||
if (UNSUCCESS == SearchHash(H, key, p, c)){ //没有相同key | ||
if (c*1.0 / H.size < 0.5){ //冲突次数未达到上线 | ||
//插入代码 | ||
H.rcd[p].key = key; | ||
H.tag[p] = 1; | ||
H.count++; | ||
return SUCCESS; | ||
} | ||
else recreateHash(H); //重构哈希表 | ||
} | ||
return UNSUCCESS; | ||
} | ||
|
||
Status DeleteHash(HashTable &H, KeyType key){ | ||
int p, c; | ||
if (SUCCESS == SearchHash(H, key, p, c)){ | ||
//删除代码 | ||
H.tag[p] = -1; | ||
H.count--; | ||
|
||
|
||
return SUCCESS; | ||
} | ||
else return UNSUCCESS; | ||
} | ||
|
||
void main() | ||
{ | ||
printf("-----哈希表-----\n"); | ||
HashTable H; | ||
int i; | ||
int size = 11; | ||
KeyType array[8] = { 22, 41, 53, 46, 30, 13, 12, 67 }; | ||
KeyType key; | ||
RcdType e; | ||
|
||
//初始化哈希表 | ||
printf("初始化哈希表\n"); | ||
if (SUCCESS == InitHashTable(H, hashsize[index++])) printf("初始化成功\n"); | ||
|
||
//插入哈希表 | ||
printf("插入哈希表\n"); | ||
for (i = 0; i <= 7; i++){ | ||
key = array[i]; | ||
InsertHash(H, key); | ||
printHash(H); | ||
} | ||
|
||
//删除哈希表 | ||
printf("删除哈希表\n"); | ||
int p, c; | ||
if (SUCCESS == DeleteHash(H, 12)) { | ||
printf("删除成功,此时哈希表为:\n"); | ||
printHash(H); | ||
} | ||
|
||
//查询哈希表 | ||
printf("查询哈希表\n"); | ||
if (SUCCESS == SearchHash(H, 67, p, c)) printf("查询成功\n"); | ||
|
||
//再次插入,测试哈希表的重构 | ||
printf("再次插入,测试哈希表的重构:\n"); | ||
KeyType array1[8] = { 27, 47, 57, 47, 37, 17, 93, 67 }; | ||
for (i = 0; i <= 7; i++){ | ||
key = array1[i]; | ||
InsertHash(H, key); | ||
printHash(H); | ||
} | ||
} |
Oops, something went wrong.