refactor: Replace vector with array in C code (#894)

* Re-implement merge sort function.

* Replace vector with array for C.

* fix
This commit is contained in:
Yudong Jin
2023-10-27 23:26:48 +08:00
committed by GitHub
parent 5385057993
commit 492a69ebca
8 changed files with 307 additions and 325 deletions
@@ -6,22 +6,20 @@
#include "../utils/common.h" #include "../utils/common.h"
vector *res; // 假设结果长度不超过 100
#define MAX_SIZE 100
// 打印向量中的元素 TreeNode *res[MAX_SIZE];
void printFunc(vector *v, void *p) { int resSize = 0;
TreeNode *node = p;
printf("%d ", node->val);
}
/* 前序遍历:例题一 */ /* 前序遍历:例题一 */
void preOrder(TreeNode *root) { static void preOrder(TreeNode *root) {
if (root == NULL) { if (root == NULL) {
return; return;
} }
if (root->val == 7) { if (root->val == 7) {
// 记录解 // 记录解
vectorPushback(res, root, sizeof(int)); res[resSize++] = root;
} }
preOrder(root->left); preOrder(root->left);
preOrder(root->right); preOrder(root->right);
@@ -30,15 +28,21 @@ void preOrder(TreeNode *root) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7}; int arr[] = {1, 7, 3, 4, 5, 6, 7};
res = newVector(); TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0])); printf("\n初始化二叉树\n");
printf("\n初始化二叉树\r\n");
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
preOrder(root); preOrder(root);
printf("\n输出所有值为 7 的节点\r\n"); printf("\n输出所有值为 7 的节点\n");
printVector(res, printFunc); int vals[resSize];
delVector(res); for (int i = 0; i < resSize; i++) {
vals[i] = res[i]->val;
}
printArray(vals, resSize);
// 释放内存
freeMemoryTree(root);
return 0;
} }
@@ -6,62 +6,55 @@
#include "../utils/common.h" #include "../utils/common.h"
// 假设路径和结果长度不超过 100
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
TreeNode *path[MAX_SIZE];
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
int pathSize = 0, resSize = 0;
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
void preOrder(TreeNode *root, vector *path, vector *res) { static void preOrder(TreeNode *root) {
if (root == NULL) { if (root == NULL) {
return; return;
} }
// 尝试 // 尝试
vectorPushback(path, root, sizeof(TreeNode)); path[pathSize++] = root;
if (root->val == 7) { if (root->val == 7) {
// 记录解 // 记录解
vector *newPath = newVector(); for (int i = 0; i < pathSize; ++i) {
for (int i = 0; i < path->size; i++) { res[resSize][i] = path[i];
vectorPushback(newPath, path->data[i], sizeof(int));
} }
vectorPushback(res, newPath, sizeof(vector)); resSize++;
} }
preOrder(root->left);
preOrder(root->left, path, res); preOrder(root->right);
preOrder(root->right, path, res);
// 回退 // 回退
vectorPopback(path); pathSize--;
}
// 打印向量中的元素
void printResult(vector *vv) {
for (int i = 0; i < vv->size; i++) {
vector *v = (vector *)vv->data[i];
for (int j = 0; j < v->size; j++) {
TreeNode *node = (TreeNode *)v->data[j];
printf("%d ", node->val);
}
printf("\n");
}
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7}; int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]); TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
TreeNode *root = arrToTree(arr, n); printf("\n初始化二叉树\n");
printf("\r\n初始化二叉树\r\n");
printTree(root); printTree(root);
// 创建存储路径和结果的向量
vector *path = newVector();
vector *res = newVector();
// 前序遍历 // 前序遍历
preOrder(root, path, res); preOrder(root);
// 输出结果 printf("\n输出所有根节点到节点 7 的路径\n");
printf("输出所有根节点到节点 7 的路径:\n"); for (int i = 0; i < resSize; ++i) {
printResult(res); int vals[MAX_SIZE];
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printArray(vals, size);
}
// 释放内存 // 释放内存
delVector(path); freeMemoryTree(root);
delVector(res);
return 0; return 0;
} }
@@ -6,64 +6,56 @@
#include "../utils/common.h" #include "../utils/common.h"
// 假设路径和结果长度不超过 100
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
TreeNode *path[MAX_SIZE];
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
int pathSize = 0, resSize = 0;
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
void preOrder(TreeNode *root, vector *path, vector *res) { void preOrder(TreeNode *root) {
// 剪枝 // 剪枝
if (root == NULL || root->val == 3) { if (root == NULL || root->val == 3) {
return; return;
} }
// 尝试 // 尝试
vectorPushback(path, root, sizeof(TreeNode)); path[pathSize++] = root;
if (root->val == 7) { if (root->val == 7) {
// 记录解 // 记录解
vector *newPath = newVector(); for (int i = 0; i < pathSize; i++) {
for (int i = 0; i < path->size; i++) { res[resSize][i] = path[i];
vectorPushback(newPath, path->data[i], sizeof(int));
} }
vectorPushback(res, newPath, sizeof(vector)); resSize++;
res->depth++;
} }
preOrder(root->left);
preOrder(root->left, path, res); preOrder(root->right);
preOrder(root->right, path, res);
// 回退 // 回退
vectorPopback(path); pathSize--;
}
// 打印向量中的元素
void printResult(vector *vv) {
for (int i = 0; i < vv->size; i++) {
vector *v = (vector *)vv->data[i];
for (int j = 0; j < v->size; j++) {
TreeNode *node = (TreeNode *)v->data[j];
printf("%d ", node->val);
}
printf("\n");
}
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7}; int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]); TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
TreeNode *root = arrToTree(arr, n); printf("\n初始化二叉树\n");
printf("\r\n初始化二叉树\r\n");
printTree(root); printTree(root);
// 创建存储路径和结果的向量
vector *path = newVector();
vector *res = newVector();
// 前序遍历 // 前序遍历
preOrder(root, path, res); preOrder(root);
// 输出结果 printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n"); for (int i = 0; i < resSize; ++i) {
printResult(res); int vals[MAX_SIZE];
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printArray(vals, size);
}
// 释放内存 // 释放内存
delVector(path); freeMemoryTree(root);
delVector(res);
return 0; return 0;
} }
@@ -6,96 +6,87 @@
#include "../utils/common.h" #include "../utils/common.h"
// 假设路径和结果长度不超过 100
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
TreeNode *path[MAX_SIZE];
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
int pathSize = 0, resSize = 0;
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
bool isSolution(vector *state) { bool isSolution(void) {
return state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7; return pathSize > 0 && path[pathSize - 1]->val == 7;
} }
/* 记录解 */ /* 记录解 */
void recordSolution(vector *state, vector *res) { void recordSolution(void) {
vector *newPath = newVector(); for (int i = 0; i < pathSize; i++) {
for (int i = 0; i < state->size; i++) { res[resSize][i] = path[i];
vectorPushback(newPath, state->data[i], sizeof(int));
} }
vectorPushback(res, newPath, sizeof(vector)); resSize++;
} }
/* 判断在当前状态下,该选择是否合法 */ /* 判断在当前状态下,该选择是否合法 */
bool isValid(vector *state, TreeNode *choice) { bool isValid(TreeNode *choice) {
return choice != NULL && choice->val != 3; return choice != NULL && choice->val != 3;
} }
/* 更新状态 */ /* 更新状态 */
void makeChoice(vector *state, TreeNode *choice) { void makeChoice(TreeNode *choice) {
vectorPushback(state, choice, sizeof(TreeNode)); path[pathSize++] = choice;
} }
/* 恢复状态 */ /* 恢复状态 */
void undoChoice(vector *state, TreeNode *choice) { void undoChoice(void) {
vectorPopback(state); pathSize--;
} }
/* 回溯算法:例题三 */ /* 回溯算法:例题三 */
void backtrack(vector *state, vector *choices, vector *res) { void backtrack(TreeNode *choices[2]) {
// 检查是否为解 // 检查是否为解
if (isSolution(state)) { if (isSolution()) {
// 记录解 // 记录解
recordSolution(state, res); recordSolution();
return;
} }
// 遍历所有选择 // 遍历所有选择
for (int i = 0; i < choices->size; i++) { for (int i = 0; i < 2; i++) {
TreeNode *choice = choices->data[i]; TreeNode *choice = choices[i];
// 剪枝:检查选择是否合法 // 剪枝:检查选择是否合法
if (isValid(state, choice)) { if (isValid(choice)) {
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
makeChoice(state, choice); makeChoice(choice);
// 进行下一轮选择 // 进行下一轮选择
vector *nextChoices = newVector(); TreeNode *nextChoices[2] = {choice->left, choice->right};
vectorPushback(nextChoices, choice->left, sizeof(TreeNode)); backtrack(nextChoices);
vectorPushback(nextChoices, choice->right, sizeof(TreeNode));
backtrack(state, nextChoices, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice); undoChoice();
} }
} }
} }
// 打印向量中的元素
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d ", node->val);
}
/* Driver Code */ /* Driver Code */
int main() { int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7}; int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]); TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
TreeNode *root = arrToTree(arr, n); printf("\n初始化二叉树\n");
printf("\r\n初始化二叉树\r\n");
printTree(root); printTree(root);
// 回溯算法 // 回溯算法
vector *state = newVector(); TreeNode *choices[2] = {root, NULL};
vector *choices = newVector(); backtrack(choices);
vector *res = newVector();
vectorPushback(choices, root, sizeof(TreeNode));
backtrack(state, choices, res);
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n"); printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
for (int i = 0; i < res->size; i++) { for (int i = 0; i < resSize; ++i) {
vector *path = res->data[i]; int vals[MAX_SIZE];
vector *vals = newVector(); int size = 0;
for (int j = 0; j < path->size; j++) { for (int j = 0; res[i][j] != NULL; ++j) {
TreeNode *node = path->data[j]; vals[size++] = res[i][j]->val;
vectorPushback(vals, &node->val, sizeof(int));
} }
printVector(vals, printFunc); printArray(vals, size);
} }
// 释放内存 // 释放内存
delVector(state); freeMemoryTree(root);
delVector(choices);
delVector(res);
return 0; return 0;
} }
+38 -38
View File
@@ -6,73 +6,73 @@
#include "../utils/common.h" #include "../utils/common.h"
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
// 状态(子集)
int state[MAX_SIZE];
int stateSize = 0;
// 结果列表(子集列表)
int res[MAX_RES_SIZE][MAX_SIZE];
int resColSizes[MAX_RES_SIZE];
int resSize = 0;
/* 回溯算法:子集和 I */ /* 回溯算法:子集和 I */
void backtrack(vector *state, int target, vector *choices, int start, vector *res) { void backtrack(int target, int *choices, int choicesSize, int start) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (target == 0) { if (target == 0) {
vector *tmpVector = newVector(); for (int i = 0; i < stateSize; ++i) {
for (int i = 0; i < state->size; i++) { res[resSize][i] = state[i];
vectorPushback(tmpVector, state->data[i], sizeof(int));
} }
vectorPushback(res, tmpVector, sizeof(vector)); resColSizes[resSize++] = stateSize;
return; return;
} }
// 遍历所有选择 // 遍历所有选择
// 剪枝二:从 start 开始遍历,避免生成重复子集 // 剪枝二:从 start 开始遍历,避免生成重复子集
for (int i = start; i < choices->size; i++) { for (int i = start; i < choicesSize; i++) {
// 剪枝:若子集和超过 target ,则跳过该选择 // 剪枝:若子集和超过 target ,则直接结束循环
if (target - *(int *)(choices->data[i]) < 0) { // 这是因为数组已排序,后边元素更大,子集和一定超过 target
if (target - choices[i] < 0) {
break; break;
} }
// 尝试:做出选择,更新 target, start // 尝试:做出选择,更新 target, start
vectorPushback(state, choices->data[i], sizeof(int)); state[stateSize] = choices[i];
stateSize++;
// 进行下一轮选择 // 进行下一轮选择
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res); backtrack(target - choices[i], choices, choicesSize, i);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
vectorPopback(state); stateSize--;
} }
} }
/* 用来做比较函数 */ /* 比较函数 */
int comp(const void *a, const void *b) { int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b; return (*(int *)a - *(int *)b);
} }
/* 求解子集和 I */ /* 求解子集和 I */
vector *subsetSumI(vector *nums, int target) { void subsetSumI(int *nums, int numsSize, int target) {
vector *state = newVector(); // 状态(子集) qsort(nums, numsSize, sizeof(int), cmp); // 对 nums 进行排序
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序 int start = 0; // 遍历起始点
int start = 0; // 子集和 backtrack(target, nums, numsSize, start);
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
return res;
}
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
int *node = p;
printf("%d", *node);
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int nums[] = {3, 4, 5}; int nums[] = {3, 4, 5};
vector *iNums = newVector(); int numsSize = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9; int target = 9;
vector *res = subsetSumI(iNums, target); subsetSumI(nums, numsSize, target);
printf("输入数组 nums = "); printf("输入数组 nums = ");
printVector(iNums, printFunc); printArray(nums, numsSize);
printf("target = %d\n", target); printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target); printf("所有和等于 %d 的子集 res = \n", target);
printVectorMatrix(res, printFunc); for (int i = 0; i < resSize; ++i) {
printArray(res[i], resColSizes[i]);
}
delVector(iNums);
delVector(res);
return 0; return 0;
} }
@@ -6,65 +6,64 @@
#include "../utils/common.h" #include "../utils/common.h"
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
// 状态(子集)
int state[MAX_SIZE];
int stateSize = 0;
// 结果列表(子集列表)
int res[MAX_RES_SIZE][MAX_SIZE];
int resColSizes[MAX_RES_SIZE];
int resSize = 0;
/* 回溯算法:子集和 I */ /* 回溯算法:子集和 I */
void backtrack(vector *state, int target, int total, vector *choices, vector *res) { void backtrack(int target, int total, int *choices, int choicesSize) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (total == target) { if (total == target) {
vector *tmpVector = newVector(); for (int i = 0; i < stateSize; i++) {
for (int i = 0; i < state->size; i++) { res[resSize][i] = state[i];
vectorPushback(tmpVector, state->data[i], sizeof(int));
} }
vectorPushback(res, tmpVector, sizeof(vector)); resColSizes[resSize++] = stateSize;
return; return;
} }
// 遍历所有选择 // 遍历所有选择
for (size_t i = 0; i < choices->size; i++) { for (int i = 0; i < choicesSize; i++) {
// 剪枝:若子集和超过 target ,则跳过该选择 // 剪枝:若子集和超过 target ,则跳过该选择
if (total + *(int *)(choices->data[i]) > target) { if (total + choices[i] > target) {
continue; continue;
} }
// 尝试:做出选择,更新元素和 total // 尝试:做出选择,更新元素和 total
vectorPushback(state, choices->data[i], sizeof(int)); state[stateSize++] = choices[i];
// 进行下一轮选择 // 进行下一轮选择
backtrack(state, target, total + *(int *)(choices->data[i]), choices, res); backtrack(target, total + choices[i], choices, choicesSize);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
vectorPopback(state); stateSize--;
} }
} }
/* 求解子集和 I(包含重复子集) */ /* 求解子集和 I(包含重复子集) */
vector *subsetSumINaive(vector *nums, int target) { void subsetSumINaive(int *nums, int numsSize, int target) {
vector *state = newVector(); // 状态(子集) resSize = 0; // 初始化解的数量为0
int total = 0; // 子集和 backtrack(target, 0, nums, numsSize);
vector *res = newVector(); // 结果列表(子集列表)
backtrack(state, target, total, nums, res);
return res;
}
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
int *node = p;
printf("%d", *node);
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int nums[] = {3, 4, 5}; int nums[] = {3, 4, 5};
vector *iNums = newVector(); int numsSize = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9; int target = 9;
vector *res = subsetSumINaive(iNums, target); subsetSumINaive(nums, numsSize, target);
printf("输入数组 nums = "); printf("输入数组 nums = ");
printVector(iNums, printFunc); printArray(nums, numsSize);
printf("target = %d\n", target); printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target); printf("所有和等于 %d 的子集 res = \n", target);
printVectorMatrix(res, printFunc); for (int i = 0; i < resSize; i++) {
printArray(res[i], resColSizes[i]);
}
delVector(iNums);
delVector(res);
return 0; return 0;
} }
+39 -39
View File
@@ -6,78 +6,78 @@
#include "../utils/common.h" #include "../utils/common.h"
#define MAX_SIZE 100
#define MAX_RES_SIZE 100
// 状态(子集)
int state[MAX_SIZE];
int stateSize = 0;
// 结果列表(子集列表)
int res[MAX_RES_SIZE][MAX_SIZE];
int resColSizes[MAX_RES_SIZE];
int resSize = 0;
/* 回溯算法:子集和 II */ /* 回溯算法:子集和 II */
void backtrack(vector *state, int target, vector *choices, int start, vector *res) { void backtrack(int target, int *choices, int choicesSize, int start) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (target == 0) { if (target == 0) {
vector *tmpVector = newVector(); for (int i = 0; i < stateSize; i++) {
for (int i = 0; i < state->size; i++) { res[resSize][i] = state[i];
vectorPushback(tmpVector, state->data[i], sizeof(int));
} }
vectorPushback(res, tmpVector, sizeof(vector)); resColSizes[resSize++] = stateSize;
return; return;
} }
// 遍历所有选择 // 遍历所有选择
// 剪枝二:从 start 开始遍历,避免生成重复子集 // 剪枝二:从 start 开始遍历,避免生成重复子集
// 剪枝三:从 start 开始遍历,避免重复选择同一元素 // 剪枝三:从 start 开始遍历,避免重复选择同一元素
for (int i = start; i < choices->size; i++) { for (int i = start; i < choicesSize; i++) {
// 剪枝一:若子集和超过 target ,则直接结束循环 // 剪枝一:若子集和超过 target ,则直接跳过
// 这是因为数组已排序,后边元素更大,子集和一定超过 target if (target - choices[i] < 0) {
if (target - *(int *)(choices->data[i]) < 0) {
continue; continue;
} }
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过 // 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
if (i > start && *(int *)(choices->data[i]) == *(int *)(choices->data[i - 1])) { if (i > start && choices[i] == choices[i - 1]) {
continue; continue;
} }
// 尝试:做出选择,更新 target, start // 尝试:做出选择,更新 target, start
vectorPushback(state, choices->data[i], sizeof(int)); state[stateSize] = choices[i];
stateSize++;
// 进行下一轮选择 // 进行下一轮选择
backtrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res); backtrack(target - choices[i], choices, choicesSize, i + 1);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
vectorPopback(state); stateSize--;
} }
} }
/* 比较规则 */ /* 比较函数 */
int comp(const void *a, const void *b) { int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b; return (*(int *)a - *(int *)b);
} }
/* 求解子集和 II */ /* 求解子集和 II */
vector *subsetSumII(vector *nums, int target) { void subsetSumII(int *nums, int numsSize, int target) {
vector *state = newVector(); // 状态(子集) // 对 nums 进行排序
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序 qsort(nums, numsSize, sizeof(int), cmp);
int start = 0; // 子集和 // 开始回溯
vector *res = newVector(); // 结果列表(子集列表) backtrack(target, nums, numsSize, 0);
backtrack(state, target, nums, start, res);
return res;
}
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
int *node = p;
printf("%d", *node);
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int nums[] = {4, 4, 5}; int nums[] = {4, 4, 5};
vector *iNums = newVector(); int numsSize = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
vectorPushback(iNums, &nums[i], sizeof(int));
}
int target = 9; int target = 9;
vector *res = subsetSumII(iNums, target); subsetSumII(nums, numsSize, target);
printf("输入数组 nums = "); printf("输入数组 nums = ");
printVector(iNums, printFunc); printArray(nums, numsSize);
printf("target = %d\n", target); printf("target = %d\n", target);
printf("所有和等于 %d 的子集 res = \r\n", target); printf("所有和等于 %d 的子集 res = \n", target);
printVectorMatrix(res, printFunc); for (int i = 0; i < resSize; ++i) {
printArray(res[i], resColSizes[i]);
}
delVector(iNums);
delVector(res);
return 0; return 0;
} }
+80 -77
View File
@@ -6,21 +6,24 @@
#include "../utils/common.h" #include "../utils/common.h"
/* 数组表示下的二叉树 */ /* 数组表示下的二叉树结构 */
typedef struct { typedef struct {
vector *tree; int *tree;
int size;
} ArrayBinaryTree; } ArrayBinaryTree;
/* 构造函数 */ /* 构造方法 */
ArrayBinaryTree *newArrayBinaryTree(vector *arr) { ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) {
ArrayBinaryTree *newABT = malloc(sizeof(ArrayBinaryTree)); ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
newABT->tree = arr; abt->tree = malloc(sizeof(int) * arrSize);
return newABT; memcpy(abt->tree, arr, sizeof(int) * arrSize);
abt->size = arrSize;
return abt;
} }
/* 节点数量 */ /* 节点数量 */
int size(ArrayBinaryTree *abt) { int size(ArrayBinaryTree *abt) {
return abt->tree->size; return abt->size;
} }
/* 获取索引为 i 节点的值 */ /* 获取索引为 i 节点的值 */
@@ -28,7 +31,7 @@ int val(ArrayBinaryTree *abt, int i) {
// 若索引越界,则返回 INT_MAX ,代表空位 // 若索引越界,则返回 INT_MAX ,代表空位
if (i < 0 || i >= size(abt)) if (i < 0 || i >= size(abt))
return INT_MAX; return INT_MAX;
return *(int *)abt->tree->data[i]; return abt->tree[i];
} }
/* 获取索引为 i 节点的左子节点的索引 */ /* 获取索引为 i 节点的左子节点的索引 */
@@ -46,114 +49,114 @@ int parent(int i) {
return (i - 1) / 2; return (i - 1) / 2;
} }
/* 层序遍历 */
int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
// 直接遍历数组
for (int i = 0; i < size(abt); i++) {
if (val(abt, i) != INT_MAX)
res[index++] = val(abt, i);
}
*returnSize = index;
return res;
}
/* 深度优先遍历 */ /* 深度优先遍历 */
void dfs(ArrayBinaryTree *abt, int i, const char *order, vector *res) { void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
// 若为空位,则返回 // 若为空位,则返回
if (val(abt, i) == INT_MAX) if (val(abt, i) == INT_MAX)
return; return;
// 前序遍历 // 前序遍历
if (strcmp(order, "pre") == 0) { if (strcmp(order, "pre") == 0)
int tmp = val(abt, i); res[(*index)++] = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp)); dfs(abt, left(i), order, res, index);
}
dfs(abt, left(i), order, res);
// 中序遍历 // 中序遍历
if (strcmp(order, "in") == 0) { if (strcmp(order, "in") == 0)
int tmp = val(abt, i); res[(*index)++] = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp)); dfs(abt, right(i), order, res, index);
}
dfs(abt, right(i), order, res);
// 后序遍历 // 后序遍历
if (strcmp(order, "post") == 0) { if (strcmp(order, "post") == 0)
int tmp = val(abt, i); res[(*index)++] = 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) { int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
vector *res = newVector(); int *res = (int *)malloc(sizeof(int) * size(abt));
dfs(abt, 0, "pre", res); int index = 0;
dfs(abt, 0, "pre", res, &index);
*returnSize = index;
return res; return res;
} }
/* 中序遍历 */ /* 中序遍历 */
vector *inOrder(ArrayBinaryTree *abt) { int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
vector *res = newVector(); int *res = (int *)malloc(sizeof(int) * size(abt));
dfs(abt, 0, "in", res); int index = 0;
dfs(abt, 0, "in", res, &index);
*returnSize = index;
return res; return res;
} }
/* 后序遍历 */ /* 后序遍历 */
vector *postOrder(ArrayBinaryTree *abt) { int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
vector *res = newVector(); int *res = (int *)malloc(sizeof(int) * size(abt));
dfs(abt, 0, "post", res); int index = 0;
dfs(abt, 0, "post", res, &index);
*returnSize = index;
return res; return res;
} }
/* 打印向量中的元素 */
void printFunc(vector *v, void *p) {
int *val = p;
printf("%d", *val);
}
/* Driver Code */ /* Driver Code */
int main() { int main() {
// 初始化二叉树 // 初始化二叉树
// 使用 INT_MAX 代表空位 nullptr // 使用 INT_MAX 代表空位 NULL
int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; 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])); int arrSize = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrayToTree(arr, arrSize);
printf("\n初始化二叉树\n"); printf("\n初始化二叉树\n");
printf("二叉树的数组表示:\n"); printf("二叉树的数组表示:\n");
printArray(arr, sizeof(arr) / sizeof(arr[0])); printArray(arr, arrSize);
printf("二叉树的链表表示:\n"); printf("二叉树的链表表示:\n");
printTree(root); printTree(root);
vector *vArr = newVector(); ArrayBinaryTree *abt = createArrayBinaryTree(arr, arrSize);
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 i = 1;
int l = left(i), r = right(i), p = parent(i); int l = left(i), r = right(i), p = parent(i);
printf("\n当前节点的索引为 %d ,值为 %d\n", i, val(abt, i)); printf("\n当前节点的索引为 %d,值为 %d\n", i, val(abt, i));
printf("其左子节点的索引为 %d ,值为 %d\r\n", l, val(abt, l)); printf("其左子节点的索引为 %d,值为 %d\n", l, l < arrSize ? val(abt, l) : INT_MAX);
printf("其右子节点的索引为 %d ,值为 %d\r\n", r, val(abt, r)); printf("其右子节点的索引为 %d,值为 %d\n", r, r < arrSize ? val(abt, r) : INT_MAX);
printf("其父节点的索引为 %d ,值为 %d\r\n", p, val(abt, p)); printf("其父节点的索引为 %d,值为 %d\n", p, p < arrSize ? val(abt, p) : INT_MAX);
// 遍历树 // 遍历树
vector *res = levelOrder(abt); int returnSize;
int *res;
res = levelOrder(abt, &returnSize);
printf("\n层序遍历为: "); printf("\n层序遍历为: ");
printVector(res, printFunc); printArray(res, returnSize);
delVector(res); free(res);
res = preOrder(abt);
res = preOrder(abt, &returnSize);
printf("前序遍历为: "); printf("前序遍历为: ");
printVector(res, printFunc); printArray(res, returnSize);
delVector(res); free(res);
res = inOrder(abt);
res = inOrder(abt, &returnSize);
printf("中序遍历为: "); printf("中序遍历为: ");
printVector(res, printFunc); printArray(res, returnSize);
delVector(res); free(res);
res = postOrder(abt);
res = postOrder(abt, &returnSize);
printf("后序遍历为: "); printf("后序遍历为: ");
printVector(res, printFunc); printArray(res, returnSize);
delVector(res); free(res);
// 释放内存
free(root);
free(abt);
return 0; return 0;
} }