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
This commit is contained in:
76
shared/BinaryTree.go
Normal file
76
shared/BinaryTree.go
Normal file
@ -0,0 +1,76 @@
|
||||
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
|
||||
}
|
@ -1,6 +1,18 @@
|
||||
package shared
|
||||
|
||||
//Waiting for generic types to release,
|
||||
//this will allow for a Node with a different value type
|
||||
|
||||
type Node struct {
|
||||
Value string
|
||||
Next *Node
|
||||
|
||||
//Universal
|
||||
Parent *Node
|
||||
|
||||
//For LLs, Stacks, Queues...
|
||||
Next *Node
|
||||
|
||||
//For Binary Trees...
|
||||
Left *Node
|
||||
Right *Node
|
||||
}
|
||||
|
Reference in New Issue
Block a user