"binary tree in golang" Code Answer's
You're definitely familiar with the best coding language Go that developers use to develop their projects and they get all their queries like "binary tree in golang" answered properly. Developers are finding an appropriate answer about binary tree in golang related to the Go coding language. By visiting this online portal developers get answers concerning Go codes question like binary tree in golang. Enter your desired code related query in the search bar and get every piece of information about Go code related question on binary tree in golang.
binary tree in golang
// Binary Tree in Golang
package main
import (
"fmt"
"os"
"io"
)
type BinaryNode struct {
left *BinaryNode
right *BinaryNode
data int64
}
type BinaryTree struct {
root *BinaryNode
}
func (t *BinaryTree) insert(data int64) *BinaryTree {
if t.root == nil {
t.root = &BinaryNode{data: data, left: nil, right: nil}
} else {
t.root.insert(data)
}
return t
}
func (n *BinaryNode) insert(data int64) {
if n == nil {
return
} else if data <= n.data {
if n.left == nil {
n.left = &BinaryNode{data: data, left: nil, right: nil}
} else {
n.left.insert(data)
}
} else {
if n.right == nil {
n.right = &BinaryNode{data: data, left: nil, right: nil}
} else {
n.right.insert(data)
}
}
}
func print(w io.Writer, node *BinaryNode, ns int, ch rune) {
if node == nil {
return
}
for i := 0; i < ns; i++ {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%c:%v\n", ch, node.data)
print(w, node.left, ns+2, 'L')
print(w, node.right, ns+2, 'R')
}
func main() {
tree := &BinaryTree{}
tree.insert(100).
insert(-20).
insert(-50).
insert(-15).
insert(-60).
insert(50).
insert(60).
insert(55).
insert(85).
insert(15).
insert(5).
insert(-10)
print(os.Stdout, tree.root, 0, 'M')
}
Source: www.golangprograms.com
All those coders who are working on the Go based application and are stuck on binary tree in golang can get a collection of related answers to their query. Programmers need to enter their query on binary tree in golang related to Go code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about binary tree in golang for the programmers working on Go code while coding their module. Coders are also allowed to rectify already present answers of binary tree in golang while working on the Go language code. Developers can add up suggestions if they deem fit any other answer relating to "binary tree in golang". Visit this developer's friendly online web community, CodeProZone, and get your queries like binary tree in golang resolved professionally and stay updated to the latest Go updates.