Dan*_*any 1 struct linked-list list go
在我的用例中,我想知道如何在Go中实现以下Java代码
class TreeNode {
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(){}
}
LinkedList<TreeNode> treeList = new LinkedList<TreeNode>();
Run Code Online (Sandbox Code Playgroud)
我能够导入容器/列表包并添加一个接口.但它不允许任何通用对象.我是否必须使用TreeNode结构实现我自己的列表版本?
我只需要知道如何LinkedList<T>在Go中工作.
编辑1: 为了说清楚,我在这里添加完整的代码.我试图找到二叉树中每个深度的所有节点的链表.我使用了两个包列表和二叉树.您可以在此处找到binarytree的源代码并在此处列出.list与容器/列表相同,但我添加了一些额外的功能
package main
import (
"fmt"
"go/chapter02-linkedlists/list"
"go/chapter04-treesandgraphs/binarytree"
)
func main() {
inArr := []int{4, 5, 7, 8, 9}
t1 := binarytree.NewMinimalHeightBST(inArr, 0, len(inArr)-1)
binarytree.InOrderTraverse(t1)
var nodeList []*list.List
nodeList = getLevelbasedList(t1, 0)
fmt.Println()
for _, value := range nodeList {
fmt.Print("[ ")
for x := value.Front(); x != nil; x = x.Next() {
fmt.Print(x.Value.(int), " ")
}
fmt.Println("]")
}
}
func getLevelbasedList(root *binarytree.Tree, level int) []*list.List {
if root == nil {
return nil
}
var nodeList []*list.List
parents := list.New()
current := list.New()
current.PushFront(root)
for current.Len() > 0 {
nodeList = append(nodeList, current)
parents = current
current = list.New()
for x := current.Front(); x != nil; x = x.Next() {
node := x.Value.(*binarytree.Tree)
if node.Left != nil {
current = current.PushFront(node.Left)
}
if node.Right != nil {
current = current.PushFront(node.Right)
}
}
return nodeList
}
}
Run Code Online (Sandbox Code Playgroud)
错误是,
./question4_4b.go:56: cannot use current.PushFront((interface {})(node.Left)) (type *list.Element) as type *list.List in assignment
./question4_4b.go:59: cannot use current.PushFront((interface {})(node.Right)) (type *list.Element) as type *list.List in assignment
Run Code Online (Sandbox Code Playgroud)
编辑2:根据我编辑的JamesHenstridge的评论
current = current.PushFront(node.Left)
Run Code Online (Sandbox Code Playgroud)
至
current.PushFront(node.Left)
Run Code Online (Sandbox Code Playgroud)
问题解决了.但现在我得到接口转换错误,
[ panic: interface conversion: interface is *binarytree.Tree, not int
goroutine 1 [running]:
Run Code Online (Sandbox Code Playgroud)
Go不支持泛型类型(请参阅常见问题解答为什么Go没有泛型类型?).
您必须使用Type断言来获取所需的类型值.
例如创建你的TreeNode类型:
type TreeNode struct {
Data int
Left *TreeNode
Right *TreeNode
}
Run Code Online (Sandbox Code Playgroud)
并迭代包含TreeNode值的列表:
l := list.New()
// Populate list
for e := l.Front(); e != nil; e = e.Next() {
if tn, ok := e.Value.(TreeNode); ok {
// do something with tn which is of type TreeNode
fmt.Println(tn)
} else {
// e.Value is not of type TreeNode
}
}
Run Code Online (Sandbox Code Playgroud)
如果组装列表并且可以确定它只包含类型的值TreeNode,则可以省略类型断言中的错误检查,它变为如下所示:
for e := l.Front(); e != nil; e = e.Next() {
// if e.Value would not be of type TreeNode, run-time panic would occur
tn := e.Value.(TreeNode) // tn is of type TreeNode
fmt.Println(tn)
}
Run Code Online (Sandbox Code Playgroud)
编辑:
你得到的错误:
cannot use current.PushFront((interface {})(node.Left)) (type *list.Element)
as type *list.List in assignment
Run Code Online (Sandbox Code Playgroud)
在线:
current = current.PushFront(node.Left)
Run Code Online (Sandbox Code Playgroud)
的current变量的类型的list.List,并且该方法current.PushFront()返回类型的值*list.Element.这些是两种不同的类型,您不能将a分配给*Element具有类型的变量List.
编辑2:
你的第二个错误:
panic: interface conversion: interface is *binarytree.Tree, not int
Run Code Online (Sandbox Code Playgroud)
是由线引起的:
fmt.Print(x.Value.(int), " ")
Run Code Online (Sandbox Code Playgroud)
你试图断言值x.Value是类型int但不是!x.Value是类型*binarytree.Tree所以断言显然会失败.