Represent null with INT_MAX in C, C++.

pull/469/head
krahets 2 years ago
parent ed8fa6aea3
commit 6723cdbc7e

@ -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)

@ -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);

@ -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);
}

@ -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--;

@ -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);

@ -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

@ -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");

@ -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);

@ -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");

@ -11,7 +11,8 @@
extern "C" {
#endif
#define NIL ('#')
#include <limits.h>
#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++;
}

@ -6,6 +6,8 @@
#pragma once
#include <limits.h>
/* Definition for a binary tree node */
struct TreeNode {
int val{};
@ -26,20 +28,21 @@ TreeNode *vecToTree(vector<int> list) {
auto *root = new TreeNode(list[0]);
queue<TreeNode *> 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);
}
}

Loading…
Cancel
Save