Add array_binary_tree.c and update push_back and pop_back in vector.h (#664)

* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element.

* fix(codes/cpp): Fix access error when printArray(arr, 0)

* Update PrintUtil.hpp

* fix(codes/c): Fix some errors of cmake build

* feat(codes/c): Add hashing_search.c

* styles(codes/c): Modify function description

* styles(codes/c): Modify binary_search.c code style

* fix(codes/c): Fix the problem in binary_tree_bfs.c and the problem that the memory is not released.

* feat: Add preorder_traversal_i_compact.c

* feat(codes/c): Add head_sort.c

* feat(codes/c): Add bucket_sort.c

* feat(codes/c): Add binary_search_edge.c

* fix(codes/c): Add programs that are not managed by cmake (c code)

* feat(codes/c): Add selection_sort.c

* style(codes/c): Change swap in selection_sort.c to `selectionSort`

* styles(codes/c): Change style.

* fix(codes/c): Fix some formatting errors and temporarily remove backtracking chapters

* fix(codes/c): Fix space_complexity.c build error.

* feat(codes/c): Add array_binary_tree.c

* feat(code/c): Update push_back and pop_back in vector.h

* styles(codes/c): Adjust  format.

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
gonglja 2023-08-03 14:48:10 +08:00 committed by GitHub
parent 70784a1ec3
commit 9900e0c668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 247 additions and 79 deletions

View File

@ -12,9 +12,9 @@ void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
if (state->size == choices->size) {
vector *newState = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(newState, state->data[i]);
vectorPushback(newState, state->data[i], sizeof(int));
}
vectorPushback(res, newState);
vectorPushback(res, newState, sizeof(vector));
return;
}
// 遍历所有选择
@ -26,7 +26,7 @@ void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
if (!select) {
// 尝试:做出选择,更新状态
*((bool *)selected->data[i]) = true;
vectorPushback(state, choice);
vectorPushback(state, choice, sizeof(int));
// 进行下一轮选择
backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
@ -43,7 +43,7 @@ vector *permutationsI(vector *nums) {
int select[3] = {false, false, false};
vector *bSelected = newVector();
for (int i = 0; i < nums->size; i++) {
vectorPushback(bSelected, &select[i]);
vectorPushback(bSelected, &select[i], sizeof(int));
}
vector *res = newVector();
@ -55,8 +55,8 @@ vector *permutationsI(vector *nums) {
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d", node->val);
int *node = p;
printf("%d", *node);
}
/* Driver Code */
@ -64,7 +64,7 @@ int main() {
int nums[] = {1, 2, 3};
vector *iNums = newVector(); // int
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(iNums, &nums[i]);
vectorPushback(iNums, &nums[i], sizeof(int));
}
vector *res = permutationsI(iNums);
@ -76,6 +76,7 @@ int main() {
printVectorMatrix(res, printFunc);
// 释放内存
delVector(iNums);
delVector(res);
return 0;
}

View File

@ -21,7 +21,7 @@ static void preOrder(TreeNode *root) {
}
if (root->val == 7) {
// 记录解
vectorPushback(res, root);
vectorPushback(res, root, sizeof(int));
}
preOrder(root->left);
preOrder(root->right);

View File

@ -12,14 +12,14 @@ void preOrder(TreeNode *root, vector *path, vector *res) {
return;
}
// 尝试
vectorPushback(path, root);
vectorPushback(path, root, sizeof(TreeNode));
if (root->val == 7) {
// 记录解
vector *newPath = newVector();
for (int i = 0; i < path->size; i++) {
vectorPushback(newPath, path->data[i]);
vectorPushback(newPath, path->data[i], sizeof(int));
}
vectorPushback(res, newPath);
vectorPushback(res, newPath, sizeof(vector));
}
preOrder(root->left, path, res);

View File

@ -13,14 +13,14 @@ void preOrder(TreeNode *root, vector *path, vector *res) {
return;
}
// 尝试
vectorPushback(path, root);
vectorPushback(path, root, sizeof(TreeNode));
if (root->val == 7) {
// 记录解
vector *newPath = newVector();
for (int i = 0; i < path->size; i++) {
vectorPushback(newPath, path->data[i]);
vectorPushback(newPath, path->data[i], sizeof(int));
}
vectorPushback(res, newPath);
vectorPushback(res, newPath, sizeof(vector));
res->depth++;
}

View File

@ -15,9 +15,9 @@ bool isSolution(vector *state) {
void recordSolution(vector *state, vector *res) {
vector *newPath = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(newPath, state->data[i]);
vectorPushback(newPath, state->data[i], sizeof(int));
}
vectorPushback(res, newPath);
vectorPushback(res, newPath, sizeof(vector));
}
/* 判断在当前状态下,该选择是否合法 */
@ -27,7 +27,7 @@ bool isValid(vector *state, TreeNode *choice) {
/* 更新状态 */
void makeChoice(vector *state, TreeNode *choice) {
vectorPushback(state, choice);
vectorPushback(state, choice, sizeof(TreeNode));
}
/* 恢复状态 */
@ -52,8 +52,8 @@ void backtrack(vector *state, vector *choices, vector *res) {
makeChoice(state, choice);
// 进行下一轮选择
vector *nextChoices = newVector();
vectorPushback(nextChoices, choice->left);
vectorPushback(nextChoices, choice->right);
vectorPushback(nextChoices, choice->left, sizeof(TreeNode));
vectorPushback(nextChoices, choice->right, sizeof(TreeNode));
backtrack(state, nextChoices, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
@ -79,7 +79,7 @@ int main() {
vector *state = newVector();
vector *choices = newVector();
vector *res = newVector();
vectorPushback(choices, root);
vectorPushback(choices, root, sizeof(TreeNode));
backtrack(state, choices, res);
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
@ -88,7 +88,7 @@ int main() {
vector *vals = newVector();
for (int j = 0; j < path->size; j++) {
TreeNode *node = path->data[j];
vectorPushback(vals, &node->val);
vectorPushback(vals, &node->val, sizeof(int));
}
printVector(vals, printFunc);
}

View File

@ -12,9 +12,9 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
if (target == 0) {
vector *tmpVector = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(tmpVector, state->data[i]);
vectorPushback(tmpVector, state->data[i], sizeof(int));
}
vectorPushback(res, tmpVector);
vectorPushback(res, tmpVector, sizeof(vector));
return;
}
// 遍历所有选择
@ -22,10 +22,10 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
for (int i = start; i < choices->size; i++) {
// 剪枝:若子集和超过 target ,则跳过该选择
if (target - *(int *)(choices->data[i]) < 0) {
continue;
break;
}
// 尝试:做出选择,更新 target, start
vectorPushback(state, choices->data[i]);
vectorPushback(state, choices->data[i], sizeof(int));
// 进行下一轮选择
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res);
// 回退:撤销选择,恢复到之前的状态
@ -41,7 +41,7 @@ int comp(const void *a, const void *b) {
/* 求解子集和 I */
vector *subsetSumI(vector *nums, int target) {
vector *state = newVector(); // 状态(子集)
qsort(nums->data[0], nums->size, sizeof(int), comp); // 对 nums 进行排序
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
@ -50,27 +50,29 @@ vector *subsetSumI(vector *nums, int target) {
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d", node->val);
int *node = p;
printf("%d", *node);
}
/* Driver Code */
int main() {
int nums[] = {3, 4, 5};
vector *vNums = newVector();
vector *iNums = newVector();
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(vNums, &nums[i]);
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9;
vector *res = subsetSumI(vNums, target);
vector *res = subsetSumI(iNums, target);
printf("输入数组 nums = ");
printVector(vNums, printFunc);
printVector(iNums, printFunc);
printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target);
printVectorMatrix(res, printFunc);
delVector(iNums);
delVector(res);
return 0;
}

View File

@ -12,9 +12,9 @@ void backtrack(vector *state, int target, int total, vector *choices, vector *re
if (total == target) {
vector *tmpVector = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(tmpVector, state->data[i]);
vectorPushback(tmpVector, state->data[i], sizeof(int));
}
vectorPushback(res, tmpVector);
vectorPushback(res, tmpVector, sizeof(vector));
return;
}
// 遍历所有选择
@ -24,7 +24,7 @@ void backtrack(vector *state, int target, int total, vector *choices, vector *re
continue;
}
// 尝试:做出选择,更新元素和 total
vectorPushback(state, choices->data[i]);
vectorPushback(state, choices->data[i], sizeof(int));
// 进行下一轮选择
backtrack(state, target, total + *(int *)(choices->data[i]), choices, res);
// 回退:撤销选择,恢复到之前的状态
@ -43,27 +43,28 @@ vector *subsetSumINaive(vector *nums, int target) {
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d", node->val);
int *node = p;
printf("%d", *node);
}
/* Driver Code */
int main() {
int nums[] = {3, 4, 5};
vector *vNums = newVector();
vector *iNums = newVector();
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(vNums, &nums[i]);
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9;
vector *res = subsetSumINaive(vNums, target);
vector *res = subsetSumINaive(iNums, target);
printf("输入数组 nums = ");
printVector(vNums, printFunc);
printVector(iNums, printFunc);
printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target);
printVectorMatrix(res, printFunc);
delVector(iNums);
delVector(res);
return 0;
}

View File

@ -12,9 +12,9 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
if (target == 0) {
vector *tmpVector = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(tmpVector, state->data[i]);
vectorPushback(tmpVector, state->data[i], sizeof(int));
}
vectorPushback(res, tmpVector);
vectorPushback(res, tmpVector, sizeof(vector));
return;
}
// 遍历所有选择
@ -31,7 +31,7 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
continue;
}
// 尝试:做出选择,更新 target, start
vectorPushback(state, choices->data[i]);
vectorPushback(state, choices->data[i], sizeof(int));
// 进行下一轮选择
backtrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);
// 回退:撤销选择,恢复到之前的状态
@ -47,7 +47,7 @@ int comp(const void *a, const void *b) {
/* 求解子集和 II */
vector *subsetSumII(vector *nums, int target) {
vector *state = newVector(); // 状态(子集)
qsort(nums->data[0], nums->size, sizeof(int), comp); // 对 nums 进行排序
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
@ -56,27 +56,28 @@ vector *subsetSumII(vector *nums, int target) {
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d", node->val);
int *node = p;
printf("%d", *node);
}
/* Driver Code */
int main() {
int nums[] = {4, 4, 5};
vector *vNums = newVector();
vector *iNums = newVector();
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(vNums, &nums[i]);
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9;
vector *res = subsetSumII(vNums, target);
vector *res = subsetSumII(iNums, target);
printf("输入数组 nums = ");
printVector(vNums, printFunc);
printVector(iNums, printFunc);
printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target);
printVectorMatrix(res, printFunc);
delVector(iNums);
delVector(res);
return 0;
}

View File

@ -3,3 +3,4 @@ add_executable(binary_tree binary_tree.c)
add_executable(binary_tree_bfs binary_tree_bfs.c)
add_executable(binary_tree_dfs binary_tree_dfs.c)
add_executable(binary_search_tree binary_search_tree.c)
add_executable(array_binary_tree array_binary_tree.c)

View File

@ -0,0 +1,159 @@
/**
* File: array_binary_tree.c
* Created Time: 2023-07-29
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
struct arrayBinaryTree {
vector *tree;
};
typedef struct arrayBinaryTree arrayBinaryTree;
arrayBinaryTree *newArrayBinaryTree(vector *arr) {
arrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));
newABT->tree = arr;
return newABT;
}
/* 节点数量 */
int size(arrayBinaryTree *abt) {
return abt->tree->size;
}
/* 获取索引为 i 节点的值 */
int val(arrayBinaryTree *abt, int i) {
// 若索引越界,则返回 INT_MAX ,代表空位
if (i < 0 || i >= size(abt))
return INT_MAX;
return *(int *)abt->tree->data[i];
}
/* 获取索引为 i 节点的左子节点的索引 */
int left(int i) {
return 2 * i + 1;
}
/* 获取索引为 i 节点的右子节点的索引 */
int right(int i) {
return 2 * i + 2;
}
/* 获取索引为 i 节点的父节点的索引 */
int parent(int i) {
return (i - 1) / 2;
}
/* 深度优先遍历 */
void dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {
// 若为空位,则返回
if (val(abt, i) == INT_MAX)
return;
// 前序遍历
if (strcmp(order, "pre") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
dfs(abt, left(i), order, res);
// 中序遍历
if (strcmp(order, "in") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
dfs(abt, right(i), order, res);
// 后序遍历
if (strcmp(order, "post") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
}
/* 层序遍历 */
vector *levelOrder(arrayBinaryTree *abt) {
vector *res = newVector();
// 直接遍历数组
for (int i = 0; i < size(abt); i++) {
if (val(abt, i) != INT_MAX) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(int));
}
}
return res;
}
/* 前序遍历 */
vector *preOrder(arrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "pre", res);
return res;
}
/* 中序遍历 */
vector *inOrder(arrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "in", res);
return res;
}
/* 后序遍历 */
vector *postOrder(arrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "post", res);
return res;
}
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
int *val = p;
printf("%d", *val);
}
/* Driver Code */
int main() {
// 初始化二叉树
// 使用 INT_MAX 代表空位 nullptr
int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printf("二叉树的数组表示:\n");
printArray(arr, sizeof(arr) / sizeof(arr[0]));
printf("二叉树的链表表示:\n");
printTree(root);
vector *vArr = newVector();
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
vectorPushback(vArr, &arr[i], sizeof(int));
}
// 数组表示下的二叉树类
arrayBinaryTree *abt = newArrayBinaryTree(vArr);
// 访问节点
int i = 1;
int l = left(i), r = right(i), p = parent(i);
printf("\n当前节点的索引为 %d值为 %d\n", i, val(abt, i));
printf("其左子节点的索引为 %d值为 %d\r\n", l, val(abt, l));
printf("其右子节点的索引为 %d值为 %d\r\n", r, val(abt, r));
printf("其父节点的索引为 %d值为 %d\r\n", p, val(abt, p));
// 遍历树
vector *res = levelOrder(abt);
printf("\n层序遍历为: ");
printVector(res, printFunc);
delVector(res);
res = preOrder(abt);
printf("前序遍历为: ");
printVector(res, printFunc);
delVector(res);
res = inOrder(abt);
printf("中序遍历为: ");
printVector(res, printFunc);
delVector(res);
res = postOrder(abt);
printf("后序遍历为: ");
printVector(res, printFunc);
delVector(res);
return 0;
}

