Files
hello-algo/en/codes/go/chapter_tree/array_binary_tree_test.go
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* 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
2025-12-31 07:44:52 +08:00

48 lines
1.3 KiB
Go

// File: array_binary_tree_test.go
// Created Time: 2023-07-24
// Author: Reanon (793584285@qq.com)
package chapter_tree
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestArrayBinaryTree(t *testing.T) {
// Initialize binary tree
// Here we use a function to generate a binary tree directly from an array
arr := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
root := SliceToTree(arr)
fmt.Println("\nInitialize binary tree")
fmt.Println("Array representation of binary tree:")
fmt.Println(arr)
fmt.Println("Linked list representation of binary tree:")
PrintTree(root)
// Binary tree class represented by array
abt := newArrayBinaryTree(arr)
// Access node
i := 1
l := abt.left(i)
r := abt.right(i)
p := abt.parent(i)
fmt.Println("\nCurrent node index is", i, ", value is", abt.val(i))
fmt.Println("Its left child node index is", l, ", value is", abt.val(l))
fmt.Println("Its right child node index is", r, ", value is", abt.val(r))
fmt.Println("Its parent node index is", p, ", value is", abt.val(p))
// Traverse tree
res := abt.levelOrder()
fmt.Println("\nLevel-order traversal is:", res)
res = abt.preOrder()
fmt.Println("Preorder traversal is:", res)
res = abt.inOrder()
fmt.Println("Inorder traversal is:", res)
res = abt.postOrder()
fmt.Println("Postorder traversal is:", res)
}