Skip to content

Instantly share code, notes, and snippets.

@dantheman213
Created September 5, 2019 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantheman213/41448eb2f5aa54afbfaa33ab70ee0943 to your computer and use it in GitHub Desktop.
Save dantheman213/41448eb2f5aa54afbfaa33ab70ee0943 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
fmt.Println("Welcome to B-Tree test")
btree := makeBTree()
fmt.Println("In-order b-tree traversal")
traverse(&btree)
fmt.Println("Exit B-Tree test")
}
type Node struct {
value int
leftChild *Node
rightChild *Node
}
func makeBTree() Node {
fmt.Println("Making binary tree ...")
// level 0 - root
// 0
root := Node{0, nil, nil}
// level 1
// 0
// 1 2
root.leftChild = &Node{1, nil, nil}
root.rightChild = &Node{2, nil, nil}
// level 2
// 0
// 1 . 2
// 3 4 5 . 6
l2l := root.leftChild
l2l.leftChild = &Node{3, nil, nil}
l2l.rightChild = &Node{4, nil, nil}
l2r := root.rightChild
l2r.leftChild = &Node{5, nil, nil}
l2r.rightChild = &Node{6, nil, nil}
// level 3
// 0
// 1 . 2
// 3 4 5 . 6
fmt.Println("Made binary tree!")
return root
}
func traverse(tree *Node) {
if tree == nil {
return
}
fmt.Printf("%d ", tree.value)
if tree.leftChild != nil {
traverse(tree.leftChild)
}
if tree.rightChild != nil {
traverse(tree.rightChild)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment