From 6723cdbc7e53176108c721997017108a18370bb8 Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 18 Apr 2023 14:31:23 +0800 Subject: [PATCH] Represent null with INT_MAX in C, C++. --- codes/c/CMakeLists.txt | 2 ++ codes/c/chapter_heap/my_heap.c | 2 +- codes/c/chapter_searching/leetcode_two_sum.c | 14 ++++++++------ codes/c/chapter_stack_and_queue/array_stack.c | 4 ++-- .../chapter_stack_and_queue/linkedlist_stack.c | 2 +- codes/c/chapter_tree/avl_tree.c | 2 +- codes/c/chapter_tree/binary_tree_bfs.c | 2 +- codes/c/include/include_test.c | 2 +- codes/c/include/print_util.h | 4 ++-- codes/c/include/tree_node.h | 10 ++++++---- codes/cpp/include/TreeNode.hpp | 17 ++++++++++------- 11 files changed, 35 insertions(+), 26 deletions(-) diff --git a/codes/c/CMakeLists.txt b/codes/c/CMakeLists.txt index 588025726..0f4179a0f 100644 --- a/codes/c/CMakeLists.txt +++ b/codes/c/CMakeLists.txt @@ -10,5 +10,7 @@ add_subdirectory(chapter_computational_complexity) add_subdirectory(chapter_array_and_linkedlist) add_subdirectory(chapter_stack_and_queue) add_subdirectory(chapter_binary_search) +add_subdirectory(chapter_tree) add_subdirectory(chapter_heap) add_subdirectory(chapter_searching) +add_subdirectory(chapter_sorting) diff --git a/codes/c/chapter_heap/my_heap.c b/codes/c/chapter_heap/my_heap.c index 29dc640a6..d59f3c3de 100644 --- a/codes/c/chapter_heap/my_heap.c +++ b/codes/c/chapter_heap/my_heap.c @@ -90,7 +90,7 @@ int pop(maxHeap *h) { // 判空处理 if (isEmpty(h)) { printf("heap is empty!"); - return NIL; + return INT_MAX; } // 交换根节点与最右叶节点(即交换首元素与尾元素) swap(h, 0, size(h) - 1); diff --git a/codes/c/chapter_searching/leetcode_two_sum.c b/codes/c/chapter_searching/leetcode_two_sum.c index a6298199d..20a34eea7 100644 --- a/codes/c/chapter_searching/leetcode_two_sum.c +++ b/codes/c/chapter_searching/leetcode_two_sum.c @@ -11,10 +11,10 @@ int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) { for (int i = 0; i < numsSize; ++i) { for (int j = i + 1; j < numsSize; ++j) { if (nums[i] + nums[j] == target) { - int *ret = malloc(sizeof(int) * 2); - ret[0] = i, ret[1] = j; + int *res = malloc(sizeof(int) * 2); + res[0] = i, res[1] = j; *returnSize = 2; - return ret; + return res; } } } @@ -32,12 +32,14 @@ struct hashTable { typedef struct hashTable hashTable; +/* 哈希表查询 */ hashTable *find(hashTable *h, int key) { hashTable *tmp; HASH_FIND_INT(h, &key, tmp); return tmp; } +/* 哈希表元素插入 */ void insert(hashTable *h, int key, int val) { hashTable *t = find(h, key); if (t == NULL) { @@ -55,10 +57,10 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) { for (int i = 0; i < numsSize; i++) { hashTable *t = find(hashtable, target - nums[i]); if (t != NULL) { - int *ret = malloc(sizeof(int) * 2); - ret[0] = t->val, ret[1] = i; + int *res = malloc(sizeof(int) * 2); + res[0] = t->val, res[1] = i; *returnSize = 2; - return ret; + return res; } insert(hashtable, nums[i], i); } diff --git a/codes/c/chapter_stack_and_queue/array_stack.c b/codes/c/chapter_stack_and_queue/array_stack.c index fb42cf864..d8f48e18e 100644 --- a/codes/c/chapter_stack_and_queue/array_stack.c +++ b/codes/c/chapter_stack_and_queue/array_stack.c @@ -48,7 +48,7 @@ void push(arrayStack *s, int num) { int peek(arrayStack *s) { if (s->size == 0) { printf("stack is empty.\n"); - return NIL; + return INT_MAX; } return s->data[s->size - 1]; } @@ -57,7 +57,7 @@ int peek(arrayStack *s) { int pop(arrayStack *s) { if (s->size == 0) { printf("stack is empty.\n"); - return NIL; + return INT_MAX; } int val = peek(s); s->size--; diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index e740fb481..34b1d87bd 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -65,7 +65,7 @@ void push(linkedListStack *s, int num) { int pop(linkedListStack *s) { if (s->size == 0) { printf("stack is empty.\n"); - return NIL; + return INT_MAX; } assert(s); int val = peek(s); diff --git a/codes/c/chapter_tree/avl_tree.c b/codes/c/chapter_tree/avl_tree.c index 8142979db..f9714322b 100644 --- a/codes/c/chapter_tree/avl_tree.c +++ b/codes/c/chapter_tree/avl_tree.c @@ -30,7 +30,7 @@ int height(TreeNode *node) { } /* 更新节点高度 */ -int updateHeight(TreeNode *node) { +void updateHeight(TreeNode *node) { int lh = height(node->left); int rh = height(node->right); // 节点高度等于最高子树高度 + 1 diff --git a/codes/c/chapter_tree/binary_tree_bfs.c b/codes/c/chapter_tree/binary_tree_bfs.c index b5cb63f9e..d20bc5430 100644 --- a/codes/c/chapter_tree/binary_tree_bfs.c +++ b/codes/c/chapter_tree/binary_tree_bfs.c @@ -49,7 +49,7 @@ int *levelOrder(TreeNode *root, int *size) { int main() { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; + int nums[] = {1, 2, 3, 4, 5, 6, 7}; int size = sizeof(nums) / sizeof(int); TreeNode *root = arrToTree(nums, size); printf("初始化二叉树\n"); diff --git a/codes/c/include/include_test.c b/codes/c/include/include_test.c index 602670d68..282de9276 100644 --- a/codes/c/include/include_test.c +++ b/codes/c/include/include_test.c @@ -17,7 +17,7 @@ void testListNode() { } void testTreeNode() { - int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; + int nums[] = {1, 2, 3, INT_MAX, 5, 6, INT_MAX}; int size = sizeof(nums) / sizeof(int); TreeNode *root = arrToTree(nums, size); diff --git a/codes/c/include/print_util.h b/codes/c/include/print_util.h index b38a82495..fa0302ceb 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -23,13 +23,13 @@ static void printArray(int arr[], int size) { printf("["); if (arr != NULL && size != 0) { for (int i = 0; i < size - 1; i++) { - if (arr[i] != NIL) { + if (arr[i] != INT_MAX) { printf("%d, ", arr[i]); } else { printf("NULL, "); } } - if (arr[size - 1] != NIL) { + if (arr[size - 1] != INT_MAX) { printf("%d]\n", arr[size - 1]); } else { printf("NULL]\n"); diff --git a/codes/c/include/tree_node.h b/codes/c/include/tree_node.h index 7a8b5584e..5e0c13732 100644 --- a/codes/c/include/tree_node.h +++ b/codes/c/include/tree_node.h @@ -11,7 +11,8 @@ extern "C" { #endif -#define NIL ('#') +#include + #define MAX_NODE_SIZE 5000 struct TreeNode { @@ -59,14 +60,15 @@ TreeNode *arrToTree(const int *arr, size_t size) { node = queue[front++]; index++; if (index < size) { - if (arr[index] != NIL) { + // represent null with INT_MAX + if (arr[index] != INT_MAX) { node->left = newTreeNode(arr[index]); queue[rear++] = node->left; } } index++; if (index < size) { - if (arr[index] != NIL) { + if (arr[index] != INT_MAX) { node->right = newTreeNode(arr[index]); queue[rear++] = node->right; } @@ -102,7 +104,7 @@ int *treeToArr(TreeNode *root) { queue[rear++] = node->left; queue[rear++] = node->right; } else { - arr[index] = NIL; + arr[index] = INT_MAX; } index++; } diff --git a/codes/cpp/include/TreeNode.hpp b/codes/cpp/include/TreeNode.hpp index b3ab85fe3..3d4460159 100644 --- a/codes/cpp/include/TreeNode.hpp +++ b/codes/cpp/include/TreeNode.hpp @@ -6,6 +6,8 @@ #pragma once +#include + /* Definition for a binary tree node */ struct TreeNode { int val{}; @@ -26,20 +28,21 @@ TreeNode *vecToTree(vector list) { auto *root = new TreeNode(list[0]); queue que; que.emplace(root); - size_t n = list.size(), index = 0; + size_t n = list.size(), i = 0; while (!que.empty()) { auto node = que.front(); que.pop(); - if (++index >= n) + if (++i >= n) break; - if (index < n) { - node->left = new TreeNode(list[index]); + // INT_MAX represent null + if (list[i] != INT_MAX) { + node->left = new TreeNode(list[i]); que.emplace(node->left); } - if (++index >= n) + if (++i >= n) break; - if (index < n) { - node->right = new TreeNode(list[index]); + if (list[i] != INT_MAX) { + node->right = new TreeNode(list[i]); que.emplace(node->right); } }