Golang:如何用go2go将二叉搜索树改造为通用数据结构
Golang:如何用go2go将二叉搜索树改造为通用数据结构 大家好,
想必大家都知道,泛型数据类型和函数即将在未来的某个Go版本中发布。
当泛型提案公布,以及随之而来的go2go转译器和在线运行环境推出时,已经有很多文章讨论了这一新的语言特性,以及如何创建泛型数据类型或函数。
最近,我有了一个不同的想法:将现有代码转换为泛型代码会怎样?
于是,我从一篇较早的博客文章中选取了一个容器数据类型——平衡搜索树——并将其转换成了一个泛型搜索树。毕竟,这能有多难呢?
我将我的探索过程和结果写成了一篇新的博客文章:appliedgo.net/generictree
希望你们喜欢,并期待听到你们的想法!
更多关于Golang:如何用go2go将二叉搜索树改造为通用数据结构的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于Golang:如何用go2go将二叉搜索树改造为通用数据结构的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
泛型二叉搜索树实现示例
以下是将传统二叉搜索树改造为泛型数据结构的完整示例:
package main
import (
"fmt"
"golang.org/x/exp/constraints"
)
// 泛型节点结构
type TreeNode[T constraints.Ordered] struct {
value T
left *TreeNode[T]
right *TreeNode[T]
}
// 泛型二叉搜索树
type BinarySearchTree[T constraints.Ordered] struct {
root *TreeNode[T]
}
// 插入方法
func (bst *BinarySearchTree[T]) Insert(value T) {
newNode := &TreeNode[T]{value: value}
if bst.root == nil {
bst.root = newNode
return
}
current := bst.root
for {
if value < current.value {
if current.left == nil {
current.left = newNode
return
}
current = current.left
} else {
if current.right == nil {
current.right = newNode
return
}
current = current.right
}
}
}
// 查找方法
func (bst *BinarySearchTree[T]) Search(value T) bool {
current := bst.root
for current != nil {
if value == current.value {
return true
}
if value < current.value {
current = current.left
} else {
current = current.right
}
}
return false
}
// 中序遍历
func (bst *BinarySearchTree[T]) InOrderTraversal() []T {
var result []T
var traverse func(node *TreeNode[T])
traverse = func(node *TreeNode[T]) {
if node == nil {
return
}
traverse(node.left)
result = append(result, node.value)
traverse(node.right)
}
traverse(bst.root)
return result
}
// 泛型最小值查找
func (bst *BinarySearchTree[T]) Min() (T, bool) {
if bst.root == nil {
var zero T
return zero, false
}
current := bst.root
for current.left != nil {
current = current.left
}
return current.value, true
}
// 泛型最大值查找
func (bst *BinarySearchTree[T]) Max() (T, bool) {
if bst.root == nil {
var zero T
return zero, false
}
current := bst.root
for current.right != nil {
current = current.right
}
return current.value, true
}
func main() {
// 整数类型二叉搜索树
intTree := &BinarySearchTree[int]{}
intTree.Insert(50)
intTree.Insert(30)
intTree.Insert(70)
intTree.Insert(20)
intTree.Insert(40)
fmt.Println("整数树中序遍历:", intTree.InOrderTraversal())
fmt.Println("查找40:", intTree.Search(40))
fmt.Println("查找100:", intTree.Search(100))
// 浮点数类型二叉搜索树
floatTree := &BinarySearchTree[float64]{}
floatTree.Insert(3.14)
floatTree.Insert(2.71)
floatTree.Insert(1.618)
fmt.Println("\n浮点数树中序遍历:", floatTree.InOrderTraversal())
// 字符串类型二叉搜索树
stringTree := &BinarySearchTree[string]{}
stringTree.Insert("apple")
stringTree.Insert("banana")
stringTree.Insert("cherry")
fmt.Println("\n字符串树中序遍历:", stringTree.InOrderTraversal())
// 获取最小值和最大值
if min, ok := intTree.Min(); ok {
fmt.Println("整数树最小值:", min)
}
if max, ok := intTree.Max(); ok {
fmt.Println("整数树最大值:", max)
}
}
关键改造点说明
- 类型参数声明:使用
[T constraints.Ordered]声明泛型类型参数,确保类型可比较 - 结构体泛型化:将
TreeNode和BinarySearchTree结构体改为泛型结构 - 方法泛型化:所有方法都使用相同的类型参数
T - 比较操作:依赖
constraints.Ordered确保类型支持<、>、==等比较操作 - 零值处理:使用
var zero T获取类型的零值
编译和运行
使用 Go 1.18+ 版本编译:
go run generictree.go
输出示例:
整数树中序遍历: [20 30 40 50 70]
查找40: true
查找100: false
浮点数树中序遍历: [1.618 2.71 3.14]
字符串树中序遍历: [apple banana cherry]
整数树最小值: 20
整数树最大值: 70
这个实现展示了如何将传统的二叉搜索树改造为完全泛型的数据结构,支持任何可比较类型(int、float64、string等),同时保持了类型安全和编译时检查的优势。

