Skip to content

Commit

Permalink
Fine-tune code and texts.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Oct 26, 2023
1 parent 3628b40 commit 5b1a219
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
6 changes: 3 additions & 3 deletions codes/cpp/chapter_tree/array_binary_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> res = abt.levelOrder();
Expand Down
3 changes: 1 addition & 2 deletions codes/cpp/utils/tree_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ vector<int> 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;
}
4 changes: 2 additions & 2 deletions codes/python/chapter_tree/array_binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def post_order(self) -> list[int]:
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)

# 数组表示下的二叉树类
Expand Down
5 changes: 1 addition & 4 deletions docs/chapter_sorting/merge_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`

## 算法特性

Expand Down

0 comments on commit 5b1a219

Please sign in to comment.