AdventOfCode2021/shared/BinaryTree.go
Luke Else 5bd95a716f day 18a incomplete
was unable to complete the challenge today

Ideally, I would like to wait for generics to release in Go version 1.18 (February 2022)

decided to spend the time working on an implementation of a Binary Search Tree that makes use of the string that is currently stored in the Node Item
2021-12-18 22:09:37 +00:00

77 lines
1.3 KiB
Go

package shared
import (
"fmt"
"strconv"
)
type BinaryTree struct {
Head *Node
}
func (t *BinaryTree) Insert(val int) {
if t.Head == nil {
t.Head = &Node{Value: strconv.Itoa(val), Left: nil, Right: nil}
return
}
current := t.Head
var parent *Node = nil
for {
currentVal, _ := strconv.Atoi(current.Value)
parent = current
left := true
if val > currentVal {
left = false
_, current = t.Traverse(current)
} else {
current, _ = t.Traverse(current)
}
if current == nil {
current = &Node{Value: strconv.Itoa(val), Left: nil, Right: nil, Parent: parent}
if left {
parent.Left = current
} else {
parent.Right = current
}
return
}
}
}
func (t *BinaryTree) InOrder(start *Node) {
if start.Left != nil {
t.InOrder(start.Left)
}
fmt.Print(start.Value, " ")
if start.Right != nil {
t.InOrder(start.Right)
}
}
func (t *BinaryTree) PreOrder(start *Node) {
fmt.Print(start.Value, " ")
if start.Left != nil {
t.PreOrder(start.Left)
}
if start.Right != nil {
t.PreOrder(start.Right)
}
}
func (t *BinaryTree) PostOrder(start *Node) {
if start.Left != nil {
t.PostOrder(start.Left)
}
if start.Right != nil {
t.PostOrder(start.Right)
}
fmt.Print(start.Value, " ")
}
func (t *BinaryTree) Traverse(current *Node) (left *Node, right *Node) {
return current.Left, current.Right
}