Skip to content

Commit

Permalink
refactor: Replace vector with array in C code (krahets#894)
Browse files Browse the repository at this point in the history
* Re-implement merge sort function.

* Replace vector with array for C.

* fix
  • Loading branch information
krahets authored Oct 27, 2023
1 parent 5385057 commit 492a69e
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 327 deletions.
34 changes: 19 additions & 15 deletions codes/c/chapter_backtracking/preorder_traversal_i_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

#include "../utils/common.h"

vector *res;
// 假设结果长度不超过 100
#define MAX_SIZE 100

// 打印向量中的元素
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d ", node->val);
}
TreeNode *res[MAX_SIZE];
int resSize = 0;

/* 前序遍历:例题一 */
void preOrder(TreeNode *root) {
static void preOrder(TreeNode *root) {
if (root == NULL) {
return;
}
if (root->val == 7) {
// 记录解
vectorPushback(res, root, sizeof(int));
res[resSize++] = root;
}
preOrder(root->left);
preOrder(root->right);
Expand All @@ -30,15 +28,21 @@ void preOrder(TreeNode *root) {
/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
res = newVector();
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 前序遍历
preOrder(root);

printf("\n输出所有值为 7 的节点\r\n");
printVector(res, printFunc);
delVector(res);
}
printf("\n输出所有值为 7 的节点\n");
int vals[resSize];
for (int i = 0; i < resSize; i++) {
vals[i] = res[i]->val;
}
printArray(vals, resSize);

// 释放内存
freeMemoryTree(root);
return 0;
}
67 changes: 30 additions & 37 deletions codes/c/chapter_backtracking/preorder_traversal_ii_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,55 @@

#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) {
return;
}
// 尝试
vectorPushback(path, root, sizeof(TreeNode));
path[pathSize++] = root;
if (root->val == 7) {
// 记录解
vector *newPath = newVector();
for (int i = 0; i < path->size; i++) {
vectorPushback(newPath, path->data[i], sizeof(int));
for (int i = 0; i < pathSize; ++i) {
res[resSize][i] = path[i];
}
vectorPushback(res, newPath, sizeof(vector));
resSize++;
}

preOrder(root->left, path, res);
preOrder(root->right, path, res);

preOrder(root->left);
preOrder(root->right);
// 回退
vectorPopback(path);
}

// 打印向量中的元素
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");
}
pathSize--;
}

/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrToTree(arr, n);
printf("\r\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 创建存储路径和结果的向量
vector *path = newVector();
vector *res = newVector();

// 前序遍历
preOrder(root, path, res);

// 输出结果
printf("输出所有根节点到节点 7 的路径:\n");
printResult(res);
preOrder(root);

printf("\n输出所有根节点到节点 7 的路径\n");
for (int i = 0; i < resSize; ++i) {
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);
delVector(res);
freeMemoryTree(root);
return 0;
}
68 changes: 30 additions & 38 deletions codes/c/chapter_backtracking/preorder_traversal_iii_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,56 @@

#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) {
return;
}
// 尝试
vectorPushback(path, root, sizeof(TreeNode));
path[pathSize++] = root;
if (root->val == 7) {
// 记录解
vector *newPath = newVector();
for (int i = 0; i < path->size; i++) {
vectorPushback(newPath, path->data[i], sizeof(int));
for (int i = 0; i < pathSize; i++) {
res[resSize][i] = path[i];
}
vectorPushback(res, newPath, sizeof(vector));
res->depth++;
resSize++;
}

preOrder(root->left, path, res);
preOrder(root->right, path, res);

preOrder(root->left);
preOrder(root->right);
// 回退
vectorPopback(path);
}

// 打印向量中的元素
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");
}
pathSize--;
}

/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrToTree(arr, n);
printf("\r\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 创建存储路径和结果的向量
vector *path = newVector();
vector *res = newVector();

// 前序遍历
preOrder(root, path, res);

// 输出结果
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
printResult(res);
preOrder(root);

printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
for (int i = 0; i < resSize; ++i) {
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);
delVector(res);
freeMemoryTree(root);
return 0;
}
91 changes: 41 additions & 50 deletions codes/c/chapter_backtracking/preorder_traversal_iii_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,87 @@

#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) {
return state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7;
bool isSolution(void) {
return pathSize > 0 && path[pathSize - 1]->val == 7;
}

/* 记录解 */
void recordSolution(vector *state, vector *res) {
vector *newPath = newVector();
for (int i = 0; i < state->size; i++) {
vectorPushback(newPath, state->data[i], sizeof(int));
void recordSolution(void) {
for (int i = 0; i < pathSize; i++) {
res[resSize][i] = path[i];
}
vectorPushback(res, newPath, sizeof(vector));
resSize++;
}

/* 判断在当前状态下,该选择是否合法 */
bool isValid(vector *state, TreeNode *choice) {
bool isValid(TreeNode *choice) {
return choice != NULL && choice->val != 3;
}

/* 更新状态 */
void makeChoice(vector *state, TreeNode *choice) {
vectorPushback(state, choice, sizeof(TreeNode));
void makeChoice(TreeNode *choice) {
path[pathSize++] = choice;
}

/* 恢复状态 */
void undoChoice(vector *state, TreeNode *choice) {
vectorPopback(state);
void undoChoice(void) {
pathSize--;
}

/* 回溯算法:例题三 */
void backtrack(vector *state, vector *choices, vector *res) {
void backtrack(TreeNode *choices[2]) {
// 检查是否为解
if (isSolution(state)) {
if (isSolution()) {
// 记录解
recordSolution(state, res);
return;
recordSolution();
}
// 遍历所有选择
for (int i = 0; i < choices->size; i++) {
TreeNode *choice = choices->data[i];
for (int i = 0; i < 2; i++) {
TreeNode *choice = choices[i];
// 剪枝:检查选择是否合法
if (isValid(state, choice)) {
if (isValid(choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
makeChoice(choice);
// 进行下一轮选择
vector *nextChoices = newVector();
vectorPushback(nextChoices, choice->left, sizeof(TreeNode));
vectorPushback(nextChoices, choice->right, sizeof(TreeNode));
backtrack(state, nextChoices, res);
TreeNode *nextChoices[2] = {choice->left, choice->right};
backtrack(nextChoices);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
undoChoice();
}
}
}

// 打印向量中的元素
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d ", node->val);
}

/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrToTree(arr, n);
printf("\r\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 回溯算法
vector *state = newVector();
vector *choices = newVector();
vector *res = newVector();
vectorPushback(choices, root, sizeof(TreeNode));
backtrack(state, choices, res);
TreeNode *choices[2] = {root, NULL};
backtrack(choices);

printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
for (int i = 0; i < res->size; i++) {
vector *path = res->data[i];
vector *vals = newVector();
for (int j = 0; j < path->size; j++) {
TreeNode *node = path->data[j];
vectorPushback(vals, &node->val, sizeof(int));
printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
for (int i = 0; i < resSize; ++i) {
int vals[MAX_SIZE];
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printVector(vals, printFunc);
printArray(vals, size);
}

// 释放内存
delVector(state);
delVector(choices);
delVector(res);
freeMemoryTree(root);
return 0;
}
Loading

0 comments on commit 492a69e

Please sign in to comment.