Terminate Scanf format strings with \n. To see why, do "go doc fmt.Scanf". 
Always check the result of (at least) any input operation such as Scanf.

> On Apr 19, 2018, at 10:31 AM, Alex Dvoretskiy <advoretski...@gmail.com> wrote:
> 
> Hello Golang-nuts,
> 
> Following code reads data from file and creates binary tree structure:
> 
> '
> // go run main.go < input
> 
> package main
> 
> import "fmt"
> 
> type TreeNode struct {
>       Value int
>       Left *TreeNode
>       Right *TreeNode
> }
> 
> func main () {
>       nodes := read()
> 
>       for i, node := range(nodes) {
>               fmt.Printf("%p\n", &nodes[i])
>               printNode(&node)
>       }
> 
>       //passing root node
>       fmt.Println(nodes)
>       fmt.Println(maxDepth(&nodes[len(nodes) - 1]))
> }
> 
> func maxDepth(root *TreeNode) int {
>       if root == nil {
>               return 0
>       }
>       dl := 1 + maxDepth(root.Left)
>       dr := 1 + maxDepth(root.Right)
>       if dl > dr {
>               return dl
>       } else {
>               return dr
>       }
> }
> 
> func read() []TreeNode {
>       /*test := &Node{
>               1,
>               nil,
>               nil,
>       }
>       right := &Node{
>               33,
>               nil,
>               nil,
>       }
>       test.Right = right
>       fmt.Println(test)
>       printNode(test)*/
> 
>       var N int
>       fmt.Scanf("%d", &N)
>       fmt.Println("N: ", N)
> 
>       var nodes []TreeNode = make([]TreeNode, N)
> 
>       var val, indexLeft, indexRight int
>       for i := 0; i < N; i++ {
>               fmt.Scanf("%d %d %d", &val, &indexLeft, &indexRight)
>               nodes[i].Value = val
>               if indexLeft >= 0 {
>                       nodes[i].Left = &nodes[indexLeft]
>               }
>               if indexRight >= 0 {
>                       nodes[i].Right = &nodes[indexRight]
>               }
>       }
> 
>       return nodes
> }
> 
> func printNode(n *TreeNode) {
>       fmt.Print("Value: ", n.Value)
>       if n.Left != nil {
>               fmt.Print(" Left: ", n.Left.Value)
>       }
>       if n.Right != nil {
>               fmt.Print(" Right: ", n.Right.Value)
>       }
>       fmt.Println()
> }
> '
> Code works fine on Linux64 machine, result: "[{15 <nil> <nil>} {7 <nil> 
> <nil>} {9 <nil> <nil>} {20 0x11958080 0x1195808c} {3 0x11958098 0x119580a4}]"
> maxDepth = 3
> 
> But if, I'm runing this code on windows 32 machine, I'm getting different 
> result: "[{0 0x11a94000 0x11a94000} {15 <nil> <nil>} {15 <nil> <nil>} {7 
> <nil> <nil>} {7 <nil> <nil>}]"
> maxDepth = 1
> which is not correct.
> 
> Go version 10.0. Windows Server Standard 2007 SP2 2007.
> What is wrong? Is it a bug? Or something wrong with my code? 
> 
> 
> This is something to do with fmt.Scanf
> 
> When I don't read data from file using fmt.Scanf the code works fine on both 
> machines:
> https://play.golang.org/p/Rhi5jJKGYjX
> 
> Please advise.
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to