Created
September 5, 2019 00:43
-
-
Save dantheman213/41448eb2f5aa54afbfaa33ab70ee0943 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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