Merge pull request #234 from Reanon/feat/c-basic-include

feat(include\tree): add C codes and modify exist C codes
pull/236/head
Yudong Jin 2 years ago committed by GitHub
commit 3581203854
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
.gitignore vendored

@ -4,7 +4,9 @@
# Editor
.vscode/
.idea/
cmake-build-debug/
hello-algo.iml
*.dSYM/
# mkdocs files
site/

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.10)
project(hello_algo C)
set(CMAKE_C_STANDARD 11)
include_directories(./include)
add_subdirectory(include)
add_subdirectory(chapter_computational_complexity)
add_subdirectory(chapter_array_and_linkedlist)
add_subdirectory(chapter_sorting)
add_subdirectory(chapter_tree)

@ -74,11 +74,11 @@ int main() {
int size = 5;
int arr[5];
printf("数组 arr = ");
printArray(arr, size);
PrintArray(arr, size);
int nums[5] = { 1, 3, 2, 5, 4 };
printf("数组 nums = ");
printArray(nums, size);
PrintArray(nums, size);
/* 随机访问 */
int randomNum = randomAccess(nums, size);
@ -89,17 +89,17 @@ int main() {
int* res = extend(nums, size, enlarge);
size += enlarge;
printf("将数组长度扩展至 8 ,得到 nums = ");
printArray(res, size);
PrintArray(res, size);
/* 插入元素 */
insert(res, size, 6, 3);
printf("在索引 3 处插入数字 6 ,得到 nums = ");
printArray(res, size);
PrintArray(res, size);
/* 删除元素 */
removeItem(res, size, 2);
printf("删除索引 2 处的元素,得到 nums = ");
printArray(res, size);
PrintArray(res, size);
/* 遍历数组 */
traverse(res, size);

@ -0,0 +1,2 @@
add_executable(time_complexity time_complexity.c )
add_executable(worst_best_time_complexity worst_best_time_complexity.c)

@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
int *nums = randomNumbers(n);
int index = findOne(nums, n);
printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = ");
printArray(nums, n);
PrintArray(nums, n);
printf("数字 1 的索引为 %d\n", index);
// 释放堆区内存
if (nums != NULL) {
@ -49,6 +49,5 @@ int main(int argc, char *argv[]) {
nums = NULL;
}
}
getchar();
return 0;
}

@ -0,0 +1,2 @@
add_executable(bubble_sort bubble_sort.c)
add_executable(insertion_sort insertion_sort.c)

@ -50,14 +50,14 @@ void bubble_sort_with_flag(int nums[], int size) {
/* Driver Code */
int main() {
int nums[6] = {4, 1, 3, 1, 5, 2};
printf("冒泡排序后:\n");
printf("冒泡排序后: ");
bubble_sort(nums, 6);
for (int i = 0; i < 6; i++)
{
printf("%d ", nums[i]);
}
printf("优化版冒泡排序后:\n");
printf("\n优化版冒泡排序后: ");
bubble_sort_with_flag(nums, 6);
for (int i = 0; i < 6; i++)
{

@ -28,7 +28,7 @@ void insertionSort(int nums[], int size) {
int main() {
int nums[] = {4, 1, 3, 1, 5, 2};
insertionSort(nums, 6);
printf("插入排序完成后 nums = \n");
printf("插入排序完成后 nums = ");
for (int i = 0; i < 6; i++)
{
printf("%d ", nums[i]);

@ -0,0 +1,4 @@
add_executable(binary_search 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)

@ -0,0 +1,8 @@
/**
* File: binary_search_tree.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../include/include.h"

@ -0,0 +1,43 @@
/**
* File: binary_tree.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../include/include.h"
/* Driver Code */
int main() {
/* 初始化二叉树 */
// 初始化结点
TreeNode* n1 = NewTreeNode(1);
TreeNode* n2 = NewTreeNode(2);
TreeNode* n3 = NewTreeNode(3);
TreeNode* n4 = NewTreeNode(4);
TreeNode* n5 = NewTreeNode(5);
// 构建引用指向(即指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
printf("初始化二叉树\n");
PrintTree(n1);
/* 插入与删除结点 */
TreeNode* P = NewTreeNode(0);
// 在 n1 -> n2 中间插入结点 P
n1->left = P;
P->left = n2;
printf("插入结点 P 后\n");
PrintTree(n1);
// 删除结点 P
n1->left = n2;
// 释放内存
free(P);
printf("删除结点 P 后\n");
PrintTree(n1);
return 0;
}

@ -0,0 +1,66 @@
/**
* File: binary_tree_bfs.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../include/include.h"
/* 层序遍历 */
int *levelOrder(TreeNode *root, int *size) {
/* 辅助队列 */
int front, rear;
int index, *arr;
TreeNode *node;
TreeNode **queue;
/* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 加入根结点
queue[rear++] = root;
// 初始化一个列表,用于保存遍历序列
/* 辅助数组 */
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针
index = 0;
while (front < rear) {
// 队列出队
node = queue[front++];
// 保存结点
arr[index++] = node->val;
if (node->left != NULL) {
// 左子结点入队
queue[rear++] = node->left;
}
if (node->right != NULL) {
// 右子结点入队
queue[rear++] = node->right;
}
}
// 更新数组长度的值
*size = index;
arr = realloc(arr, sizeof(int) * (*size));
return arr;
}
/* Driver Code */
int main() {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
int nums[] = {1, 2, 3, NIL, 5, 6, NIL};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = ArrayToTree(nums, size);
printf("初始化二叉树\n");
PrintTree(root);
/* 层序遍历 */
// 需要传入数组的长度
int *arr = levelOrder(root, &size);
printf("层序遍历的结点打印序列 = ");
PrintArray(arr, size);
return 0;
}

@ -0,0 +1,72 @@
/**
* File: binary_tree_dfs.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../include/include.h"
/* 辅助数组,用于存储遍历序列 */
int *arr;
/* 前序遍历 */
void preOrder(TreeNode *root, int *size) {
if (root == NULL) return;
// 访问优先级:根结点 -> 左子树 -> 右子树
arr[(*size)++] = root->val;
preOrder(root->left, size);
preOrder(root->right, size);
}
/* 中序遍历 */
void inOrder(TreeNode *root, int *size) {
if (root == NULL) return;
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root->left, size);
arr[(*size)++] = root->val;
inOrder(root->right, size);
}
/* 后序遍历 */
void postOrder(TreeNode *root, int *size) {
if (root == NULL) return;
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root->left, size);
postOrder(root->right, size);
arr[(*size)++] = root->val;
}
/* Driver Code */
int main() {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
int nums[] = {1, 2, 3, 4, 5, 6, 7};
int size = sizeof(nums) / sizt ceof(int);
TreeNode *root = ArrayToTree(nums, size);
printf("初始化二叉树\n");
PrintTree(root);
/* 前序遍历 */
// 初始化辅助数组
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
size = 0;
preOrder(root, &size);
printf("前序遍历的结点打印序列 = ");
PrintArray(arr, size);
/* 中序遍历 */
size = 0;
inOrder(root, &size);
printf("中序遍历的结点打印序列 = ");
PrintArray(arr, size);
/* 后序遍历 */
size = 0;
postOrder(root, &size);
printf("后序遍历的结点打印序列 = ");
PrintArray(arr, size);
return 0;
}

@ -0,0 +1,4 @@
add_executable(include
include_test.c
include.h print_util.h
list_node.h tree_node.h)

@ -1,28 +0,0 @@
/**
* File: PrintUtil.h
* Created Time: 2022-12-21
* Author: MolDum (moldum@163.com)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// #include "ListNode.h"
// #include "TreeNode.h"
/**
* @brief Print an Array
*
* @param arr
* @param n
*/
static void printArray(int* arr, int n)
{
printf("[");
for (int i = 0; i < n - 1; i++) {
printf("%d, ", arr[i]);
}
printf("%d]\n", arr[n-1]);
}

@ -1,13 +1,28 @@
/**
* File: include.h
* Created Time: 2022-12-20
* Author: MolDuM (moldum@163.com)
* Author: MolDuM (moldum@163.com)Reanon (793584285@qq.com)
*/
#ifndef C_INCLUDE_H
#define C_INCLUDE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "PrintUtil.h"
#include "list_node.h"
#include "tree_node.h"
#include "print_util.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // C_INCLUDE_H

@ -0,0 +1,38 @@
/**
* File: include_test.c
* Created Time: 2023-01-10
* Author: Reanon (793584285@qq.com)
*/
#include "include.h"
void testListNode() {
int nums[] = {2, 3, 5, 6, 7};
int size = sizeof(nums) / sizeof(int);
ListNode *head = ArrayToLinkedList(nums, size);
PrintLinkedList(head);
ListNode *node = GetListNode(head, 5);
printf("find node: %d\n", node->val);
}
void testTreeNode() {
int nums[] = {1, 2, 3, NIL, 5, 6, NIL};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = ArrayToTree(nums, size);
// print tree
PrintTree(root);
// tree to arr
int *arr = TreeToArray(root);
PrintArray(arr, size);
}
int main(int argc, char *argv[]) {
printf("==testListNode==\n");
testListNode();
printf("==testTreeNode==\n");
testTreeNode();
return 0;
}

@ -0,0 +1,72 @@
/**
* File: list_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef LIST_NODE_H
#define LIST_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Definition for a singly-linked list node
*
*/
struct ListNode {
int val; // 结点值
struct ListNode *next; // 指向下一结点的指针(引用)
};
// typedef 为 C 语言的关键字,作用是为一种数据类型定义一个新名字
typedef struct ListNode ListNode;
ListNode *NewListNode(int val) {
ListNode *node, *next;
node = (ListNode *) malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
return node;
}
/**
* @brief Generate a linked list with a vector
*
* @param list
* @return ListNode*
*/
ListNode *ArrayToLinkedList(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
ListNode *dummy = NewListNode(0);
ListNode *node = dummy;
for (int i = 0; i < size; i++) {
node->next = NewListNode(arr[i]);
node = node->next;
}
return dummy->next;
}
/**
* @brief Get a list node with specific value from a linked list
*
* @param head
* @param val
* @return ListNode*
*/
ListNode *GetListNode(ListNode *head, int val) {
while (head != NULL && head->val != val) {
head = head->next;
}
return head;
}
#ifdef __cplusplus
}
#endif
#endif // LIST_NODE_H

@ -0,0 +1,135 @@
/**
* File: print_util.h
* Created Time: 2022-12-21
* Author: MolDum (moldum@163.com)Reanon (793584285@qq.com)
*/
#ifndef PRINT_UTIL_H
#define PRINT_UTIL_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list_node.h"
#include "tree_node.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Print an Array
*
* @param arr
* @param size
*/
static void PrintArray(int *arr, int size) {
printf("[");
for (int i = 0; i < size - 1; i++) {
if (arr[i] != NIL) {
printf("%d, ", arr[i]);
} else {
printf("NULL, ");
}
}
if (arr[size - 1] != NIL) {
printf("%d]\n", arr[size - 1]);
}else{
printf("NULL]\n");
}
}
/**
* @brief Print a linked list
*
* @param head
*/
static void PrintLinkedList(ListNode *node) {
if (node == NULL) {
return;
}
while (node->next != NULL) {
printf("%d -> ", node->val);
node = node->next;
}
printf("%d\n", node->val);
}
struct Trunk {
struct Trunk *prev;
char *str;
};
typedef struct Trunk Trunk;
Trunk *newTrunk(Trunk *prev, char *str) {
Trunk *trunk = (Trunk *) malloc(sizeof(Trunk));
trunk->prev = prev;
trunk->str = (char *) malloc(sizeof(char) * 10);
strcpy(trunk->str, str);
return trunk;
}
/**
* @brief Helper function to print branches of the binary tree
*
* @param trunk
*/
void showTrunks(Trunk *trunk) {
if (trunk == NULL) {
return;
}
showTrunks(trunk->prev);
printf("%s", trunk->str);
}
/**
* Help to print a binary tree, hide more details
* @param node
* @param prev
* @param isLeft
*/
static void printTreeHelper(TreeNode *node, Trunk *prev, bool isLeft) {
if (node == NULL) {
return;
}
char *prev_str = " ";
Trunk *trunk = newTrunk(prev, prev_str);
printTreeHelper(node->right, trunk, true);
if (prev == NULL) {
trunk->str = "———";
} else if (isLeft) {
trunk->str = "/———";
prev_str = " |";
} else {
trunk->str = "\\———";
prev->str = prev_str;
}
showTrunks(trunk);
printf("%d\n", node->val);
if (prev != NULL) {
prev->str = prev_str;
}
trunk->str = " |";
printTreeHelper(node->left, trunk, false);
}
/**
* @brief Print a binary tree
*
* @param head
*/
static void PrintTree(TreeNode *root) {
printTreeHelper(root, NULL, false);
}
#ifdef __cplusplus
}
#endif
#endif // PRINT_UTIL_H

@ -0,0 +1,131 @@
/**
* File: tree_node.h
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef TREE_NODE_H
#define TREE_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
#define NIL ('#')
#define MAX_NODE_SIZE 5000
struct TreeNode {
int val;
int height;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode TreeNode;
TreeNode *NewTreeNode(int val) {
TreeNode *node;
node = (TreeNode *) malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
/**
* @brief Generate a binary tree with an array
*
* @param arr
* @param size
* @return TreeNode *
*/
TreeNode *ArrayToTree(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
int front, rear, index;
TreeNode *root, *node;
TreeNode **queue;
/* 根结点 */
root = NewTreeNode(arr[0]);
/* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 将根结点放入队尾
queue[rear++] = root;
// 记录遍历数组的索引
index = 0;
while (front < rear) {
// 取队列中的头结点,并让头结点出队
node = queue[front++];
index++;
if (index < size) {
if (arr[index] != NIL) {
node->left = NewTreeNode(arr[index]);
queue[rear++] = node->left;
}
}
index++;
if (index < size) {
if (arr[index] != NIL) {
node->right = NewTreeNode(arr[index]);
queue[rear++] = node->right;
}
}
}
return root;
}
/**
* @brief Generate a binary tree with an array
*
* @param arr
* @param size
* @return TreeNode *
*/
int *TreeToArray(TreeNode *root) {
if (root == NULL) {
return NULL;
}
int front, rear;
int index, *arr;
TreeNode *node;
TreeNode **queue;
/* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 将根结点放入队尾
queue[rear++] = root;
/* 辅助数组 */
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针
index = 0;
while (front < rear) {
// 取队列中的头结点,并让头结点出队
node = queue[front++];
if (node != NULL) {
arr[index] = node->val;
queue[rear++] = node->left;
queue[rear++] = node->right;
} else {
arr[index] = NIL;
}
index++;
}
return arr;
}
#ifdef __cplusplus
}
#endif
#endif // TREE_NODE_H

@ -20,7 +20,7 @@ func TestBinarySearchTree(t *testing.T) {
fmt.Println("\n二叉树的根结点为:", node.Val)
// 查找结点
node = bst.Search(7)
node = bst.search(7)
fmt.Println("查找到的结点对象为", node, ",结点值 =", node.Val)
// 插入结点

Loading…
Cancel
Save