如何在Golang中使用struct创建二叉树?

huangapple go评论88阅读模式
英文:

How to create a binary tree use struct in golang?

问题

我想创建一个二叉树并使用Go语言初始化该树。
代码如下:

package Tree

import "fmt"

type TreeNode struct {
    Left  *TreeNode
    Right *TreeNode
    Value int
}


func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
    if tree == nil {
        tree = node
    }
    if node.Value > tree.Value {
        InsertNodeToTree(tree.Right, node)
    }
    if node.Value < tree.Value {
        InsertNodeToTree(tree.Left, node)
    }
}

func InitTree(values ...int) (root *TreeNode) {
    rootNode := TreeNode{Value: values[0]}
    for _, value := range values {
        node := TreeNode{Value:value}
        InsertNodeToTree(&rootNode, &node)
    }
    return &rootNode
}

func main() {
    treeNode := InitTree(5, 4, 6, 8, 9, 7, 1, 3, 2)
    fmt.Println(treeNode)
}

为什么树的左右节点都是nil?
我传递了树节点的引用,为什么不起作用?

英文:

I want to create a binary tree and initialize the tree use golang.
And the codes like these:

package Tree

import &quot;fmt&quot;

type TreeNode struct {
	Left  *TreeNode
	Right *TreeNode
	Value int
}


func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
	if tree == nil {
		tree = node
	}
	if node.Value &gt; tree.Value {
		InsertNodeToTree(tree.Right, node)
	}
	if node.Value &lt; tree.Value {
		InsertNodeToTree(tree.Left, node)
	}
}

func InitTree(values ...int) (root *TreeNode) {
	rootNode := TreeNode{Value: values[0]}
	for _, value := range values {
		node := TreeNode{Value:value}
		InsertNodeToTree(&amp;rootNode, &amp;node)
	}
	return &amp;rootNode
}

func main() {
	treeNode := InitTree(5, 4, 6, 8, 9, 7, 1, 3, 2)
	fmt.Println(treeNode)
}

Why the tree's left and right are nil?
I pass the reference of the tree node, why not work?

答案1

得分: 4

tree只是指针的一个副本。对变量进行赋值是无用的。相反,你需要将其赋值给一个已经存在的节点。例如:

func InsertNodeToTree(tree *TreeNode, node *TreeNode) {
    if tree == nil {
        panic("cannot insert into nil root")
    }

    if node.Value > tree.Value {
        if tree.Right == nil {
            tree.Right = node
        } else {
            InsertNodeToTree(tree.Right, node)
        }
    }
    if node.Value < tree.Value {
        if tree.Left == nil {
            tree.Left = node
        } else {
            InsertNodeToTree(tree.Left, node)
        }
    }
}
英文:

tree is only a copy of the pointer. Assigning to the variable is useless. Instead, you need to assign to an already existing node. For example:

https://play.golang.org/p/Agzby-Yinq

func InsertNodeToTree(tree *TreeNode, node *TreeNode) {
	if tree == nil {
		panic(&quot;cannot insert into nil root&quot;)
	}

	if node.Value &gt; tree.Value {
		if tree.Right == nil {
			tree.Right = node
		} else {
			InsertNodeToTree(tree.Right, node)
		}
	}
	if node.Value &lt; tree.Value {
		if tree.Left == nil {
			tree.Left = node
		} else {
			InsertNodeToTree(tree.Left, node)
		}
	}
}

答案2

得分: 4

在C/C++编程语言中,你可以使用TreeNode *&tree。但在Go编程语言中,你不能使用*&tree只是指针的一个副本,所以你不能将值指向另一个TreeNode。我修改了你的程序,现在可以成功运行。也许你需要以下代码:

package Tree

type TreeNode struct {
    Left  *TreeNode
    Right *TreeNode
    Value int
}


var DefaultValue int = -1024


func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
    if tree == nil {
        return
    }
    if tree.Value == DefaultValue {
        tree.Value = node.Value
        return
    }
    if node.Value > tree.Value {
        if tree.Right == nil {
            tree.Right = &TreeNode{Value: DefaultValue}
        }
        InsertNodeToTree(tree.Right, node)
    }
    if node.Value < tree.Value {
        if tree.Left == nil {
            tree.Left = &TreeNode{Value: DefaultValue}
        }
        InsertNodeToTree(tree.Left, node)
    }
}

func InitTree(values ...int) (root *TreeNode) {
    rootNode := TreeNode{Value: DefaultValue, Right: nil, Left: nil}
    for _, value := range values {
        node := TreeNode{Value:value}
        InsertNodeToTree(&rootNode, &node)
    }
    return &rootNode
}
英文:

In C/C++ programming language, you can use TreeNode *&amp;tree.<br/>
But in golang programming language, you can not use *&amp;.<br/>
tree is just a copy of the pointer, so you can't point the value to another TreeNode.<br/>
I modified your program, and it can run successfully now.<br/>
Maybe these codes you need:

package Tree

type TreeNode struct {
	Left  *TreeNode
	Right *TreeNode
	Value int
}


var DefaultValue int = -1024


func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
	if tree == nil {
		return
	}
	if tree.Value == DefaultValue {
		tree.Value = node.Value
		return
	}
	if node.Value &gt; tree.Value {
		if tree.Right == nil {
			tree.Right = &amp;TreeNode{Value: DefaultValue}
		}
		InsertNodeToTree(tree.Right, node)
	}
	if node.Value &lt; tree.Value {
		if tree.Left == nil {
			tree.Left = &amp;TreeNode{Value: DefaultValue}
		}
		InsertNodeToTree(tree.Left, node)
	}
}

func InitTree(values ...int) (root *TreeNode) {
	rootNode := TreeNode{Value: DefaultValue, Right: nil, Left: nil}
	for _, value := range values {
		node := TreeNode{Value:value}
		InsertNodeToTree(&amp;rootNode, &amp;node)
	}
	return &amp;rootNode
}

huangapple
  • 本文由 发表于 2017年9月5日 11:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/46046800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定