diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_i_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_i_compact.cpp index f18a5b9ce..46b218128 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_i_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_i_compact.cpp @@ -23,7 +23,7 @@ static void preOrder(TreeNode *root) { /* Driver Code */ int main() { - TreeNode *root = vecToTree(vector{1, 7, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 7, 3, 4, 5, 6, 7}); cout << "\n初始化二叉树" << endl; printTree(root); diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_ii_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_ii_compact.cpp index e1511256a..bb810afd5 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_ii_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_ii_compact.cpp @@ -28,7 +28,7 @@ static void preOrder(TreeNode *root) { /* Driver Code */ int main() { - TreeNode *root = vecToTree(vector{1, 7, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 7, 3, 4, 5, 6, 7}); cout << "\n初始化二叉树" << endl; printTree(root); diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp index 26fe1f14e..ee00f47f5 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp @@ -29,7 +29,7 @@ static void preOrder(TreeNode *root) { /* Driver Code */ int main() { - TreeNode *root = vecToTree(vector{1, 7, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 7, 3, 4, 5, 6, 7}); cout << "\n初始化二叉树" << endl; printTree(root); diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp index 1d043b583..80c45dc1a 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp @@ -56,7 +56,7 @@ void backtrack(vector &state, vector &choices, vector{1, 7, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 7, 3, 4, 5, 6, 7}); cout << "\n初始化二叉树" << endl; printTree(root); diff --git a/codes/cpp/chapter_heap/my_heap.cpp b/codes/cpp/chapter_heap/my_heap.cpp index 75746c91b..8854bfee2 100644 --- a/codes/cpp/chapter_heap/my_heap.cpp +++ b/codes/cpp/chapter_heap/my_heap.cpp @@ -114,7 +114,7 @@ class MaxHeap { cout << "堆的数组表示:"; printVector(maxHeap); cout << "堆的树状表示:" << endl; - TreeNode *root = vecToTree(maxHeap); + TreeNode *root = vectorToTree(maxHeap); printTree(root); freeMemoryTree(root); } diff --git a/codes/cpp/chapter_tree/CMakeLists.txt b/codes/cpp/chapter_tree/CMakeLists.txt index 3a9736758..fa7009bcb 100644 --- a/codes/cpp/chapter_tree/CMakeLists.txt +++ b/codes/cpp/chapter_tree/CMakeLists.txt @@ -3,3 +3,4 @@ add_executable(binary_search_tree binary_search_tree.cpp) add_executable(binary_tree binary_tree.cpp) add_executable(binary_tree_bfs binary_tree_bfs.cpp) add_executable(binary_tree_dfs binary_tree_dfs.cpp) +add_executable(array_binary_tree array_binary_tree.cpp) \ No newline at end of file diff --git a/codes/cpp/chapter_tree/array_binary_tree.cpp b/codes/cpp/chapter_tree/array_binary_tree.cpp new file mode 100644 index 000000000..0ca7ff799 --- /dev/null +++ b/codes/cpp/chapter_tree/array_binary_tree.cpp @@ -0,0 +1,137 @@ +/** + * File: array_binary_tree.cpp + * Created Time: 2023-07-19 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* 数组表示下的二叉树类 */ +class ArrayBinaryTree { + public: + /* 构造方法 */ + ArrayBinaryTree(vector arr) { + tree = arr; + } + + /* 节点数量 */ + int size() { + return tree.size(); + } + + /* 获取索引为 i 节点的值 */ + int val(int i) { + // 若索引越界,则返回 INT_MAX ,代表空位 + if (i < 0 || i >= size()) + return INT_MAX; + return tree[i]; + } + + /* 获取索引为 i 节点的左子节点的索引 */ + int left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + int right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + int parent(int i) { + return (i - 1) / 2; + } + + /* 层序遍历 */ + vector levelOrder() { + vector res; + // 直接遍历数组 + for (int i = 0; i < size(); i++) { + if (val(i) != INT_MAX) + res.push_back(val(i)); + } + return res; + } + + /* 前序遍历 */ + vector preOrder() { + vector res; + dfs(0, "pre", res); + return res; + } + + /* 中序遍历 */ + vector inOrder() { + vector res; + dfs(0, "in", res); + return res; + } + + /* 后序遍历 */ + vector postOrder() { + vector res; + dfs(0, "post", res); + return res; + } + + private: + vector tree; + + /* 深度优先遍历 */ + void dfs(int i, string order, vector &res) { + // 若为空位,则返回 + if (val(i) == INT_MAX) + return; + // 前序遍历 + if (order == "pre") + res.push_back(val(i)); + dfs(left(i), order, res); + // 中序遍历 + if (order == "in") + res.push_back(val(i)); + dfs(right(i), order, res); + // 后序遍历 + if (order == "post") + res.push_back(val(i)); + } +}; + +/* Driver Code */ +int main() { + // 初始化二叉树 + // 使用 INT_MAX 代表空位 nullptr + vector arr = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; + TreeNode *root = vectorToTree(arr); + cout << "\n初始化二叉树\n"; + cout << "二叉树的数组表示:\n"; + printVector(arr); + cout << "二叉树的链表表示:\n"; + printTree(root); + + // 数组表示下的二叉树类 + ArrayBinaryTree abt(arr); + + // 访问节点 + 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"; + + // 遍历树 + vector res = abt.levelOrder(); + cout << "\n层序遍历为: "; + printVector(res); + res = abt.preOrder(); + cout << "前序遍历为: "; + printVector(res); + res = abt.inOrder(); + cout << "中序遍历为: "; + printVector(res); + res = abt.postOrder(); + cout << "后序遍历为: "; + printVector(res); + + return 0; +} diff --git a/codes/cpp/chapter_tree/binary_tree_bfs.cpp b/codes/cpp/chapter_tree/binary_tree_bfs.cpp index dad366439..85d4500dd 100644 --- a/codes/cpp/chapter_tree/binary_tree_bfs.cpp +++ b/codes/cpp/chapter_tree/binary_tree_bfs.cpp @@ -29,7 +29,7 @@ vector levelOrder(TreeNode *root) { int main() { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - TreeNode *root = vecToTree(vector{1, 2, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 2, 3, 4, 5, 6, 7}); cout << endl << "初始化二叉树\n" << endl; printTree(root); diff --git a/codes/cpp/chapter_tree/binary_tree_dfs.cpp b/codes/cpp/chapter_tree/binary_tree_dfs.cpp index a756d33e7..3b5cefa7e 100644 --- a/codes/cpp/chapter_tree/binary_tree_dfs.cpp +++ b/codes/cpp/chapter_tree/binary_tree_dfs.cpp @@ -43,7 +43,7 @@ void postOrder(TreeNode *root) { int main() { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - TreeNode *root = vecToTree(vector{1, 2, 3, 4, 5, 6, 7}); + TreeNode *root = vectorToTree(vector{1, 2, 3, 4, 5, 6, 7}); cout << endl << "初始化二叉树\n" << endl; printTree(root); diff --git a/codes/cpp/utils/list_node.hpp b/codes/cpp/utils/list_node.hpp index 0e2d94977..618dff314 100644 --- a/codes/cpp/utils/list_node.hpp +++ b/codes/cpp/utils/list_node.hpp @@ -7,6 +7,8 @@ #pragma once #include +#include + using namespace std; /* Definition for a singly-linked list node */ diff --git a/codes/cpp/utils/print_utils.hpp b/codes/cpp/utils/print_utils.hpp index e497956bc..52342dde4 100644 --- a/codes/cpp/utils/print_utils.hpp +++ b/codes/cpp/utils/print_utils.hpp @@ -223,7 +223,7 @@ template void printHeap(priority_queue +#include -/* Definition for a binary tree node */ +using namespace std; + +/* 二叉树节点结构体 */ struct TreeNode { int val{}; int height = 0; @@ -20,45 +23,55 @@ struct TreeNode { } }; -/* Generate a binary tree with a vector */ -TreeNode *vecToTree(vector list) { - if (list.empty()) +// 序列化编码规则请参考: +// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ +// 二叉树的数组表示: +// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] +// 二叉树的链表表示: +// /——— 15 +// /——— 7 +// /——— 3 +// | \——— 6 +// | \——— 12 +// ——— 1 +// \——— 2 +// | /——— 9 +// \——— 4 +// \——— 8 + +/* 将列表反序列化为二叉树:递归 */ +TreeNode *vectorToTreeDFS(vector &arr, int i) { + if (i < 0 || i >= arr.size() || arr[i] == INT_MAX) { return nullptr; - - auto *root = new TreeNode(list[0]); - queue que; - que.emplace(root); - size_t n = list.size(), i = 0; - while (!que.empty()) { - auto node = que.front(); - que.pop(); - if (++i >= n) - break; - // INT_MAX represent null - if (list[i] != INT_MAX) { - node->left = new TreeNode(list[i]); - que.emplace(node->left); - } - if (++i >= n) - break; - if (list[i] != INT_MAX) { - node->right = new TreeNode(list[i]); - que.emplace(node->right); - } } - + TreeNode *root = new TreeNode(arr[i]); + root->left = vectorToTreeDFS(arr, 2 * i + 1); + root->right = vectorToTreeDFS(arr, 2 * i + 2); return root; } -/* Get a tree node with specific value in a binary tree */ -TreeNode *getTreeNode(TreeNode *root, int val) { +/* 将列表反序列化为二叉树 */ +TreeNode *vectorToTree(vector arr) { + return vectorToTreeDFS(arr, 0); +} + +/* 将二叉树序列化为列表:递归 */ +void treeToVecorDFS(TreeNode *root, int i, vector &res) { if (root == nullptr) - return nullptr; - if (root->val == val) - return root; - TreeNode *left = getTreeNode(root->left, val); - TreeNode *right = getTreeNode(root->right, val); - return left != nullptr ? left : right; + return; + while (i >= res.size()) { + res.push_back(INT_MAX); + } + res[i] = root->val; + treeToVecorDFS(root->left, 2 * i + 1, res); + treeToVecorDFS(root->right, 2 * i + 2, res); +} + +/* 将二叉树序列化为列表 */ +vector treeToVecor(TreeNode *root) { + vector res; + treeToVecorDFS(root, 0, res); + return res; } /* Free the memory allocated to a tree */ diff --git a/codes/cpp/utils/vertex.hpp b/codes/cpp/utils/vertex.hpp index 447ffc266..395e36fc4 100644 --- a/codes/cpp/utils/vertex.hpp +++ b/codes/cpp/utils/vertex.hpp @@ -7,6 +7,7 @@ #pragma once #include + using namespace std; /* 顶点类 */ diff --git a/codes/csharp/utils/TreeNode.cs b/codes/csharp/utils/TreeNode.cs index 5bf3f3c38..efebc32a7 100644 --- a/codes/csharp/utils/TreeNode.cs +++ b/codes/csharp/utils/TreeNode.cs @@ -59,15 +59,4 @@ public class TreeNode { } return list; } - - /* Get a tree node with specific value in a binary tree */ - public static TreeNode? GetTreeNode(TreeNode? root, int val) { - if (root == null) - return null; - if (root.val == val) - return root; - TreeNode? left = GetTreeNode(root.left, val); - TreeNode? right = GetTreeNode(root.right, val); - return left ?? right; - } } diff --git a/codes/java/chapter_tree/array_binary_tree.java b/codes/java/chapter_tree/array_binary_tree.java new file mode 100644 index 000000000..029b1b0c4 --- /dev/null +++ b/codes/java/chapter_tree/array_binary_tree.java @@ -0,0 +1,136 @@ +/** + * File: array_binary_tree.java + * Created Time: 2023-07-19 + * Author: Krahets (krahets@163.com) + */ + +package chapter_tree; + +import utils.*; +import java.util.*; + +/* 数组表示下的二叉树类 */ +class ArrayBinaryTree { + private List tree; + + /* 构造方法 */ + public ArrayBinaryTree(List arr) { + tree = new ArrayList<>(arr); + } + + /* 节点数量 */ + public int size() { + return tree.size(); + } + + /* 获取索引为 i 节点的值 */ + public Integer val(int i) { + // 若索引越界,则返回 null ,代表空位 + if (i < 0 || i >= size()) + return null; + return tree.get(i); + } + + /* 获取索引为 i 节点的左子节点的索引 */ + public Integer left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + public Integer right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + public Integer parent(int i) { + return (i - 1) / 2; + } + + /* 层序遍历 */ + public List levelOrder() { + List res = new ArrayList<>(); + // 直接遍历数组 + for (int i = 0; i < size(); i++) { + if (val(i) != null) + res.add(val(i)); + } + return res; + } + + /* 深度优先遍历 */ + private void dfs(Integer i, String order, List res) { + // 若为空位,则返回 + if (val(i) == null) + return; + // 前序遍历 + if (order == "pre") + res.add(val(i)); + dfs(left(i), order, res); + // 中序遍历 + if (order == "in") + res.add(val(i)); + dfs(right(i), order, res); + // 后序遍历 + if (order == "post") + res.add(val(i)); + } + + /* 前序遍历 */ + public List preOrder() { + List res = new ArrayList<>(); + dfs(0, "pre", res); + return res; + } + + /* 中序遍历 */ + public List inOrder() { + List res = new ArrayList<>(); + dfs(0, "in", res); + return res; + } + + /* 后序遍历 */ + public List postOrder() { + List res = new ArrayList<>(); + dfs(0, "post", res); + return res; + } +} + +public class array_binary_tree { + public static void main(String[] args) { + // 初始化二叉树 + // 这里借助了一个从数组直接生成二叉树的函数 + List arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15); + + TreeNode root = TreeNode.listToTree(arr); + System.out.println("\n初始化二叉树\n"); + System.out.println("二叉树的数组表示:"); + System.out.println(arr); + System.out.println("二叉树的链表表示:"); + PrintUtil.printTree(root); + + // 数组表示下的二叉树类 + ArrayBinaryTree abt = new ArrayBinaryTree(arr); + + // 访问节点 + int i = 1; + Integer l = abt.left(i); + Integer r = abt.right(i); + Integer p = abt.parent(i); + System.out.println("\n当前节点的索引为 " + i + " ,值为 " + abt.val(i)); + System.out.println("其左子节点的索引为 " + l + " ,值为 " + (l == null ? "null" : abt.val(l))); + System.out.println("其右子节点的索引为 " + r + " ,值为 " + (r == null ? "null" : abt.val(r))); + System.out.println("其父节点的索引为 " + p + " ,值为 " + (p == null ? "null" : abt.val(p))); + + // 遍历树 + List res = abt.levelOrder(); + System.out.println("\n层序遍历为:" + res); + res = abt.preOrder(); + System.out.println("前序遍历为:" + res); + res = abt.inOrder(); + System.out.println("中序遍历为:" + res); + res = abt.postOrder(); + System.out.println("后序遍历为:" + res); + } +} diff --git a/codes/java/utils/TreeNode.java b/codes/java/utils/TreeNode.java index 13e116052..fa4a45692 100644 --- a/codes/java/utils/TreeNode.java +++ b/codes/java/utils/TreeNode.java @@ -8,61 +8,66 @@ package utils; import java.util.*; -/* Definition for a binary tree node. */ +/* 二叉树节点类 */ public class TreeNode { public int val; // 节点值 public int height; // 节点高度 public TreeNode left; // 左子节点引用 public TreeNode right; // 右子节点引用 + /* 构造方法 */ public TreeNode(int x) { val = x; } - /* Generate a binary tree given an array */ - public static TreeNode listToTree(List list) { - int size = list.size(); - if (size == 0) - return null; + // 序列化编码规则请参考: + // https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ + // 二叉树的数组表示: + // [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] + // 二叉树的链表表示: + // /——— 15 + // /——— 7 + // /——— 3 + // | \——— 6 + // | \——— 12 + // ——— 1 + // \——— 2 + // | /——— 9 + // \——— 4 + // \——— 8 - TreeNode root = new TreeNode(list.get(0)); - Queue queue = new LinkedList<>(); - queue.add(root); - int i = 0; - while (!queue.isEmpty()) { - TreeNode node = queue.poll(); - if (++i >= size) - break; - if (list.get(i) != null) { - node.left = new TreeNode(list.get(i)); - queue.add(node.left); - } - if (++i >= size) - break; - if (list.get(i) != null) { - node.right = new TreeNode(list.get(i)); - queue.add(node.right); - } + /* 将列表反序列化为二叉树:递归 */ + private static TreeNode listToTreeDFS(List arr, int i) { + if (i < 0 || i >= arr.size() || arr.get(i) == null) { + return null; } + TreeNode root = new TreeNode(arr.get(i)); + root.left = listToTreeDFS(arr, 2 * i + 1); + root.right = listToTreeDFS(arr, 2 * i + 2); return root; } - /* Serialize a binary tree to a list */ - public static List treeToList(TreeNode root) { - List list = new ArrayList<>(); + /* 将列表反序列化为二叉树 */ + public static TreeNode listToTree(List arr) { + return listToTreeDFS(arr, 0); + } + + /* 将二叉树序列化为列表:递归 */ + private static void treeToListDFS(TreeNode root, int i, List res) { if (root == null) - return list; - Queue queue = new LinkedList() {{ add(root); }}; - while (!queue.isEmpty()) { - TreeNode node = queue.poll(); - if (node != null) { - list.add(node.val); - queue.add(node.left); - queue.add(node.right); - } else { - list.add(null); - } + return; + while (i >= res.size()) { + res.add(null); } - return list; + res.set(i, root.val); + treeToListDFS(root.left, 2 * i + 1, res); + treeToListDFS(root.right, 2 * i + 2, res); + } + + /* 将二叉树序列化为列表 */ + public static List treeToList(TreeNode root) { + List res = new ArrayList<>(); + treeToListDFS(root, 0, res); + return res; } } diff --git a/codes/python/chapter_tree/array_binary_tree.py b/codes/python/chapter_tree/array_binary_tree.py new file mode 100644 index 000000000..724a6eec9 --- /dev/null +++ b/codes/python/chapter_tree/array_binary_tree.py @@ -0,0 +1,118 @@ +""" +File: array_binary_tree.py +Created Time: 2023-07-19 +Author: Krahets (krahets@163.com) +""" + +import sys, os.path as osp + +sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) +from modules import * + + +class ArrayBinaryTree: + """数组表示下的二叉树类""" + + def __init__(self, arr: list[int | None]): + """构造方法""" + self.__tree = list(arr) + + def size(self): + """节点数量""" + return len(self.__tree) + + def val(self, i: int) -> int: + """获取索引为 i 节点的值""" + # 若索引越界,则返回 None ,代表空位 + if i < 0 or i >= self.size(): + return None + return self.__tree[i] + + def left(self, i: int) -> int | None: + """获取索引为 i 节点的左子节点的索引""" + return 2 * i + 1 + + def right(self, i: int) -> int | None: + """获取索引为 i 节点的右子节点的索引""" + return 2 * i + 2 + + def parent(self, i: int) -> int | None: + """获取索引为 i 节点的父节点的索引""" + return (i - 1) // 2 + + def level_order(self) -> list[int]: + """层序遍历""" + self.res = [] + # 直接遍历数组 + for i in range(self.size()): + if self.val(i) is not None: + self.res.append(self.val(i)) + return self.res + + def __dfs(self, i: int, order: str): + """深度优先遍历""" + if self.val(i) is None: + return + # 前序遍历 + if order == "pre": + self.res.append(self.val(i)) + self.__dfs(self.left(i), order) + # 中序遍历 + if order == "in": + self.res.append(self.val(i)) + self.__dfs(self.right(i), order) + # 后序遍历 + if order == "post": + self.res.append(self.val(i)) + + def pre_order(self) -> list[int]: + """前序遍历""" + self.res = [] + self.__dfs(0, order="pre") + return self.res + + def in_order(self) -> list[int]: + """中序遍历""" + self.res = [] + self.__dfs(0, order="in") + return self.res + + def post_order(self) -> list[int]: + """后序遍历""" + self.res = [] + self.__dfs(0, order="post") + return self.res + + +"""Driver Code""" +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(arr) + print(f"二叉树的链表表示:") + print_tree(root) + + # 数组表示下的二叉树类 + abt = ArrayBinaryTree(arr) + + # 访问节点 + i = 1 + l, r, p = abt.left(i), abt.right(i), abt.parent(i) + print(f"\n当前节点的索引为 {i} ,值为 {abt.val(i)}") + print(f"其左子节点的索引为 {l} ,值为 {abt.val(l)}") + print(f"其右子节点的索引为 {r} ,值为 {abt.val(r)}") + print(f"其父节点的索引为 {p} ,值为 {abt.val(p)}") + + # 遍历树 + res = abt.level_order() + print("\n层序遍历为:", res) + res = abt.pre_order() + print("前序遍历为:", res) + res = abt.in_order() + print("中序遍历为:", res) + res = abt.post_order() + print("后序遍历为:", res) diff --git a/codes/python/modules/__init__.py b/codes/python/modules/__init__.py index d83ba118a..96a04f2cb 100644 --- a/codes/python/modules/__init__.py +++ b/codes/python/modules/__init__.py @@ -3,13 +3,13 @@ from __future__ import annotations # Import common libs here to simplify the codes by `from module import *` -from .linked_list import ( +from .list_node import ( ListNode, list_to_linked_list, linked_list_to_list, get_list_node, ) -from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node +from .tree_node import TreeNode, list_to_tree, tree_to_list, get_tree_node from .vertex import Vertex, vals_to_vets, vets_to_vals from .print_util import ( print_matrix, diff --git a/codes/python/modules/binary_tree.py b/codes/python/modules/binary_tree.py deleted file mode 100644 index b889e1e8c..000000000 --- a/codes/python/modules/binary_tree.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -File: binary_tree.py -Created Time: 2021-12-11 -Author: Krahets (krahets@163.com) -""" - -from collections import deque - - -class TreeNode: - """Definition for a binary tree node""" - - def __init__(self, val: int = 0): - self.val: int = val # 节点值 - self.height: int = 0 # 节点高度 - self.left: TreeNode | None = None # 左子节点引用 - self.right: TreeNode | None = None # 右子节点引用 - - -def list_to_tree(arr: list[int]) -> TreeNode | None: - """Generate a binary tree with a list""" - if not arr: - return None - - i = 0 - root = TreeNode(arr[0]) - queue: deque[TreeNode] = deque([root]) - while queue: - node: TreeNode = queue.popleft() - i += 1 - if i >= len(arr): - break - if arr[i] != None: - node.left = TreeNode(arr[i]) - queue.append(node.left) - i += 1 - if i >= len(arr): - break - if arr[i] != None: - node.right = TreeNode(arr[i]) - queue.append(node.right) - - return root - - -def tree_to_list(root: TreeNode | None) -> list[int]: - """Serialize a tree into an array""" - if not root: - return [] - queue: deque[TreeNode] = deque() - queue.append(root) - res: list[int] = [] - while queue: - node: TreeNode | None = queue.popleft() - if node: - res.append(node.val) - queue.append(node.left) - queue.append(node.right) - else: - res.append(None) - return res - - -def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None: - """Get a tree node with specific value in a binary tree""" - if not root: - return - if root.val == val: - return root - left: TreeNode | None = get_tree_node(root.left, val) - right: TreeNode | None = get_tree_node(root.right, val) - return left if left else right diff --git a/codes/python/modules/linked_list.py b/codes/python/modules/list_node.py similarity index 97% rename from codes/python/modules/linked_list.py rename to codes/python/modules/list_node.py index ce5fe27ed..8baca1696 100644 --- a/codes/python/modules/linked_list.py +++ b/codes/python/modules/list_node.py @@ -1,5 +1,5 @@ """ -File: linked_list.py +File: list_node.py Created Time: 2021-12-11 Author: Krahets (krahets@163.com) """ diff --git a/codes/python/modules/print_util.py b/codes/python/modules/print_util.py index d6b80da08..a75c55850 100644 --- a/codes/python/modules/print_util.py +++ b/codes/python/modules/print_util.py @@ -4,8 +4,8 @@ Created Time: 2021-12-11 Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com) """ -from .binary_tree import TreeNode, list_to_tree -from .linked_list import ListNode, linked_list_to_list +from .tree_node import TreeNode, list_to_tree +from .list_node import ListNode, linked_list_to_list def print_matrix(mat: list[list[int]]) -> None: diff --git a/codes/python/modules/tree_node.py b/codes/python/modules/tree_node.py new file mode 100644 index 000000000..45707ace5 --- /dev/null +++ b/codes/python/modules/tree_node.py @@ -0,0 +1,80 @@ +""" +File: tree_node.py +Created Time: 2021-12-11 +Author: Krahets (krahets@163.com) +""" + +from collections import deque + + +class TreeNode: + """二叉树节点类""" + + def __init__(self, val: int = 0): + self.val: int = val # 节点值 + self.height: int = 0 # 节点高度 + self.left: TreeNode | None = None # 左子节点引用 + self.right: TreeNode | None = None # 右子节点引用 + + # 序列化编码规则请参考: + # https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ + # 二叉树的数组表示: + # [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] + # 二叉树的链表表示: + # /——— 15 + # /——— 7 + # /——— 3 + # | \——— 6 + # | \——— 12 + # ——— 1 + # \——— 2 + # | /——— 9 + # \——— 4 + # \——— 8 + + +def list_to_tree_dfs(arr: list[int], i: int) -> TreeNode | None: + """将列表反序列化为二叉树:递归""" + # 如果索引超出数组长度,或者对应的元素为 None,返回 None + if i < 0 or i >= len(arr) or arr[i] is None: + return None + # 构建当前节点 + root = TreeNode(arr[i]) + # 递归构建左右子树 + root.left = list_to_tree_dfs(arr, 2 * i + 1) + root.right = list_to_tree_dfs(arr, 2 * i + 2) + return root + + +def list_to_tree(arr: list[int]) -> TreeNode | None: + """将列表反序列化为二叉树""" + return list_to_tree_dfs(arr, 0) + + +def tree_to_list_dfs(root: TreeNode, i: int, res: list[int]) -> list[int]: + """将二叉树序列化为列表:递归""" + if root is None: + return + if i >= len(res): + res += [None] * (i - len(res) + 1) + res[i] = root.val + tree_to_list_dfs(root.left, 2 * i + 1, res) + tree_to_list_dfs(root.right, 2 * i + 2, res) + + +def tree_to_list(root: TreeNode | None) -> list[int]: + """将二叉树序列化为列表""" + res = [] + tree_to_list_dfs(root, 0, res) + return res + + +def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None: + """Get a tree node with specific value in a binary tree""" + if not root: + return + if root.val == val: + return root + left: TreeNode | None = get_tree_node(root.left, val) + right: TreeNode | None = get_tree_node(root.right, val) + return left if left else right diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index d6ccb1728..11d572fd8 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -541,8 +541,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ## 数组典型应用 -**随机访问**。如果我们想要随机抽取一些样本,那么可以用数组存储,并生成一个随机序列,根据索引实现样本的随机抽取。 +数组是最基础的数据结构,在各类数据结构和算法中都有广泛应用。 -**二分查找**。例如前文查字典的例子,我们可以将字典中的所有字按照拼音顺序存储在数组中,然后使用与日常查纸质字典相同的“翻开中间,排除一半”的方式,来实现一个查电子字典的算法。 - -**深度学习**。神经网络中大量使用了向量、矩阵、张量之间的线性代数运算,这些数据都是以数组的形式构建的。数组是神经网络编程中最常使用的数据结构。 +- **随机访问**:如果我们想要随机抽取一些样本,那么可以用数组存储,并生成一个随机序列,根据索引实现样本的随机抽取。 +- **排序和搜索**:数组是排序和搜索算法最常用的数据结构。例如,快速排序、归并排序、二分查找等都需要在数组上进行。 +- **查找表**:当我们需要快速查找一个元素或者需要查找一个元素的对应关系时,可以使用数组作为查找表。例如,我们有一个字符到其 ASCII 码的映射,可以将字符的 ASCII 码值作为索引,对应的元素存放在数组中的对应位置。 +- **机器学习**:神经网络中大量使用了向量、矩阵、张量之间的线性代数运算,这些数据都是以数组的形式构建的。数组是神经网络编程中最常使用的数据结构。 +- **数据结构实现**:数组可以用于实现栈、队列、哈希表、堆、图等数据结构。例如,邻接矩阵是图的常见表示之一,它实质上是一个二维数组。 diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 5c2312d64..8de5a7252 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -825,3 +825,22 @@ ``` ![常见链表种类](linked_list.assets/linkedlist_common_types.png) + +## 链表典型应用 + +单向链表通常用于实现栈、队列、散列表和图等数据结构。 + +- **栈与队列**:当插入和删除操作都在链表的一端进行时,它表现出先进后出的的特性,对应栈;当插入操作在链表的一端进行,删除操作在链表的另一端进行,它表现出先进先出的特性,对应队列。 +- **散列表**:链地址法是解决哈希冲突的主流方案之一,在该方案中,所有冲突的元素都会被放到一个链表中。 +- **图**:邻接表是表示图的一种常用方式,在其中,图的每个顶点都与一个链表相关联,链表中的每个元素都代表与该顶点相连的其他顶点。 + +双向链表常被用于需要快速查找前一个和下一个元素的场景。 + +- **高级数据结构**:比如在红黑树、B 树中,我们需要知道一个节点的父节点,这可以通过在节点中保存一个指向父节点的指针来实现,类似于双向链表。 +- **浏览器历史**:在网页浏览器中,当用户点击前进或后退按钮时,浏览器需要知道用户访问过的前一个和后一个网页。双向链表的特性使得这种操作变得简单。 +- **LRU 算法**:在缓存淘汰算法(LRU)中,我们需要快速找到最近最少使用的数据,以及支持快速地添加和删除节点。这时候使用双向链表就非常合适。 + +循环链表常被用于需要周期性操作的场景,比如操作系统的资源调度。 + +- **时间片轮转调度算法**:在操作系统中,时间片轮转调度算法是一种常见的 CPU 调度算法,它需要对一组进程进行循环。每个进程被赋予一个时间片,当时间片用完时,CPU 将切换到下一个进程。这种循环的操作就可以通过循环链表来实现。 +- **数据缓冲区**:在某些数据缓冲区的实现中,也可能会使用到循环链表。比如在音频、视频播放器中,数据流可能会被分成多个缓冲块并放入一个循环链表,以便实现无缝播放。 diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index c4e577eaa..59508c648 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -34,7 +34,7 @@ ```cpp title="" /* 二叉树的数组表示 */ - // 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX + // 使用 int 最大值 INT_MAX 标记空位 vector tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; ``` @@ -110,6 +110,77 @@ ![任意类型二叉树的数组表示](array_representation_of_tree.assets/array_representation_with_empty.png) +以下为数组表示下二叉树的实现,包括: + +- 获取节点数量、节点值、左(右)子节点、父节点等基础操作; +- 获取前序遍历、中序遍历、后序遍历、层序遍历的节点值序列; + +=== "Java" + + ```java title="array_binary_tree.java" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "C++" + + ```cpp title="array_binary_tree.cpp" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Python" + + ```python title="array_binary_tree.py" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Go" + + ```go title="array_binary_tree.go" + [class]{arrayBinaryTree}-[func]{} + ``` + +=== "JavaScript" + + ```javascript title="array_binary_tree.js" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "TypeScript" + + ```typescript title="array_binary_tree.ts" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "C" + + ```c title="array_binary_tree.c" + [class]{arrayBinaryTree}-[func]{} + ``` + +=== "C#" + + ```csharp title="array_binary_tree.cs" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Swift" + + ```swift title="array_binary_tree.swift" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Zig" + + ```zig title="array_binary_tree.zig" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Dart" + + ```dart title="array_binary_tree.dart" + [class]{ArrayBinaryTree}-[func]{} + ``` + ## 优势与局限性 二叉树的数组表示存在以下优点: diff --git a/mkdocs.yml b/mkdocs.yml index ac9784529..4417487f7 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -138,7 +138,7 @@ nav: - 0.2.   如何使用本书: chapter_preface/suggestions.md - 0.3.   小结: chapter_preface/summary.md - 1.   初识算法: - # [icon: material/code-tags] + # [icon: material/calculator-variant-outline] - chapter_introduction/index.md - 1.1.   算法无处不在: chapter_introduction/algorithms_are_everywhere.md - 1.2.   算法是什么: chapter_introduction/what_is_dsa.md @@ -151,7 +151,7 @@ nav: - 2.3.   空间复杂度: chapter_computational_complexity/space_complexity.md - 2.4.   小结: chapter_computational_complexity/summary.md - 3.   数据结构: - # [icon: material/database-outline] + # [icon: material/shape-outline] - chapter_data_structure/index.md - 3.1.   数据结构分类: chapter_data_structure/classification_of_data_structure.md - 3.2.   基本数据类型: chapter_data_structure/basic_data_types.md @@ -159,7 +159,7 @@ nav: - 3.4.   字符编码 *: chapter_data_structure/character_encoding.md - 3.5.   小结: chapter_data_structure/summary.md - 4.   数组与链表: - # [icon: material/view-grid-outline] + # [icon: material/view-list-outline] - chapter_array_and_linkedlist/index.md - 4.1.   数组: chapter_array_and_linkedlist/array.md - 4.2.   链表: chapter_array_and_linkedlist/linked_list.md @@ -225,7 +225,7 @@ nav: - 11.10.   基数排序: chapter_sorting/radix_sort.md - 11.11.   小结: chapter_sorting/summary.md - 12.   分治: - # [icon: material/file-tree-outline, status: new] + # [icon: material/set-split, status: new] - chapter_divide_and_conquer/index.md # [status: new] - 12.1.   分治算法: chapter_divide_and_conquer/divide_and_conquer.md