Re: [go-nuts] binary tree. different results on linux 64 and windows 32 machines.
Thanks a lot!!! On Thursday, April 19, 2018 at 11:55:49 AM UTC-7, Bakul Shah wrote: > > 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> 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", [i]) > printNode() > } > > //passing root node > fmt.Println(nodes) > fmt.Println(maxDepth([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 := { > 1, > nil, > nil, > } > right := { > 33, > nil, > nil, > } > test.Right = right > fmt.Println(test) > printNode(test)*/ > > var N int > fmt.Scanf("%d", ) > 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", , , ) > nodes[i].Value = val > if indexLeft >= 0 { > nodes[i].Left = [indexLeft] > } > if indexRight >= 0 { > nodes[i].Right = [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 } {7 > } {9 } {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 } {15 } {7 > } {7 }]" > 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...@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.
Re: [go-nuts] binary tree. different results on linux 64 and windows 32 machines.
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 Dvoretskiywrote: > > 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", [i]) > printNode() > } > > //passing root node > fmt.Println(nodes) > fmt.Println(maxDepth([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 := { > 1, > nil, > nil, > } > right := { > 33, > nil, > nil, > } > test.Right = right > fmt.Println(test) > printNode(test)*/ > > var N int > fmt.Scanf("%d", ) > 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", , , ) > nodes[i].Value = val > if indexLeft >= 0 { > nodes[i].Left = [indexLeft] > } > if indexRight >= 0 { > nodes[i].Right = [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 } {7 > } {9 } {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 } {15 } {7 > } {7 }]" > 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.
[go-nuts] binary tree. different results on linux 64 and windows 32 machines.
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", [i]) printNode() } //passing root node fmt.Println(nodes) fmt.Println(maxDepth([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 := { 1, nil, nil, } right := { 33, nil, nil, } test.Right = right fmt.Println(test) printNode(test)*/ var N int fmt.Scanf("%d", ) 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", , , ) nodes[i].Value = val if indexLeft >= 0 { nodes[i].Left = [indexLeft] } if indexRight >= 0 { nodes[i].Right = [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 } {7 } {9 } {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 } {15 } {7 } {7 }]" 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.