From 5b1a219b8bca77c1aa3a0a187e2e8101f80c9681 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 27 Oct 2023 01:01:21 +0800 Subject: [PATCH] Fine-tune code and texts. --- codes/cpp/chapter_tree/array_binary_tree.cpp | 6 +++--- codes/cpp/utils/tree_node.hpp | 3 +-- codes/python/chapter_tree/array_binary_tree.py | 4 ++-- docs/chapter_sorting/merge_sort.md | 5 +---- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/codes/cpp/chapter_tree/array_binary_tree.cpp b/codes/cpp/chapter_tree/array_binary_tree.cpp index 0ca7ff799..ce07ecda9 100644 --- a/codes/cpp/chapter_tree/array_binary_tree.cpp +++ b/codes/cpp/chapter_tree/array_binary_tree.cpp @@ -115,9 +115,9 @@ int main() { int i = 1; int l = abt.left(i), r = abt.right(i), p = abt.parent(i); cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n"; - cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "None") << "\n"; - cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "None") << "\n"; - cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "None") << "\n"; + cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n"; + cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n"; + cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n"; // 遍历树 vector res = abt.levelOrder(); diff --git a/codes/cpp/utils/tree_node.hpp b/codes/cpp/utils/tree_node.hpp index 055c673dd..0c0b5ee5e 100644 --- a/codes/cpp/utils/tree_node.hpp +++ b/codes/cpp/utils/tree_node.hpp @@ -74,12 +74,11 @@ vector treeToVecor(TreeNode *root) { return res; } -/* Free the memory allocated to a tree */ +/* 释放二叉树内存 */ void freeMemoryTree(TreeNode *root) { if (root == nullptr) return; freeMemoryTree(root->left); freeMemoryTree(root->right); - // 释放内存 delete root; } diff --git a/codes/python/chapter_tree/array_binary_tree.py b/codes/python/chapter_tree/array_binary_tree.py index 0073a51aa..dd556ff06 100644 --- a/codes/python/chapter_tree/array_binary_tree.py +++ b/codes/python/chapter_tree/array_binary_tree.py @@ -92,9 +92,9 @@ if __name__ == "__main__": arr = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] root = list_to_tree(arr) print("\n初始化二叉树\n") - print(f"二叉树的数组表示:") + print("二叉树的数组表示:") print(arr) - print(f"二叉树的链表表示:") + print("二叉树的链表表示:") print_tree(root) # 数组表示下的二叉树类 diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 7a596fa30..3ab4201cb 100755 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -55,10 +55,7 @@ [file]{merge_sort}-[class]{}-[func]{merge_sort} ``` -实现合并函数 `merge()` 存在以下难点。 - -- **需要特别注意各个变量的含义**。`nums` 的待合并区间为 `[left, right]` ,但由于 `tmp` 仅复制了 `nums` 该区间的元素,因此 `tmp` 对应区间为 `[0, right - left]` 。 -- 在比较 `tmp[i]` 和 `tmp[j]` 的大小时,**还需考虑子数组遍历完成后的索引越界问题**,即 `i > leftEnd` 和 `j > rightEnd` 的情况。索引越界的优先级是最高的,如果左子数组已经被合并完了,那么不需要继续比较,直接合并右子数组元素即可。 +值得注意的是,`nums` 的待合并区间为 `[left, right]` ,而 `tmp` 的对应区间为 `[0, right - left]` 。 ## 算法特性