-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(go/tree): support array binary tree (#655)
- Loading branch information
Showing
15 changed files
with
223 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// File: array_binary_tree.go | ||
// Created Time: 2023-07-24 | ||
// Author: Reanon ([email protected]) | ||
|
||
package chapter_tree | ||
|
||
/* 数组表示下的二叉树类 */ | ||
type arrayBinaryTree struct { | ||
tree []any | ||
} | ||
|
||
/* 构造方法 */ | ||
func newArrayBinaryTree(arr []any) *arrayBinaryTree { | ||
return &arrayBinaryTree{ | ||
tree: arr, | ||
} | ||
} | ||
|
||
/* 节点数量 */ | ||
func (abt *arrayBinaryTree) size() int { | ||
return len(abt.tree) | ||
} | ||
|
||
/* 获取索引为 i 节点的值 */ | ||
func (abt *arrayBinaryTree) val(i int) any { | ||
// 若索引越界,则返回 null ,代表空位 | ||
if i < 0 || i >= abt.size() { | ||
return nil | ||
} | ||
return abt.tree[i] | ||
} | ||
|
||
/* 获取索引为 i 节点的左子节点的索引 */ | ||
func (abt *arrayBinaryTree) left(i int) int { | ||
return 2*i + 1 | ||
} | ||
|
||
/* 获取索引为 i 节点的右子节点的索引 */ | ||
func (abt *arrayBinaryTree) right(i int) int { | ||
return 2*i + 2 | ||
} | ||
|
||
/* 获取索引为 i 节点的父节点的索引 */ | ||
func (abt *arrayBinaryTree) parent(i int) int { | ||
return (i - 1) / 2 | ||
} | ||
|
||
/* 层序遍历 */ | ||
func (abt *arrayBinaryTree) levelOrder() []any { | ||
var res []any | ||
// 直接遍历数组 | ||
for i := 0; i < abt.size(); i++ { | ||
if abt.val(i) != nil { | ||
res = append(res, abt.val(i)) | ||
} | ||
} | ||
return res | ||
} | ||
|
||
/* 深度优先遍历 */ | ||
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) { | ||
// 若为空位,则返回 | ||
if abt.val(i) == nil { | ||
return | ||
} | ||
// 前序遍历 | ||
if order == "pre" { | ||
*res = append(*res, abt.val(i)) | ||
} | ||
abt.dfs(abt.left(i), order, res) | ||
// 中序遍历 | ||
if order == "in" { | ||
*res = append(*res, abt.val(i)) | ||
} | ||
abt.dfs(abt.right(i), order, res) | ||
// 后序遍历 | ||
if order == "post" { | ||
*res = append(*res, abt.val(i)) | ||
} | ||
} | ||
|
||
/* 前序遍历 */ | ||
func (abt *arrayBinaryTree) preOrder() []any { | ||
var res []any | ||
abt.dfs(0, "pre", &res) | ||
return res | ||
} | ||
|
||
/* 中序遍历 */ | ||
func (abt *arrayBinaryTree) inOrder() []any { | ||
var res []any | ||
abt.dfs(0, "in", &res) | ||
return res | ||
} | ||
|
||
/* 后序遍历 */ | ||
func (abt *arrayBinaryTree) postOrder() []any { | ||
var res []any | ||
abt.dfs(0, "post", &res) | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// File: array_binary_tree_test.go | ||
// Created Time: 2023-07-24 | ||
// Author: Reanon ([email protected]) | ||
|
||
package chapter_tree | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/krahets/hello-algo/pkg" | ||
) | ||
|
||
func TestArrayBinaryTree(t *testing.T) { | ||
// 初始化二叉树 | ||
// 这里借助了一个从数组直接生成二叉树的函数 | ||
arr := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15} | ||
root := SliceToTree(arr) | ||
fmt.Println("\n初始化二叉树") | ||
fmt.Println("二叉树的数组表示:") | ||
fmt.Println(arr) | ||
fmt.Println("二叉树的链表表示:") | ||
PrintTree(root) | ||
|
||
// 数组表示下的二叉树类 | ||
abt := newArrayBinaryTree(arr) | ||
|
||
// 访问节点 | ||
i := 1 | ||
l := abt.left(i) | ||
r := abt.right(i) | ||
p := abt.parent(i) | ||
fmt.Println("\n当前节点的索引为", i, ",值为", abt.val(i)) | ||
fmt.Println("其左子节点的索引为", l, ",值为", abt.val(l)) | ||
fmt.Println("其右子节点的索引为", r, ",值为", abt.val(r)) | ||
fmt.Println("其父节点的索引为", p, ",值为", abt.val(p)) | ||
|
||
// 遍历树 | ||
res := abt.levelOrder() | ||
fmt.Println("\n层序遍历为:", res) | ||
res = abt.preOrder() | ||
fmt.Println("前序遍历为:", res) | ||
res = abt.inOrder() | ||
fmt.Println("中序遍历为:", res) | ||
res = abt.postOrder() | ||
fmt.Println("后序遍历为:", res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.