我的GO中的BST代码。我一直收到此错误消息。我正在使用着名的垫,我是一个初学者。 错误在我的for循环中。在insertList函数下。
type node struct{
left *node
right *node
val int
}
func insert(tree *node, element int) *node{
if tree == nil{
tree = &node{nil, nil, element}
} else if element > tree.val{
tree.right = insert( tree.right, element)
} else if element < tree.val{
tree.left = insert( tree.left, element)
}
return tree
}
func insertList(elementList []int) *node{
if tree == nil{
for i:=0; i<[]int.len; i++{
tree = insert([i]int)}
return tree}}
func displayBST(tree *node){
if ( tree != nil) {
displayBST( tree.left)
fmt.Println(tree.val)
displayBST(tree.right)}}
func main(){
l := [10]int{100, 3, 3, 200, 5, 8, 5, 200, 0, -4}
s := l[:]
insertList(s)
displayBST(insertList(s))
fmt.Println()}
答案 0 :(得分:0)
这可以让你超越语法错误:
package main
import "fmt"
type node struct {
left *node
right *node
val int
}
func insert(tree *node, element int) *node {
if tree == nil {
tree = &node{nil, nil, element}
} else if element > tree.val {
tree.right = insert(tree.right, element)
} else if element < tree.val {
tree.left = insert(tree.left, element)
}
return tree
}
func insertList(tree *node, elementList []int) *node {
if tree == nil {
for i := 0; i < len(elementList); i++ {
tree = insert(tree, elementList[i])
}
}
return tree
}
func displayBST(tree *node) {
if tree != nil {
displayBST(tree.left)
fmt.Println(tree.val)
displayBST(tree.right)
}
}
func main() {
l := [10]int{100, 3, 3, 200, 5, 8, 5, 200, 0, -4}
s := l[:]
var t *node
insertList(t, s)
displayBST(insertList(t, s))
fmt.Println()
}