mirror of
http://bgp.hk.skcks.cn:10086/https://github.com/krahets/hello-algo
synced 2026-04-21 05:13:00 +08:00
Some checks failed
C / build (Release, cl, codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, cl, en/codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, clang, codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, clang, en/codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, codes/c, g++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, en/codes/c, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, cl, codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, cl, en/codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, clang, codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, clang, en/codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, en/codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
Dart / Dart stable on macos-latest (push) Has been cancelled
Dart / Dart stable on ubuntu-latest (push) Has been cancelled
Dart / Dart stable on windows-latest (push) Has been cancelled
.NET / .NET 8.0.x on macos-latest (push) Has been cancelled
.NET / .NET 8.0.x on ubuntu-latest (push) Has been cancelled
.NET / .NET 8.0.x on windows-latest (push) Has been cancelled
Go / Go 1.19.x on macos-latest (push) Has been cancelled
Go / Go 1.19.x on ubuntu-latest (push) Has been cancelled
Go / Go 1.19.x on windows-latest (push) Has been cancelled
Java / Java 11 sample (push) Has been cancelled
Java / Java 17 sample (push) Has been cancelled
JavaScript / build (codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, windows-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, windows-latest) (push) Has been cancelled
Kotlin / Kotlin on macos-latest (push) Has been cancelled
Kotlin / Kotlin on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on macos-latest (push) Has been cancelled
Python / Python 3.10 on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on windows-latest (push) Has been cancelled
Ruby / Ruby 3.3 on macos-latest (push) Has been cancelled
Ruby / Ruby 3.3 on ubuntu-latest (push) Has been cancelled
Ruby / Ruby 3.3 on windows-latest (push) Has been cancelled
Rust / build (codes/rust, macos-latest) (push) Has been cancelled
Rust / build (codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (codes/rust, windows-latest) (push) Has been cancelled
Rust / build (en/codes/rust, macos-latest) (push) Has been cancelled
Rust / build (en/codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (en/codes/rust, windows-latest) (push) Has been cancelled
Swift / Swift on macos-14 (push) Has been cancelled
Swift / Swift on ubuntu-22.04 (push) Has been cancelled
TypeScript / build (codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, windows-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, windows-latest) (push) Has been cancelled
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
138 lines
3.6 KiB
C++
138 lines
3.6 KiB
C++
/**
|
|
* File: array_binary_tree.cpp
|
|
* Created Time: 2023-07-19
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* Binary tree class represented by array */
|
|
class ArrayBinaryTree {
|
|
public:
|
|
/* Constructor */
|
|
ArrayBinaryTree(vector<int> arr) {
|
|
tree = arr;
|
|
}
|
|
|
|
/* List capacity */
|
|
int size() {
|
|
return tree.size();
|
|
}
|
|
|
|
/* Get value of node at index i */
|
|
int val(int i) {
|
|
// Return INT_MAX if index out of bounds, representing empty position
|
|
if (i < 0 || i >= size())
|
|
return INT_MAX;
|
|
return tree[i];
|
|
}
|
|
|
|
/* Get index of left child node of node at index i */
|
|
int left(int i) {
|
|
return 2 * i + 1;
|
|
}
|
|
|
|
/* Get index of right child node of node at index i */
|
|
int right(int i) {
|
|
return 2 * i + 2;
|
|
}
|
|
|
|
/* Get index of parent node of node at index i */
|
|
int parent(int i) {
|
|
return (i - 1) / 2;
|
|
}
|
|
|
|
/* Level-order traversal */
|
|
vector<int> levelOrder() {
|
|
vector<int> res;
|
|
// Traverse array directly
|
|
for (int i = 0; i < size(); i++) {
|
|
if (val(i) != INT_MAX)
|
|
res.push_back(val(i));
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* Preorder traversal */
|
|
vector<int> preOrder() {
|
|
vector<int> res;
|
|
dfs(0, "pre", res);
|
|
return res;
|
|
}
|
|
|
|
/* Inorder traversal */
|
|
vector<int> inOrder() {
|
|
vector<int> res;
|
|
dfs(0, "in", res);
|
|
return res;
|
|
}
|
|
|
|
/* Postorder traversal */
|
|
vector<int> postOrder() {
|
|
vector<int> res;
|
|
dfs(0, "post", res);
|
|
return res;
|
|
}
|
|
|
|
private:
|
|
vector<int> tree;
|
|
|
|
/* Depth-first traversal */
|
|
void dfs(int i, string order, vector<int> &res) {
|
|
// If empty position, return
|
|
if (val(i) == INT_MAX)
|
|
return;
|
|
// Preorder traversal
|
|
if (order == "pre")
|
|
res.push_back(val(i));
|
|
dfs(left(i), order, res);
|
|
// Inorder traversal
|
|
if (order == "in")
|
|
res.push_back(val(i));
|
|
dfs(right(i), order, res);
|
|
// Postorder traversal
|
|
if (order == "post")
|
|
res.push_back(val(i));
|
|
}
|
|
};
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
// Initialize binary tree
|
|
// Use INT_MAX to represent empty position 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 << "\nInitialize binary tree\n";
|
|
cout << "Array representation of binary tree:\n";
|
|
printVector(arr);
|
|
cout << "Linked list representation of binary tree:\n";
|
|
printTree(root);
|
|
|
|
// Binary tree class represented by array
|
|
ArrayBinaryTree abt(arr);
|
|
|
|
// Access node
|
|
int i = 1;
|
|
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
|
cout << "\nCurrent node index is " << i << ", value is " << abt.val(i) << "\n";
|
|
cout << "Its left child node index is " << l << ", value is " << (abt.val(l) != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
|
cout << "Its right child node index is " << r << ", value is " << (abt.val(r) != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
|
cout << "Its parent node index is " << p << ", value is " << (abt.val(p) != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
|
|
|
// Traverse tree
|
|
vector<int> res = abt.levelOrder();
|
|
cout << "\nLevel-order traversal: ";
|
|
printVector(res);
|
|
res = abt.preOrder();
|
|
cout << "Pre-order traversal: ";
|
|
printVector(res);
|
|
res = abt.inOrder();
|
|
cout << "In-order traversal: ";
|
|
printVector(res);
|
|
res = abt.postOrder();
|
|
cout << "Post-order traversal: ";
|
|
printVector(res);
|
|
|
|
return 0;
|
|
}
|