mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
* 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
102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
// File: array_binary_tree.go
|
|
// Created Time: 2023-07-24
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_tree
|
|
|
|
/* Binary tree class represented by array */
|
|
type arrayBinaryTree struct {
|
|
tree []any
|
|
}
|
|
|
|
/* Constructor */
|
|
func newArrayBinaryTree(arr []any) *arrayBinaryTree {
|
|
return &arrayBinaryTree{
|
|
tree: arr,
|
|
}
|
|
}
|
|
|
|
/* List capacity */
|
|
func (abt *arrayBinaryTree) size() int {
|
|
return len(abt.tree)
|
|
}
|
|
|
|
/* Get value of node at index i */
|
|
func (abt *arrayBinaryTree) val(i int) any {
|
|
// If index out of bounds, return null to represent empty position
|
|
if i < 0 || i >= abt.size() {
|
|
return nil
|
|
}
|
|
return abt.tree[i]
|
|
}
|
|
|
|
/* Get index of left child node of node at index i */
|
|
func (abt *arrayBinaryTree) left(i int) int {
|
|
return 2*i + 1
|
|
}
|
|
|
|
/* Get index of right child node of node at index i */
|
|
func (abt *arrayBinaryTree) right(i int) int {
|
|
return 2*i + 2
|
|
}
|
|
|
|
/* Get index of parent node of node at index i */
|
|
func (abt *arrayBinaryTree) parent(i int) int {
|
|
return (i - 1) / 2
|
|
}
|
|
|
|
/* Level-order traversal */
|
|
func (abt *arrayBinaryTree) levelOrder() []any {
|
|
var res []any
|
|
// Traverse array directly
|
|
for i := 0; i < abt.size(); i++ {
|
|
if abt.val(i) != nil {
|
|
res = append(res, abt.val(i))
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
/* Depth-first traversal */
|
|
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {
|
|
// If empty position, return
|
|
if abt.val(i) == nil {
|
|
return
|
|
}
|
|
// Preorder traversal
|
|
if order == "pre" {
|
|
*res = append(*res, abt.val(i))
|
|
}
|
|
abt.dfs(abt.left(i), order, res)
|
|
// Inorder traversal
|
|
if order == "in" {
|
|
*res = append(*res, abt.val(i))
|
|
}
|
|
abt.dfs(abt.right(i), order, res)
|
|
// Postorder traversal
|
|
if order == "post" {
|
|
*res = append(*res, abt.val(i))
|
|
}
|
|
}
|
|
|
|
/* Preorder traversal */
|
|
func (abt *arrayBinaryTree) preOrder() []any {
|
|
var res []any
|
|
abt.dfs(0, "pre", &res)
|
|
return res
|
|
}
|
|
|
|
/* Inorder traversal */
|
|
func (abt *arrayBinaryTree) inOrder() []any {
|
|
var res []any
|
|
abt.dfs(0, "in", &res)
|
|
return res
|
|
}
|
|
|
|
/* Postorder traversal */
|
|
func (abt *arrayBinaryTree) postOrder() []any {
|
|
var res []any
|
|
abt.dfs(0, "post", &res)
|
|
return res
|
|
}
|