View File

@ -48,18 +48,21 @@ void delVector(vector *v) {
}
}
/* 添加元素到向量尾部 */
void vectorPushback(vector *v, void *elem) {
/* 添加元素(拷贝方式)到向量尾部 */
void vectorPushback(vector *v, void *elem, int elemSize) {
if (v->size == v->capacity) {
v->capacity *= 2;
v->data = realloc(v->data, v->capacity * sizeof(void *));
}
v->data[v->size++] = elem;
void *tmp = malloc(sizeof(char) * elemSize);
memcpy(tmp, elem, elemSize);
v->data[v->size++] = tmp;
}
/* 从向量尾部弹出元素 */
void vectorPopback(vector *v) {
if (v->size != 0) {
free(v->data[v->size - 1]);
v->size--;
}
}
@ -98,7 +101,7 @@ void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
for (int i = 0; i < v->size; i++) {
if (i == 0) {
printf("[");
} else if (i == v->size-1) {
} else if (i == v->size - 1) {
printFunc(v, v->data[i]);
printf("]\r\n");
break;
@ -123,7 +126,7 @@ void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
printf(" [");
for (int j = 0; j < v->size; j++) {
printFunc(v, v->data[j]);
if (j != v->size -1)
if (j != v->size - 1)
printf(",");
}
printf("],");