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

View file

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

View file

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

View file

@ -1165,7 +1165,7 @@ comments: true
} HashMapChaining;
/* 构造函数 */
HashMapChaining *initHashMapChaining() {
HashMapChaining *newHashMapChaining() {
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
hashMap->size = 0;
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++) {
Node *cur = hashMap->buckets[i];
while (cur) {
Node *temp = cur;
Node *tmp = cur;
cur = cur->next;
free(temp->pair);
free(temp);
free(tmp->pair);
free(tmp);
}
}
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);
Node *cur = hashMap->buckets[index];
Node *pre = NULL;

View file

@ -1408,116 +1408,118 @@ index = hash(key) % capacity
/* 基于数组简易实现的哈希表 */
typedef struct {
Pair *buckets[HASH_MAP_DEFAULT_SIZE];
Pair *buckets[HASHTABLE_CAPACITY];
} ArrayHashMap;
/* 哈希表初始化函数 */
/* 构造函数 */
ArrayHashMap *newArrayHashMap() {
ArrayHashMap *map = malloc(sizeof(ArrayHashMap));
return map;
ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
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->key = key;
Pair->val = malloc(strlen(val) + 1);
strcpy(Pair->val, val);
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);
free(d->buckets[index]->val);
free(d->buckets[index]);
d->buckets[index] = NULL;
free(hmap->buckets[index]->val);
free(hmap->buckets[index]);
hmap->buckets[index] = NULL;
}
/* 获取所有键值对 */
void pairSet(ArrayHashMap *d, MapSet *set) {
void pairSet(ArrayHashMap *hmap, MapSet *set) {
Pair *entries;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
entries = malloc(sizeof(Pair) * total);
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
entries[index].key = d->buckets[i]->key;
entries[index].val = malloc(strlen(d->buckets[i]->val + 1));
strcpy(entries[index].val, d->buckets[i]->val);
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
entries[index].key = hmap->buckets[i]->key;
entries[index].val = malloc(strlen(hmap->buckets[i]->val + 1));
strcpy(entries[index].val, hmap->buckets[i]->val);
index++;
}
}
set->set = entries;
set->len = total;
}
/* 获取所有键 */
void keySet(ArrayHashMap *d, MapSet *set) {
void keySet(ArrayHashMap *hmap, MapSet *set) {
int *keys;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
keys = malloc(total * sizeof(int));
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
keys[index] = d->buckets[i]->key;
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
keys[index] = hmap->buckets[i]->key;
index++;
}
}
set->set = keys;
set->len = total;
}
/* 获取所有值 */
void valueSet(ArrayHashMap *d, MapSet *set) {
void valueSet(ArrayHashMap *hmap, MapSet *set) {
char **vals;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
vals = malloc(total * sizeof(char *));
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
vals[index] = d->buckets[i]->val;
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
vals[index] = hmap->buckets[i]->val;
index++;
}
}
set->set = vals;
set->len = total;
}
/* 打印哈希表 */
void print(ArrayHashMap *d) {
void print(ArrayHashMap *hmap) {
int i;
MapSet set;
pairSet(d, &set);
pairSet(hmap, &set);
Pair *entries = (Pair *)set.set;
for (i = 0; i < set.len; i++) {
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 *h = (MaxHeap *)malloc(sizeof(MaxHeap));
h->size = size;
memcpy(h->data, nums, size * sizeof(int));
for (int i = parent(h, size - 1); i >= 0; i--) {
MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));
maxHeap->size = size;
memcpy(maxHeap->data, nums, size * sizeof(int));
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"
/* 获取左子节点索引 */
int left(MaxHeap *h, int i) {
int left(MaxHeap *maxHeap, int i) {
return 2 * i + 1;
}
/* 获取右子节点索引 */
int right(MaxHeap *h, int i) {
int right(MaxHeap *maxHeap, int i) {
return 2 * i + 2;
}
/* 获取父节点索引 */
int parent(MaxHeap *h, int i) {
int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2;
}
```
@ -697,8 +697,8 @@ comments: true
```c title="my_heap.c"
/* 访问堆顶元素 */
int peek(MaxHeap *h) {
return h->data[0];
int peek(MaxHeap *maxHeap) {
return maxHeap->data[0];
}
```
@ -1026,31 +1026,31 @@ comments: true
```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!");
return;
}
// 添加节点
h->data[h->size] = val;
h->size++;
maxHeap->data[maxHeap->size] = val;
maxHeap->size++;
// 从底至顶堆化
siftUp(h, h->size - 1);
siftUp(maxHeap, maxHeap->size - 1);
}
/* 从节点 i 开始,从底至顶堆化 */
void siftUp(MaxHeap *h, int i) {
void siftUp(MaxHeap *maxHeap, int i) {
while (true) {
// 获取节点 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;
}
// 交换两节点
swap(h, i, p);
swap(maxHeap, i, p);
// 循环向上堆化
i = p;
}
@ -1519,35 +1519,35 @@ comments: true
```c title="my_heap.c"
/* 元素出堆 */
int pop(MaxHeap *h) {
int pop(MaxHeap *maxHeap) {
// 判空处理
if (isEmpty(h)) {
if (isEmpty(maxHeap)) {
printf("heap is empty!");
return INT_MAX;
}
// 交换根节点与最右叶节点(即交换首元素与尾元素)
swap(h, 0, size(h) - 1);
swap(maxHeap, 0, size(maxHeap) - 1);
// 删除节点
int val = h->data[h->size - 1];
h->size--;
int val = maxHeap->data[maxHeap->size - 1];
maxHeap->size--;
// 从顶至底堆化
siftDown(h, 0);
siftDown(maxHeap, 0);
// 返回堆顶元素
return val;
}
/* 从节点 i 开始,从顶至底堆化 */
void siftDown(MaxHeap *h, int i) {
void siftDown(MaxHeap *maxHeap, int i) {
while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 max
int l = left(h, i);
int r = right(h, i);
int l = left(maxHeap, i);
int r = right(maxHeap, 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;
}
if (r < size(h) && h->data[r] > h->data[max]) {
if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
max = r;
}
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
@ -1555,7 +1555,7 @@ comments: true
break;
}
// 交换两节点
swap(h, i, max);
swap(maxHeap, i, max);
// 循环向下堆化
i = max;
}

View file

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

View file

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

View file

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

View file

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

View file

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