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
11 changed files with 247 additions and 79 deletions
@@ -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;
}
@@ -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);
@@ -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);
@@ -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++;
}
@@ -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);
}
+16 -14
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);
// 回退:撤销选择,恢复到之前的状态
@@ -40,37 +40,39 @@ 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 进行排序
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
vector *state = newVector(); // 状态(子集)
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
int start = 0; // 子集和
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
return res;
}
/* 打印向量中的元素 */
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;
}
@@ -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;
}
+11 -10
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;
}