cpp-algo-cases/chapter_tree/array_binary_tree.cpp

138 lines
3.2 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: array_binary_tree.cpp
* Created Time: 2023-07-19
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
class ArrayBinaryTree {
public:
/* <20><><EFBFBD><EFBFBD><ECB7BD> */
ArrayBinaryTree(vector<int> arr) {
tree = arr;
}
/* <20>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD> */
int size() {
return tree.size();
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϊ i <20>ڵ<EFBFBD><DAB5><EFBFBD>ֵ */
int val(int i) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>򷵻<EFBFBD> INT_MAX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
if (i < 0 || i >= size())
return INT_MAX;
return tree[i];
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϊ i <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int left(int i) {
return 2 * i + 1;
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϊ i <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int right(int i) {
return 2 * i + 2;
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϊ i <20>ڵ<EFBFBD><DAB5>ĸ<EFBFBD><C4B8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int parent(int i) {
return (i - 1) / 2;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> levelOrder() {
vector<int> res;
// ֱ<>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < size(); i++) {
if (val(i) != INT_MAX)
res.push_back(val(i));
}
return res;
}
/* ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> preOrder() {
vector<int> res;
dfs(0, "pre", res);
return res;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> inOrder() {
vector<int> res;
dfs(0, "in", res);
return res;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> postOrder() {
vector<int> res;
dfs(0, "post", res);
return res;
}
private:
vector<int> tree;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD> */
void dfs(int i, string order, vector<int> &res) {
// <20><>Ϊ<EFBFBD><CEAA>λ<EFBFBD><CEBB><EFBFBD>򷵻<EFBFBD>
if (val(i) == INT_MAX)
return;
// ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (order == "pre")
res.push_back(val(i));
dfs(left(i), order, res);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (order == "in")
res.push_back(val(i));
dfs(right(i), order, res);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (order == "post")
res.push_back(val(i));
}
};
/* Driver Code */
int main() {
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ʹ<><CAB9> INT_MAX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ 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<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>\n";
printVector(arr);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>\n";
printTree(root);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ArrayBinaryTree abt(arr);
// <20><><EFBFBD>ʽڵ<CABD>
int i = 1;
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
cout << "\n<EFBFBD><EFBFBD>ǰ<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << i << "<EFBFBD><EFBFBD>ֵΪ " << abt.val(i) << "\n";
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << l << "<EFBFBD><EFBFBD>ֵΪ " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << r << "<EFBFBD><EFBFBD>ֵΪ " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
cout << "<EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << p << "<EFBFBD><EFBFBD>ֵΪ " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<int> res = abt.levelOrder();
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> ";
printVector(res);
res = abt.preOrder();
cout << "ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> ";
printVector(res);
res = abt.inOrder();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> ";
printVector(res);
res = abt.postOrder();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> ";
printVector(res);
return 0;
}