From 449258f0b06fcb666f73bf4d6488f0de9be46942 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 27 Dec 2022 19:33:58 +0800 Subject: [PATCH] Add the code to the docs. --- codes/cpp/chapter_tree/binary_tree_bfs.cpp | 8 +-- codes/python/chapter_tree/binary_tree.py | 7 ++- codes/python/chapter_tree/binary_tree_bfs.py | 24 ++++----- codes/python/chapter_tree/binary_tree_dfs.py | 38 ++++++-------- docs/chapter_tree/binary_tree.md | 6 +-- docs/chapter_tree/binary_tree_traversal.md | 52 +++++++++++++++++--- 6 files changed, 81 insertions(+), 54 deletions(-) diff --git a/codes/cpp/chapter_tree/binary_tree_bfs.cpp b/codes/cpp/chapter_tree/binary_tree_bfs.cpp index eeec2a18e..30c2d6007 100644 --- a/codes/cpp/chapter_tree/binary_tree_bfs.cpp +++ b/codes/cpp/chapter_tree/binary_tree_bfs.cpp @@ -15,12 +15,12 @@ vector hierOrder(TreeNode* root) { vector vec; while (!queue.empty()) { TreeNode* node = queue.front(); - queue.pop(); // 队列出队 - vec.push_back(node->val); // 保存结点 + queue.pop(); // 队列出队 + vec.push_back(node->val); // 保存结点 if (node->left != nullptr) - queue.push(node->left); // 左子结点入队 + queue.push(node->left); // 左子结点入队 if (node->right != nullptr) - queue.push(node->right); // 右子结点入队 + queue.push(node->right); // 右子结点入队 } return vec; } diff --git a/codes/python/chapter_tree/binary_tree.py b/codes/python/chapter_tree/binary_tree.py index 2b25e4724..d99026352 100644 --- a/codes/python/chapter_tree/binary_tree.py +++ b/codes/python/chapter_tree/binary_tree.py @@ -12,13 +12,14 @@ from include import * """ Driver Code """ if __name__ == "__main__": - # 初始化二叉树 + """ 初始化二叉树 """ # 初始化节点 n1 = TreeNode(val=1) n2 = TreeNode(val=2) n3 = TreeNode(val=3) n4 = TreeNode(val=4) n5 = TreeNode(val=5) + # 构建引用指向(即指针) n1.left = n2 n1.right = n3 n2.left = n4 @@ -26,15 +27,13 @@ if __name__ == "__main__": print("\n初始化二叉树\n") print_tree(n1) - # 插入与删除结点 + """ 插入与删除结点 """ P = TreeNode(0) - # 在 n1 -> n2 中间插入节点 P n1.left = P P.left = n2 print("\n插入结点 P 后\n") print_tree(n1) - # 删除结点 n1.left = n2 print("\n删除结点 P 后\n"); diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index c6a73013b..0320a08dd 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -14,22 +14,18 @@ from include import * """ 层序遍历 """ def hier_order(root: TreeNode): # 初始化队列,加入根结点 - queue: typing.Deque[TreeNode] = collections.deque() + queue = collections.deque() queue.append(root) # 初始化一个列表,用于保存遍历序列 - result = [] + res = [] while queue: - # 队列出队 - node = queue.popleft() - # 保存节点值 - result.append(node.val) + node = queue.popleft() # 队列出队 + res.append(node.val) # 保存节点值 if node.left is not None: - # 左子结点入队 - queue.append(node.left) + queue.append(node.left) # 左子结点入队 if node.right is not None: - # 右子结点入队 - queue.append(node.right) - return result + queue.append(node.right) # 右子结点入队 + return res """ Driver Code """ @@ -41,6 +37,6 @@ if __name__ == "__main__": print_tree(root) # 层序遍历 - result = hier_order(root) - print("\n层序遍历的结点打印序列 = ", result) - assert result == [1, 2, 3, 4, 5, 6, 7] + res = hier_order(root) + print("\n层序遍历的结点打印序列 = ", res) + assert res == [1, 2, 3, 4, 5, 6, 7] diff --git a/codes/python/chapter_tree/binary_tree_dfs.py b/codes/python/chapter_tree/binary_tree_dfs.py index b5dc9c9f3..11ee8339c 100644 --- a/codes/python/chapter_tree/binary_tree_dfs.py +++ b/codes/python/chapter_tree/binary_tree_dfs.py @@ -11,40 +11,34 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * -result = [] +res = [] - -""" 前序遍历二叉树 """ +""" 前序遍历 """ def pre_order(root: typing.Optional[TreeNode]): if root is None: return - # 访问优先级:根结点 -> 左子树 -> 右子树 - result.append(root.val) + res.append(root.val) pre_order(root=root.left) pre_order(root=root.right) - -""" 中序遍历二叉树 """ +""" 中序遍历 """ def in_order(root: typing.Optional[TreeNode]): if root is None: return - # 访问优先级:左子树 -> 根结点 -> 右子树 in_order(root=root.left) - result.append(root.val) + res.append(root.val) in_order(root=root.right) - -""" 后序遍历二叉树 """ +""" 后序遍历 """ def post_order(root: typing.Optional[TreeNode]): if root is None: return - # 访问优先级:左子树 -> 右子树 -> 根结点 post_order(root=root.left) post_order(root=root.right) - result.append(root.val) + res.append(root.val) """ Driver Code """ @@ -56,19 +50,19 @@ if __name__ == "__main__": print_tree(root) # 前序遍历 - result.clear() + res.clear() pre_order(root) - print("\n前序遍历的结点打印序列 = ", result) - assert result == [1, 2, 4, 5, 3, 6, 7] + print("\n前序遍历的结点打印序列 = ", res) + assert res == [1, 2, 4, 5, 3, 6, 7] # 中序遍历 - result.clear() + res.clear() in_order(root) - print("\n中序遍历的结点打印序列 = ", result) - assert result == [4, 2, 5, 1, 6, 3, 7] + print("\n中序遍历的结点打印序列 = ", res) + assert res == [4, 2, 5, 1, 6, 3, 7] # 后序遍历 - result.clear() + res.clear() post_order(root) - print("\n后序遍历的结点打印序列 = ", result) - assert result == [4, 5, 2, 6, 7, 3, 1] + print("\n后序遍历的结点打印序列 = ", res) + assert res == [4, 5, 2, 6, 7, 3, 1] diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 92539cc80..262b170be 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -182,7 +182,7 @@ comments: true === "Python" ```python title="binary_tree.py" - # 初始化二叉树 + """ 初始化二叉树 """ # 初始化节点 n1 = TreeNode(val=1) n2 = TreeNode(val=2) @@ -302,7 +302,7 @@ comments: true === "Python" ```python title="binary_tree.py" - # 插入与删除结点 + """ 插入与删除结点 """ p = TreeNode(0) # 在 n1 -> n2 中间插入结点 P n1.left = p @@ -461,7 +461,7 @@ comments: true === "Python" ```python title="" - “”“ 二叉树的数组表示 ”“” + """ 二叉树的数组表示 """ # 直接使用 None 来表示空位 tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] ``` diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index 971d24cc5..f46348412 100644 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -51,12 +51,12 @@ comments: true vector vec; while (!queue.empty()) { TreeNode* node = queue.front(); - queue.pop(); // 队列出队 - vec.push_back(node->val); // 保存结点 + queue.pop(); // 队列出队 + vec.push_back(node->val); // 保存结点 if (node->left != nullptr) - queue.push(node->left); // 左子结点入队 + queue.push(node->left); // 左子结点入队 if (node->right != nullptr) - queue.push(node->right); // 右子结点入队 + queue.push(node->right); // 右子结点入队 } return vec; } @@ -65,7 +65,21 @@ comments: true === "Python" ```python title="binary_tree_bfs.py" - + """ 层序遍历 """ + def hier_order(root: TreeNode): + # 初始化队列,加入根结点 + queue = collections.deque() + queue.append(root) + # 初始化一个列表,用于保存遍历序列 + res = [] + while queue: + node = queue.popleft() # 队列出队 + res.append(node.val) # 保存节点值 + if node.left is not None: + queue.append(node.left) # 左子结点入队 + if node.right is not None: + queue.append(node.right) # 右子结点入队 + return res ``` === "Go" @@ -256,7 +270,32 @@ comments: true === "Python" ```python title="binary_tree_dfs.py" - + """ 前序遍历 """ + def pre_order(root: typing.Optional[TreeNode]): + if root is None: + return + # 访问优先级:根结点 -> 左子树 -> 右子树 + res.append(root.val) + pre_order(root=root.left) + pre_order(root=root.right) + + """ 中序遍历 """ + def in_order(root: typing.Optional[TreeNode]): + if root is None: + return + # 访问优先级:左子树 -> 根结点 -> 右子树 + in_order(root=root.left) + res.append(root.val) + in_order(root=root.right) + + """ 后序遍历 """ + def post_order(root: typing.Optional[TreeNode]): + if root is None: + return + # 访问优先级:左子树 -> 右子树 -> 根结点 + post_order(root=root.left) + post_order(root=root.right) + res.append(root.val) ``` === "Go" @@ -402,7 +441,6 @@ comments: true postOrder(root.right); list.Add(root.val); } - ``` !!! note