mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
Add implementation of array binary tree.
Rewrite the tree serialization and deserialization methods. Add applications of array and linked list.
This commit is contained in:
@@ -23,7 +23,7 @@ static void preOrder(TreeNode *root) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ static void preOrder(TreeNode *root) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ static void preOrder(TreeNode *root) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
||||
cout << "\n初始化二叉树" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ class MaxHeap {
|
||||
cout << "堆的数组表示:";
|
||||
printVector(maxHeap);
|
||||
cout << "堆的树状表示:" << endl;
|
||||
TreeNode *root = vecToTree(maxHeap);
|
||||
TreeNode *root = vectorToTree(maxHeap);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
137
codes/cpp/chapter_tree/array_binary_tree.cpp
Normal file
137
codes/cpp/chapter_tree/array_binary_tree.cpp
Normal file
@@ -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<int> 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<int> levelOrder() {
|
||||
vector<int> res;
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != INT_MAX)
|
||||
res.push_back(val(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
vector<int> preOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
vector<int> inOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
vector<int> postOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "post", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
vector<int> tree;
|
||||
|
||||
/* 深度优先遍历 */
|
||||
void dfs(int i, string order, vector<int> &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<int> 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<int> 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;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ vector<int> levelOrder(TreeNode *root) {
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode *root = vecToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "初始化二叉树\n" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ void postOrder(TreeNode *root) {
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode *root = vecToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "初始化二叉树\n" << endl;
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* Definition for a singly-linked list node */
|
||||
|
||||
@@ -223,7 +223,7 @@ template <typename T, typename S, typename C> void printHeap(priority_queue<T, S
|
||||
cout << "堆的数组表示:";
|
||||
printVector(vec);
|
||||
cout << "堆的树状表示:" << endl;
|
||||
TreeNode *root = vecToTree(vec);
|
||||
TreeNode *root = vectorToTree(vec);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
|
||||
@@ -7,8 +7,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <limits.h>
|
||||
#include <vector>
|
||||
|
||||
/* 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<int> 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<int> &arr, int i) {
|
||||
if (i < 0 || i >= arr.size() || arr[i] == INT_MAX) {
|
||||
return nullptr;
|
||||
|
||||
auto *root = new TreeNode(list[0]);
|
||||
queue<TreeNode *> 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<int> arr) {
|
||||
return vectorToTreeDFS(arr, 0);
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表:递归 */
|
||||
void treeToVecorDFS(TreeNode *root, int i, vector<int> &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<int> treeToVecor(TreeNode *root) {
|
||||
vector<int> res;
|
||||
treeToVecorDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Free the memory allocated to a tree */
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* 顶点类 */
|
||||
|
||||
Reference in New Issue
Block a user