This commit is contained in:
krahets
2023-11-01 05:12:56 +08:00
parent 26e524a1dd
commit ca8ef0575e
12 changed files with 126 additions and 105 deletions

View File

@ -63,6 +63,8 @@ status: new
| 完全二叉树 | complete binary tree | | 完全二叉树 | complete binary tree |
| 完满二叉树 | full binary tree | | 完满二叉树 | full binary tree |
| 平衡二叉树 | balanced binary tree | | 平衡二叉树 | balanced binary tree |
| AVL 树 | AVL tree |
| 红黑树 | red-black tree |
| 层序遍历 | level-order traversal | | 层序遍历 | level-order traversal |
| 广度优先遍历 | breadth-first traversal | | 广度优先遍历 | breadth-first traversal |
| 深度优先遍历 | depth-first traversal | | 深度优先遍历 | depth-first traversal |
@ -108,8 +110,8 @@ status: new
| 解 | solution | | 解 | solution |
| 状态 | state | | 状态 | state |
| 剪枝 | pruning | | 剪枝 | pruning |
| 全排列问题 | Permutations problem | | 全排列问题 | permutations problem |
| 子集和问题 | Subset-sum problem | | 子集和问题 | subset-sum problem |
| N 皇后问题 | N-queens problem | | N 皇后问题 | N-queens problem |
| 动态规划 | dynamic programming | | 动态规划 | dynamic programming |
| 初始状态 | initial state | | 初始状态 | initial state |

View File

