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:
parent
70784a1ec3
commit
9900e0c668
@ -12,9 +12,9 @@ void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
|
|||||||
if (state->size == choices->size) {
|
if (state->size == choices->size) {
|
||||||
vector *newState = newVector();
|
vector *newState = newVector();
|
||||||
for (int i = 0; i < state->size; i++) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// 遍历所有选择
|
// 遍历所有选择
|
||||||
@ -26,7 +26,7 @@ void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
|
|||||||
if (!select) {
|
if (!select) {
|
||||||
// 尝试:做出选择,更新状态
|
// 尝试:做出选择,更新状态
|
||||||
*((bool *)selected->data[i]) = true;
|
*((bool *)selected->data[i]) = true;
|
||||||
vectorPushback(state, choice);
|
vectorPushback(state, choice, sizeof(int));
|
||||||
// 进行下一轮选择
|
// 进行下一轮选择
|
||||||
backtrack(state, choices, selected, res);
|
backtrack(state, choices, selected, res);
|
||||||
// 回退:撤销选择,恢复到之前的状态
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
@ -43,7 +43,7 @@ vector *permutationsI(vector *nums) {
|
|||||||
int select[3] = {false, false, false};
|
int select[3] = {false, false, false};
|
||||||
vector *bSelected = newVector();
|
vector *bSelected = newVector();
|
||||||
for (int i = 0; i < nums->size; i++) {
|
for (int i = 0; i < nums->size; i++) {
|
||||||
vectorPushback(bSelected, &select[i]);
|
vectorPushback(bSelected, &select[i], sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
vector *res = newVector();
|
vector *res = newVector();
|
||||||
@ -55,8 +55,8 @@ vector *permutationsI(vector *nums) {
|
|||||||
|
|
||||||
/* 打印向量中的元素 */
|
/* 打印向量中的元素 */
|
||||||
void printFunc(vector *v, void *p) {
|
void printFunc(vector *v, void *p) {
|
||||||
TreeNode *node = p;
|
int *node = p;
|
||||||
printf("%d", node->val);
|
printf("%d", *node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
@ -64,7 +64,7 @@ int main() {
|
|||||||
int nums[] = {1, 2, 3};
|
int nums[] = {1, 2, 3};
|
||||||
vector *iNums = newVector(); // int
|
vector *iNums = newVector(); // int
|
||||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
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);
|
vector *res = permutationsI(iNums);
|
||||||
@ -76,6 +76,7 @@ int main() {
|
|||||||
printVectorMatrix(res, printFunc);
|
printVectorMatrix(res, printFunc);
|
||||||
|
|
||||||
// 释放内存
|
// 释放内存
|
||||||
|
delVector(iNums);
|
||||||
delVector(res);
|
delVector(res);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ static void preOrder(TreeNode *root) {
|
|||||||
}
|
}
|
||||||
if (root->val == 7) {
|
if (root->val == 7) {
|
||||||
// 记录解
|
// 记录解
|
||||||
vectorPushback(res, root);
|
vectorPushback(res, root, sizeof(int));
|
||||||
}
|
}
|
||||||
preOrder(root->left);
|
preOrder(root->left);
|
||||||
preOrder(root->right);
|
preOrder(root->right);
|
||||||
|
@ -12,14 +12,14 @@ void preOrder(TreeNode *root, vector *path, vector *res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 尝试
|
// 尝试
|
||||||
vectorPushback(path, root);
|
vectorPushback(path, root, sizeof(TreeNode));
|
||||||
if (root->val == 7) {
|
if (root->val == 7) {
|
||||||
// 记录解
|
// 记录解
|
||||||
vector *newPath = newVector();
|
vector *newPath = newVector();
|
||||||
for (int i = 0; i < path->size; i++) {
|
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);
|
preOrder(root->left, path, res);
|
||||||
|
@ -13,14 +13,14 @@ void preOrder(TreeNode *root, vector *path, vector *res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 尝试
|
// 尝试
|
||||||
vectorPushback(path, root);
|
vectorPushback(path, root, sizeof(TreeNode));
|
||||||
if (root->val == 7) {
|
if (root->val == 7) {
|
||||||
// 记录解
|
// 记录解
|
||||||
vector *newPath = newVector();
|
vector *newPath = newVector();
|
||||||
for (int i = 0; i < path->size; i++) {
|
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++;
|
res->depth++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,9 +15,9 @@ bool isSolution(vector *state) {
|
|||||||
void recordSolution(vector *state, vector *res) {
|
void recordSolution(vector *state, vector *res) {
|
||||||
vector *newPath = newVector();
|
vector *newPath = newVector();
|
||||||
for (int i = 0; i < state->size; i++) {
|
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) {
|
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);
|
makeChoice(state, choice);
|
||||||
// 进行下一轮选择
|
// 进行下一轮选择
|
||||||
vector *nextChoices = newVector();
|
vector *nextChoices = newVector();
|
||||||
vectorPushback(nextChoices, choice->left);
|
vectorPushback(nextChoices, choice->left, sizeof(TreeNode));
|
||||||
vectorPushback(nextChoices, choice->right);
|
vectorPushback(nextChoices, choice->right, sizeof(TreeNode));
|
||||||
backtrack(state, nextChoices, res);
|
backtrack(state, nextChoices, res);
|
||||||
// 回退:撤销选择,恢复到之前的状态
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
undoChoice(state, choice);
|
undoChoice(state, choice);
|
||||||
@ -79,7 +79,7 @@ int main() {
|
|||||||
vector *state = newVector();
|
vector *state = newVector();
|
||||||
vector *choices = newVector();
|
vector *choices = newVector();
|
||||||
vector *res = newVector();
|
vector *res = newVector();
|
||||||
vectorPushback(choices, root);
|
vectorPushback(choices, root, sizeof(TreeNode));
|
||||||
backtrack(state, choices, res);
|
backtrack(state, choices, res);
|
||||||
|
|
||||||
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
|
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
|
||||||
@ -88,7 +88,7 @@ int main() {
|
|||||||
vector *vals = newVector();
|
vector *vals = newVector();
|
||||||
for (int j = 0; j < path->size; j++) {
|
for (int j = 0; j < path->size; j++) {
|
||||||
TreeNode *node = path->data[j];
|
TreeNode *node = path->data[j];
|
||||||
vectorPushback(vals, &node->val);
|
vectorPushback(vals, &node->val, sizeof(int));
|
||||||
}
|
}
|
||||||
printVector(vals, printFunc);
|
printVector(vals, printFunc);
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
|
|||||||
if (target == 0) {
|
if (target == 0) {
|
||||||
vector *tmpVector = newVector();
|
vector *tmpVector = newVector();
|
||||||
for (int i = 0; i < state->size; i++) {
|
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;
|
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++) {
|
for (int i = start; i < choices->size; i++) {
|
||||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||||
if (target - *(int *)(choices->data[i]) < 0) {
|
if (target - *(int *)(choices->data[i]) < 0) {
|
||||||
continue;
|
break;
|
||||||
}
|
}
|
||||||
// 尝试:做出选择,更新 target, start
|
// 尝试:做出选择,更新 target, start
|
||||||
vectorPushback(state, choices->data[i]);
|
vectorPushback(state, choices->data[i], sizeof(int));
|
||||||
// 进行下一轮选择
|
// 进行下一轮选择
|
||||||
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res);
|
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res);
|
||||||
// 回退:撤销选择,恢复到之前的状态
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
@ -40,37 +40,39 @@ int comp(const void *a, const void *b) {
|
|||||||
|
|
||||||
/* 求解子集和 I */
|
/* 求解子集和 I */
|
||||||
vector *subsetSumI(vector *nums, int target) {
|
vector *subsetSumI(vector *nums, int target) {
|
||||||
vector *state = newVector(); // 状态(子集)
|
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; // 子集和
|
int start = 0; // 子集和
|
||||||
vector *res = newVector(); // 结果列表(子集列表)
|
vector *res = newVector(); // 结果列表(子集列表)
|
||||||
backtrack(state, target, nums, start, res);
|
backtrack(state, target, nums, start, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 打印向量中的元素 */
|
/* 打印向量中的元素 */
|
||||||
void printFunc(vector *v, void *p) {
|
void printFunc(vector *v, void *p) {
|
||||||
TreeNode *node = p;
|
int *node = p;
|
||||||
printf("%d", node->val);
|
printf("%d", *node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
int nums[] = {3, 4, 5};
|
int nums[] = {3, 4, 5};
|
||||||
vector *vNums = newVector();
|
vector *iNums = newVector();
|
||||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||||
vectorPushback(vNums, &nums[i]);
|
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
int target = 9;
|
int target = 9;
|
||||||
|
|
||||||
vector *res = subsetSumI(vNums, target);
|
vector *res = subsetSumI(iNums, target);
|
||||||
|
|
||||||
printf("输入数组 nums = ");
|
printf("输入数组 nums = ");
|
||||||
printVector(vNums, printFunc);
|
printVector(iNums, printFunc);
|
||||||
printf("target = %d\n", target);
|
printf("target = %d\n", target);
|
||||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||||
printVectorMatrix(res, printFunc);
|
printVectorMatrix(res, printFunc);
|
||||||
|
|
||||||
|
delVector(iNums);
|
||||||
delVector(res);
|
delVector(res);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ void backtrack(vector *state, int target, int total, vector *choices, vector *re
|
|||||||
if (total == target) {
|
if (total == target) {
|
||||||
vector *tmpVector = newVector();
|
vector *tmpVector = newVector();
|
||||||
for (int i = 0; i < state->size; i++) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// 遍历所有选择
|
// 遍历所有选择
|
||||||
@ -24,7 +24,7 @@ void backtrack(vector *state, int target, int total, vector *choices, vector *re
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 尝试:做出选择,更新元素和 total
|
// 尝试:做出选择,更新元素和 total
|
||||||
vectorPushback(state, choices->data[i]);
|
vectorPushback(state, choices->data[i], sizeof(int));
|
||||||
// 进行下一轮选择
|
// 进行下一轮选择
|
||||||
backtrack(state, target, total + *(int *)(choices->data[i]), choices, res);
|
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) {
|
void printFunc(vector *v, void *p) {
|
||||||
TreeNode *node = p;
|
int *node = p;
|
||||||
printf("%d", node->val);
|
printf("%d", *node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
int nums[] = {3, 4, 5};
|
int nums[] = {3, 4, 5};
|
||||||
vector *vNums = newVector();
|
vector *iNums = newVector();
|
||||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||||
vectorPushback(vNums, &nums[i]);
|
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||||
}
|
}
|
||||||
int target = 9;
|
int target = 9;
|
||||||
|
|
||||||
vector *res = subsetSumINaive(vNums, target);
|
vector *res = subsetSumINaive(iNums, target);
|
||||||
|
|
||||||
printf("输入数组 nums = ");
|
printf("输入数组 nums = ");
|
||||||
printVector(vNums, printFunc);
|
printVector(iNums, printFunc);
|
||||||
printf("target = %d\n", target);
|
printf("target = %d\n", target);
|
||||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||||
printVectorMatrix(res, printFunc);
|
printVectorMatrix(res, printFunc);
|
||||||
|
|
||||||
|
delVector(iNums);
|
||||||
delVector(res);
|
delVector(res);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
|
|||||||
if (target == 0) {
|
if (target == 0) {
|
||||||
vector *tmpVector = newVector();
|
vector *tmpVector = newVector();
|
||||||
for (int i = 0; i < state->size; i++) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// 遍历所有选择
|
// 遍历所有选择
|
||||||
@ -31,7 +31,7 @@ void backtrack(vector *state, int target, vector *choices, int start, vector *re
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 尝试:做出选择,更新 target, start
|
// 尝试:做出选择,更新 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);
|
backtrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);
|
||||||
// 回退:撤销选择,恢复到之前的状态
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
@ -47,7 +47,7 @@ int comp(const void *a, const void *b) {
|
|||||||
/* 求解子集和 II */
|
/* 求解子集和 II */
|
||||||
vector *subsetSumII(vector *nums, int target) {
|
vector *subsetSumII(vector *nums, int target) {
|
||||||
vector *state = newVector(); // 状态(子集)
|
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; // 子集和
|
int start = 0; // 子集和
|
||||||
vector *res = newVector(); // 结果列表(子集列表)
|
vector *res = newVector(); // 结果列表(子集列表)
|
||||||
backtrack(state, target, nums, start, res);
|
backtrack(state, target, nums, start, res);
|
||||||
@ -56,27 +56,28 @@ vector *subsetSumII(vector *nums, int target) {
|
|||||||
|
|
||||||
/* 打印向量中的元素 */
|
/* 打印向量中的元素 */
|
||||||
void printFunc(vector *v, void *p) {
|
void printFunc(vector *v, void *p) {
|
||||||
TreeNode *node = p;
|
int *node = p;
|
||||||
printf("%d", node->val);
|
printf("%d", *node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
int nums[] = {4, 4, 5};
|
int nums[] = {4, 4, 5};
|
||||||
vector *vNums = newVector();
|
vector *iNums = newVector();
|
||||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||||
vectorPushback(vNums, &nums[i]);
|
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||||
}
|
}
|
||||||
int target = 9;
|
int target = 9;
|
||||||
|
|
||||||
vector *res = subsetSumII(vNums, target);
|
vector *res = subsetSumII(iNums, target);
|
||||||
|
|
||||||
printf("输入数组 nums = ");
|
printf("输入数组 nums = ");
|
||||||
printVector(vNums, printFunc);
|
printVector(iNums, printFunc);
|
||||||
printf("target = %d\n", target);
|
printf("target = %d\n", target);
|
||||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||||
printVectorMatrix(res, printFunc);
|
printVectorMatrix(res, printFunc);
|
||||||
|
|
||||||
|
delVector(iNums);
|
||||||
delVector(res);
|
delVector(res);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3,3 +3,4 @@ add_executable(binary_tree binary_tree.c)
|
|||||||
add_executable(binary_tree_bfs binary_tree_bfs.c)
|
add_executable(binary_tree_bfs binary_tree_bfs.c)
|
||||||
add_executable(binary_tree_dfs binary_tree_dfs.c)
|
add_executable(binary_tree_dfs binary_tree_dfs.c)
|
||||||
add_executable(binary_search_tree binary_search_tree.c)
|
add_executable(binary_search_tree binary_search_tree.c)
|
||||||
|
add_executable(array_binary_tree array_binary_tree.c)
|
||||||
|
159
codes/c/chapter_tree/array_binary_tree.c
Normal file
159
codes/c/chapter_tree/array_binary_tree.c
Normal 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;
|
||||||
|
}
|
@ -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) {
|
if (v->size == v->capacity) {
|
||||||
v->capacity *= 2;
|
v->capacity *= 2;
|
||||||
v->data = realloc(v->data, v->capacity * sizeof(void *));
|
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) {
|
void vectorPopback(vector *v) {
|
||||||
if (v->size != 0) {
|
if (v->size != 0) {
|
||||||
|
free(v->data[v->size - 1]);
|
||||||
v->size--;
|
v->size--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,15 +99,15 @@ void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
|
|||||||
return;
|
return;
|
||||||
} else if (v->depth == 1) {
|
} else if (v->depth == 1) {
|
||||||
for (int i = 0; i < v->size; i++) {
|
for (int i = 0; i < v->size; i++) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
printf("[");
|
printf("[");
|
||||||
} else if (i == v->size-1) {
|
} else if (i == v->size - 1) {
|
||||||
printFunc(v, v->data[i]);
|
printFunc(v, v->data[i]);
|
||||||
printf("]\r\n");
|
printf("]\r\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printFunc(v, v->data[i]);
|
printFunc(v, v->data[i]);
|
||||||
printf(",");
|
printf(",");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < v->size; i++) {
|
for (int i = 0; i < v->size; i++) {
|
||||||
@ -117,19 +120,19 @@ void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
|
|||||||
|
|
||||||
/* 当前仅支持打印深度为 2 的 vector */
|
/* 当前仅支持打印深度为 2 的 vector */
|
||||||
void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
|
void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
|
||||||
printf("[\n");
|
printf("[\n");
|
||||||
for (int i = 0; i < vv->size; i++) {
|
for (int i = 0; i < vv->size; i++) {
|
||||||
vector *v = (vector *)vv->data[i];
|
vector *v = (vector *)vv->data[i];
|
||||||
printf(" [");
|
printf(" [");
|
||||||
for (int j = 0; j < v->size; j++) {
|
for (int j = 0; j < v->size; j++) {
|
||||||
printFunc(v, v->data[j]);
|
printFunc(v, v->data[j]);
|
||||||
if (j != v->size -1)
|
if (j != v->size - 1)
|
||||||
printf(",");
|
printf(",");
|
||||||
}
|
}
|
||||||
printf("],");
|
printf("],");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
Loading…
x
Reference in New Issue
Block a user