@ -1965,7 +1965,7 @@ comments: true
/* 删除元素 */ /* 删除元素 */
// 注意stdio.h 占用了 remove 关键词 // 注意stdio.h 占用了 remove 关键词
int removeNum(MyList *nums, int index) { int removeItem(MyList *nums, int index) {
assert(index >= 0 && index < size(nums)); assert(index >= 0 && index < size(nums));
int num = nums->arr[index]; int num = nums->arr[index];
for (int i = index; i < size(nums) - 1; i++) { for (int i = index; i < size(nums) - 1; i++) {

View File

@ -961,6 +961,11 @@ comments: true
return graph; return graph;
} }
/* 析构函数 */
void delGraphAdjMat(GraphAdjMat *graph) {
free(graph);
}
/* 添加顶点 */ /* 添加顶点 */
void addVertex(GraphAdjMat *graph, int val) { void addVertex(GraphAdjMat *graph, int val) {
if (graph->size == MAX_SIZE) { if (graph->size == MAX_SIZE) {

View File

@ -1165,7 +1165,7 @@ comments: true
} HashMapChaining; } HashMapChaining;
/* 构造函数 */ /* 构造函数 */
HashMapChaining *initHashMapChaining() { HashMapChaining *newHashMapChaining() {
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining)); HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
hashMap->size = 0; hashMap->size = 0;
hashMap->capacity = 4; hashMap->capacity = 4;
@ -1179,14 +1179,14 @@ comments: true
} }
/* 析构函数 */ /* 析构函数 */
void freeHashMapChaining(HashMapChaining *hashMap) { void delHashMapChaining(HashMapChaining *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Node *cur = hashMap->buckets[i]; Node *cur = hashMap->buckets[i];
while (cur) { while (cur) {
Node *temp = cur; Node *tmp = cur;
cur = cur->next; cur = cur->next;
free(temp->pair); free(tmp->pair);
free(temp); free(tmp);
} }
} }
free(hashMap->buckets); free(hashMap->buckets);
@ -1273,7 +1273,7 @@ comments: true
} }
/* 删除操作 */ /* 删除操作 */
void removeKey(HashMapChaining *hashMap, int key) { void removeItem(HashMapChaining *hashMap, int key) {
int index = hashFunc(hashMap, key); int index = hashFunc(hashMap, key);
Node *cur = hashMap->buckets[index]; Node *cur = hashMap->buckets[index];
Node *pre = NULL; Node *pre = NULL;

View File

@ -1408,116 +1408,118 @@ index = hash(key) % capacity
/* 基于数组简易实现的哈希表 */ /* 基于数组简易实现的哈希表 */
typedef struct { typedef struct {
Pair *buckets[HASH_MAP_DEFAULT_SIZE]; Pair *buckets[HASHTABLE_CAPACITY];
} ArrayHashMap; } ArrayHashMap;
/* 哈希表初始化函数 */ /* 构造函数 */
ArrayHashMap *newArrayHashMap() { ArrayHashMap *newArrayHashMap() {
ArrayHashMap *map = malloc(sizeof(ArrayHashMap)); ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
return map; return hmap;
}
/* 析构函数 */
void delArrayHashMap(ArrayHashMap *hmap) {
for (int i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
free(hmap->buckets[i]->val);
free(hmap->buckets[i]);
}
}
free(hmap);
} }
/* 添加操作 */ /* 添加操作 */
void put(ArrayHashMap *d, const int key, const char *val) { void put(ArrayHashMap *hmap, const int key, const char *val) {
Pair *Pair = malloc(sizeof(Pair)); Pair *Pair = malloc(sizeof(Pair));
Pair->key = key; Pair->key = key;
Pair->val = malloc(strlen(val) + 1); Pair->val = malloc(strlen(val) + 1);
strcpy(Pair->val, val); strcpy(Pair->val, val);
int index = hashFunc(key); int index = hashFunc(key);
d->buckets[index] = Pair; hmap->buckets[index] = Pair;
} }
/* 删除操作 */ /* 删除操作 */
void removeItem(ArrayHashMap *d, const int key) { void removeItem(ArrayHashMap *hmap, const int key) {
int index = hashFunc(key); int index = hashFunc(key);
free(d->buckets[index]->val); free(hmap->buckets[index]->val);
free(d->buckets[index]); free(hmap->buckets[index]);
d->buckets[index] = NULL; hmap->buckets[index] = NULL;
} }
/* 获取所有键值对 */ /* 获取所有键值对 */
void pairSet(ArrayHashMap *d, MapSet *set) { void pairSet(ArrayHashMap *hmap, MapSet *set) {
Pair *entries; Pair *entries;
int i = 0, index = 0; int i = 0, index = 0;
int total = 0; int total = 0;
/* 统计有效键值对数量 */ /* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
total++; total++;
} }
} }
entries = malloc(sizeof(Pair) * total); entries = malloc(sizeof(Pair) * total);
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
entries[index].key = d->buckets[i]->key; entries[index].key = hmap->buckets[i]->key;
entries[index].val = malloc(strlen(d->buckets[i]->val + 1)); entries[index].val = malloc(strlen(hmap->buckets[i]->val + 1));
strcpy(entries[index].val, d->buckets[i]->val); strcpy(entries[index].val, hmap->buckets[i]->val);
index++; index++;
} }
} }
set->set = entries; set->set = entries;
set->len = total; set->len = total;
} }
/* 获取所有键 */ /* 获取所有键 */
void keySet(ArrayHashMap *d, MapSet *set) { void keySet(ArrayHashMap *hmap, MapSet *set) {
int *keys; int *keys;
int i = 0, index = 0; int i = 0, index = 0;
int total = 0; int total = 0;
/* 统计有效键值对数量 */ /* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
total++; total++;
} }
} }
keys = malloc(total * sizeof(int)); keys = malloc(total * sizeof(int));
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
keys[index] = d->buckets[i]->key; keys[index] = hmap->buckets[i]->key;
index++; index++;
} }
} }
set->set = keys; set->set = keys;
set->len = total; set->len = total;
} }
/* 获取所有值 */ /* 获取所有值 */
void valueSet(ArrayHashMap *d, MapSet *set) { void valueSet(ArrayHashMap *hmap, MapSet *set) {
char **vals; char **vals;
int i = 0, index = 0; int i = 0, index = 0;
int total = 0; int total = 0;
/* 统计有效键值对数量 */ /* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
total++; total++;
} }
} }
vals = malloc(total * sizeof(char *)); vals = malloc(total * sizeof(char *));
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) { for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (d->buckets[i] != NULL) { if (hmap->buckets[i] != NULL) {
vals[index] = d->buckets[i]->val; vals[index] = hmap->buckets[i]->val;
index++; index++;
} }
} }
set->set = vals; set->set = vals;
set->len = total; set->len = total;
} }
/* 打印哈希表 */ /* 打印哈希表 */
void print(ArrayHashMap *d) { void print(ArrayHashMap *hmap) {
int i; int i;
MapSet set; MapSet set;
pairSet(d, &set); pairSet(hmap, &set);
Pair *entries = (Pair *)set.set; Pair *entries = (Pair *)set.set;
for (i = 0; i < set.len; i++) { for (i = 0; i < set.len; i++) {
printf("%d -> %s\n", entries[i].key, entries[i].val); printf("%d -> %s\n", entries[i].key, entries[i].val);

View File

@ -174,14 +174,14 @@ comments: true
/* 构造函数,根据切片建堆 */ /* 构造函数,根据切片建堆 */
MaxHeap *newMaxHeap(int nums[], int size) { MaxHeap *newMaxHeap(int nums[], int size) {
// 所有元素入堆 // 所有元素入堆
MaxHeap *h = (MaxHeap *)malloc(sizeof(MaxHeap)); MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));
h->size = size; maxHeap->size = size;
memcpy(h->data, nums, size * sizeof(int)); memcpy(maxHeap->data, nums, size * sizeof(int));
for (int i = parent(h, size - 1); i >= 0; i--) { for (int i = parent(maxHeap, size - 1); i >= 0; i--) {
// 堆化除叶节点以外的其他所有节点 // 堆化除叶节点以外的其他所有节点
siftDown(h, i); siftDown(maxHeap, i);
} }
return h; return maxHeap;
} }
``` ```

View File

@ -565,17 +565,17 @@ comments: true
```c title="my_heap.c" ```c title="my_heap.c"
/* 获取左子节点索引 */ /* 获取左子节点索引 */
int left(MaxHeap *h, int i) { int left(MaxHeap *maxHeap, int i) {
return 2 * i + 1; return 2 * i + 1;
} }
/* 获取右子节点索引 */ /* 获取右子节点索引 */
int right(MaxHeap *h, int i) { int right(MaxHeap *maxHeap, int i) {
return 2 * i + 2; return 2 * i + 2;
} }
/* 获取父节点索引 */ /* 获取父节点索引 */
int parent(MaxHeap *h, int i) { int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2; return (i - 1) / 2;
} }
``` ```
@ -697,8 +697,8 @@ comments: true
```c title="my_heap.c" ```c title="my_heap.c"
/* 访问堆顶元素 */ /* 访问堆顶元素 */
int peek(MaxHeap *h) { int peek(MaxHeap *maxHeap) {
return h->data[0]; return maxHeap->data[0];
} }
``` ```
@ -1026,31 +1026,31 @@ comments: true
```c title="my_heap.c" ```c title="my_heap.c"
/* 元素入堆 */ /* 元素入堆 */
void push(MaxHeap *h, int val) { void push(MaxHeap *maxHeap, int val) {
// 默认情况下,不应该添加这么多节点 // 默认情况下,不应该添加这么多节点
if (h->size == MAX_SIZE) { if (maxHeap->size == MAX_SIZE) {
printf("heap is full!"); printf("heap is full!");
return; return;
} }
// 添加节点 // 添加节点
h->data[h->size] = val; maxHeap->data[maxHeap->size] = val;
h->size++; maxHeap->size++;
// 从底至顶堆化 // 从底至顶堆化
siftUp(h, h->size - 1); siftUp(maxHeap, maxHeap->size - 1);
} }
/* 从节点 i 开始,从底至顶堆化 */ /* 从节点 i 开始,从底至顶堆化 */
void siftUp(MaxHeap *h, int i) { void siftUp(MaxHeap *maxHeap, int i) {
while (true) { while (true) {
// 获取节点 i 的父节点 // 获取节点 i 的父节点
int p = parent(h, i); int p = parent(maxHeap, i);
// 当“越过根节点”或“节点无须修复”时,结束堆化 // 当“越过根节点”或“节点无须修复”时,结束堆化
if (p < 0 || h->data[i] <= h->data[p]) { if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {
break; break;
} }
// 交换两节点 // 交换两节点
swap(h, i, p); swap(maxHeap, i, p);
// 循环向上堆化 // 循环向上堆化
i = p; i = p;
} }
@ -1519,35 +1519,35 @@ comments: true
```c title="my_heap.c" ```c title="my_heap.c"
/* 元素出堆 */ /* 元素出堆 */
int pop(MaxHeap *h) { int pop(MaxHeap *maxHeap) {
// 判空处理 // 判空处理
if (isEmpty(h)) { if (isEmpty(maxHeap)) {
printf("heap is empty!"); printf("heap is empty!");
return INT_MAX; return INT_MAX;
} }
// 交换根节点与最右叶节点(即交换首元素与尾元素) // 交换根节点与最右叶节点(即交换首元素与尾元素)
swap(h, 0, size(h) - 1); swap(maxHeap, 0, size(maxHeap) - 1);
// 删除节点 // 删除节点
int val = h->data[h->size - 1]; int val = maxHeap->data[maxHeap->size - 1];
h->size--; maxHeap->size--;
// 从顶至底堆化 // 从顶至底堆化
siftDown(h, 0); siftDown(maxHeap, 0);
// 返回堆顶元素 // 返回堆顶元素
return val; return val;
} }
/* 从节点 i 开始,从顶至底堆化 */ /* 从节点 i 开始,从顶至底堆化 */
void siftDown(MaxHeap *h, int i) { void siftDown(MaxHeap *maxHeap, int i) {
while (true) { while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 max // 判断节点 i, l, r 中值最大的节点,记为 max
int l = left(h, i); int l = left(maxHeap, i);
int r = right(h, i); int r = right(maxHeap, i);
int max = i; int max = i;
if (l < size(h) && h->data[l] > h->data[max]) { if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {
max = l; max = l;
} }
if (r < size(h) && h->data[r] > h->data[max]) { if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
max = r; max = r;
} }
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出 // 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
@ -1555,7 +1555,7 @@ comments: true
break; break;
} }
// 交换两节点 // 交换两节点
swap(h, i, max); swap(maxHeap, i, max);
// 循环向下堆化 // 循环向下堆化
i = max; i = max;
} }

View File

@ -408,7 +408,7 @@ comments: true
} }
int *res = getMinHeap(maxHeap); int *res = getMinHeap(maxHeap);
// 释放内存 // 释放内存
freeMaxHeap(maxHeap); delMaxHeap(maxHeap);
return res; return res;
} }
``` ```

View File

@ -3135,7 +3135,7 @@ comments: true
/* 析构函数 */ /* 析构函数 */
void delArrayDeque(ArrayDeque *deque) { void delArrayDeque(ArrayDeque *deque) {
free(deque->nums); free(deque->nums);
deque->queCapacity = 0; free(deque);
} }
/* 获取双向队列的容量 */ /* 获取双向队列的容量 */

View File

@ -1972,7 +1972,7 @@ comments: true
/* 析构函数 */ /* 析构函数 */
void delArrayQueue(ArrayQueue *queue) { void delArrayQueue(ArrayQueue *queue) {
free(queue->nums); free(queue->nums);
queue->queCapacity = 0; free(queue);
} }
/* 获取队列的容量 */ /* 获取队列的容量 */

View File

@ -1581,50 +1581,56 @@ comments: true
/* 构造函数 */ /* 构造函数 */
ArrayStack *newArrayStack() { ArrayStack *newArrayStack() {
ArrayStack *s = malloc(sizeof(ArrayStack)); ArrayStack *stack = malloc(sizeof(ArrayStack));
// 初始化一个大容量,避免扩容 // 初始化一个大容量,避免扩容
s->data = malloc(sizeof(int) * MAX_SIZE); stack->data = malloc(sizeof(int) * MAX_SIZE);
s->size = 0; stack->size = 0;
return s; return stack;
}
/* 析构函数 */
void delArrayStack(ArrayStack *stack) {
free(stack->data);
free(stack);
} }
/* 获取栈的长度 */ /* 获取栈的长度 */
int size(ArrayStack *s) { int size(ArrayStack *stack) {
return s->size; return stack->size;
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
bool isEmpty(ArrayStack *s) { bool isEmpty(ArrayStack *stack) {
return s->size == 0; return stack->size == 0;
} }
/* 入栈 */ /* 入栈 */
void push(ArrayStack *s, int num) { void push(ArrayStack *stack, int num) {
if (s->size == MAX_SIZE) { if (stack->size == MAX_SIZE) {
printf("stack is full.\n"); printf("stack is full.\n");
return; return;
} }
s->data[s->size] = num; stack->data[stack->size] = num;
s->size++; stack->size++;
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek(ArrayStack *s) { int peek(ArrayStack *stack) {
if (s->size == 0) { if (stack->size == 0) {
printf("stack is empty.\n"); printf("stack is empty.\n");
return INT_MAX; return INT_MAX;
} }
return s->data[s->size - 1]; return stack->data[stack->size - 1];
} }
/* 出栈 */ /* 出栈 */
int pop(ArrayStack *s) { int pop(ArrayStack *stack) {
if (s->size == 0) { if (stack->size == 0) {
printf("stack is empty.\n"); printf("stack is empty.\n");
return INT_MAX; return INT_MAX;
} }
int val = peek(s); int val = peek(stack);
s->size--; stack->size--;
return val; return val;
} }
``` ```

View File

@ -1074,7 +1074,7 @@ comments: true
} ArrayBinaryTree; } ArrayBinaryTree;
/* 构造函数 */ /* 构造函数 */
ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) { ArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree)); ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
abt->tree = malloc(sizeof(int) * arrSize); abt->tree = malloc(sizeof(int) * arrSize);
memcpy(abt->tree, arr, sizeof(int) * arrSize); memcpy(abt->tree, arr, sizeof(int) * arrSize);
@ -1082,6 +1082,12 @@ comments: true
return abt; return abt;
} }
/* 析构函数 */
void delArrayBinaryTree(ArrayBinaryTree *abt) {
free(abt->tree);
free(abt);
}
/* 节点数量 */ /* 节点数量 */
int size(ArrayBinaryTree *abt) { int size(ArrayBinaryTree *abt) {
return abt->size; return abt->size;