Skip to content

Commit

Permalink
Make node more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher committed Sep 14, 2023
1 parent a4eb6d3 commit fc6fa88
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 176 deletions.
24 changes: 12 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
func main() {
tr := tree.New[string]()

tr.AddRoot(tree.NewNode(0, "0.0"))
tr.AddRoot(tree.NewNodeWithID(0, "0.0"))

tr.Add(0, tree.NewNode(1, "0.1"))
tr.Add(0, tree.NewNode(2, "0.2"))
tr.Add(0, tree.NewNodeWithID(1, "0.1"))
tr.Add(0, tree.NewNodeWithID(2, "0.2"))

tr.Add(1, tree.NewNode(3, "1.3"))
tr.Add(1, tree.NewNode(4, "1.4"))
tr.Add(1, tree.NewNodeWithID(3, "1.3"))
tr.Add(1, tree.NewNodeWithID(4, "1.4"))

tr.Add(2, tree.NewNode(5, "2.5"))
tr.Add(2, tree.NewNode(6, "2.6"))
tr.Add(2, tree.NewNodeWithID(5, "2.5"))
tr.Add(2, tree.NewNodeWithID(6, "2.6"))

root, ok := tr.GetRoot()
fmt.Println(ok) // true
fmt.Println(root.Get()) // 0, 0.0
fmt.Println(ok) // true
fmt.Println(root.GetData()) // 0.0

node, ok := tr.Get(3)
fmt.Println(ok) // true
fmt.Println(node.Get()) // 3, 1.3
fmt.Println(ok) // true
fmt.Println(node.GetData()) // 1.3

structure, ok := tr.GetStructure()
fmt.Println(ok) // true
Expand All @@ -36,6 +36,6 @@ func main() {
nodes, ok := tr.Backtrack(6)
fmt.Println(ok) // true
for _, node := range nodes {
fmt.Println(node.Get()) // 6, 2.6; 2, 0.2; 0, 0.0
fmt.Println(node.GetData()) // 2.6; 0.2; 0.0
}
}
100 changes: 59 additions & 41 deletions tree/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func ExampleNew() {
tr := tree.New[string]()

// Add nodes do tree
tr.AddRoot(tree.NewNode(0, "root"))
tr.AddRoot(tree.NewNodeWithID(0, "root"))

// Do more things
}
Expand All @@ -20,63 +20,63 @@ func ExampleNew() {
func ExampleTree_AddRoot() {
tr := tree.New[int]()

tr.AddRoot(tree.NewNode(0, 42))
tr.AddRoot(tree.NewNodeWithID(0, 42))

// Do more things
}

// ExampleTree_GetRoot demonstrates how to retrieve root node from tree.
func ExampleTree_GetRoot() {
tr := tree.New[float64]()
tr.AddRoot(tree.NewNode(0, 3.14))
tr.AddRoot(tree.NewNodeWithID(0, 3.14))

node, ok := tr.GetRoot()
if !ok {
return
}
fmt.Println(node.Get())
fmt.Println(node.GetData())

// Do more things
}

// ExampleTree_Add demonstrates how to add node to tree.
func ExampleTree_Add() {
tr := tree.New[bool]()
tr.AddRoot(tree.NewNode(0, true))
tr.AddRoot(tree.NewNodeWithID(0, true))

tr.Add(0, tree.NewNode(1, false))
tr.Add(0, tree.NewNodeWithID(1, false))

// Do more things
}

// ExampleTree_Get demonstrates how to retrieve node from tree.
func ExampleTree_Get() {
tr := tree.New[uint]()
tr.AddRoot(tree.NewNode(0, uint(42)))
tr.AddRoot(tree.NewNodeWithID(0, uint(42)))

node, ok := tr.Get(0)
if !ok {
return
}
fmt.Println(node.Get())
fmt.Println(node.GetData())

// Do more things
}

// ExampleTree_Backtrack demonstrates how to retrieve path of nodes from node to root.
func ExampleTree_Backtrack() {
tr := tree.New[string]()
tr.AddRoot(tree.NewNode(0, "root"))
tr.Add(0, tree.NewNode(1, "level1"))
tr.Add(1, tree.NewNode(2, "level2"))
tr.Add(2, tree.NewNode(3, "leaf"))
tr.AddRoot(tree.NewNodeWithID(0, "root"))
tr.Add(0, tree.NewNodeWithID(1, "level1"))
tr.Add(1, tree.NewNodeWithID(2, "level2"))
tr.Add(2, tree.NewNodeWithID(3, "leaf"))

nodes, ok := tr.Backtrack(3)
if !ok {
return
}
for _, node := range nodes {
fmt.Println(node.Get())
fmt.Println(node.GetData())
}

// Do more things
Expand All @@ -85,10 +85,10 @@ func ExampleTree_Backtrack() {
// ExampleTree_GetStructure demonstrates how to retrieve tree structure.
func ExampleTree_GetStructure() {
tr := tree.New[string]()
tr.AddRoot(tree.NewNode(0, "root"))
tr.Add(0, tree.NewNode(1, "level1"))
tr.Add(1, tree.NewNode(2, "level2"))
tr.Add(2, tree.NewNode(3, "leaf"))
tr.AddRoot(tree.NewNodeWithID(0, "root"))
tr.Add(0, tree.NewNodeWithID(1, "level1"))
tr.Add(1, tree.NewNodeWithID(2, "level2"))
tr.Add(2, tree.NewNodeWithID(3, "leaf"))

structure, ok := tr.GetStructure()
if !ok {
Expand All @@ -101,30 +101,48 @@ func ExampleTree_GetStructure() {
// Do more things
}

// NewNodeWithID demonstrates how to create a node with ID.
func ExampleNewNodeWithID() {
n := tree.NewNodeWithID(0, "node")

n.GetData()

// Do more things
}

// ExampleNewNode demonstrates how to create a node.
func ExampleNewNode() {
n := tree.NewNode(0, "node")
n := tree.NewNode("node")

n.Get()
n.GetData()

// Do more things
}

// ExampleNode_Get demonstrates how to retrieve id and data from node.
func ExampleNode_Get() {
n := tree.NewNode(0, 3.14)
// ExampleNode_GetData demonstrates how to retrieve data from node.
func ExampleNode_GetData() {
n := tree.NewNodeWithID(0, 3.14)

id, data := n.Get()
fmt.Println(id)
data := n.GetData()
fmt.Println(data)

// Do more things
}

// ExampleNode_GetID demonstrates how to retrieve id from node.
func ExampleNode_GetID() {
n := tree.NewNodeWithID(0, 3.14)

id := n.GetID()
fmt.Println(id)

// Do more things
}

// ExampleNode_GetNexts demonstrates how to retrieve next nodes from node.
func ExampleNode_GetNexts() {
root := tree.NewNode(0, "root")
leaf := tree.NewNode(1, "leaf")
root := tree.NewNodeWithID(0, "root")
leaf := tree.NewNodeWithID(1, "leaf")

root.AddNext(leaf)
nexts := root.GetNexts()
Expand All @@ -135,19 +153,19 @@ func ExampleNode_GetNexts() {

// ExampleNode_GetPrevious demonstrates how to retrieve next nodes from node.
func ExampleNode_GetPrevious() {
root := tree.NewNode(0, "root")
leaf := tree.NewNode(1, "leaf")
root := tree.NewNodeWithID(0, "root")
leaf := tree.NewNodeWithID(1, "leaf")

root.AddNext(leaf)
previous := leaf.GetPrevious()
fmt.Println(previous.Get())
fmt.Println(previous.GetData())

// Do more things
}

// ExampleNode_IsRoot demonstrates how to retrieve info if node is root.
func ExampleNode_IsRoot() {
n := tree.NewNode(0, 'b')
n := tree.NewNodeWithID(0, 'b')

root := n.IsRoot()
fmt.Println(root)
Expand All @@ -157,8 +175,8 @@ func ExampleNode_IsRoot() {

// ExampleNode_IsLeaf demonstrates how to retrieve info if node is leaf.
func ExampleNode_IsLeaf() {
n1 := tree.NewNode(0, 'a')
n2 := tree.NewNode(0, 'b')
n1 := tree.NewNodeWithID(0, 'a')
n2 := tree.NewNodeWithID(0, 'b')

n1.AddNext(n2)

Expand All @@ -170,26 +188,26 @@ func ExampleNode_IsLeaf() {

// ExampleNode_Backtrack demonstrates how to retrieve the path between node to root.
func ExampleNode_Backtrack() {
n1 := tree.NewNode(0, 'a')
n2 := tree.NewNode(0, 'b')
n3 := tree.NewNode(0, 'c')
n1 := tree.NewNodeWithID(0, 'a')
n2 := tree.NewNodeWithID(0, 'b')
n3 := tree.NewNodeWithID(0, 'c')

n1.AddNext(n2)
n2.AddNext(n3)

nodes := n3.Backtrack()
for _, node := range nodes {
fmt.Println(node.Get())
fmt.Println(node.GetData())
}

// Do more things
}

// ExampleNode_GetStructure demonstrates how to retrieve the tree structure from node.
func ExampleNode_GetStructure() {
n1 := tree.NewNode(0, 'a')
n2 := tree.NewNode(0, 'b')
n3 := tree.NewNode(0, 'c')
n1 := tree.NewNodeWithID(0, 'a')
n2 := tree.NewNodeWithID(0, 'b')
n3 := tree.NewNodeWithID(0, 'c')

n1.AddNext(n2)
n2.AddNext(n3)
Expand All @@ -204,8 +222,8 @@ func ExampleNode_GetStructure() {

// ExampleNode_AddNext demonstrates how to add a node to a parent.
func ExampleNode_AddNext() {
n1 := tree.NewNode(0, 'a')
n2 := tree.NewNode(0, 'b')
n1 := tree.NewNodeWithID(0, 'a')
n2 := tree.NewNodeWithID(0, 'b')

n1.AddNext(n2)

Expand Down
20 changes: 16 additions & 4 deletions tree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ type Node[T any] struct {
}

// NewNode creates a new node.
func NewNode[T any](id int, data T) *Node[T] {
func NewNode[T any](data T) *Node[T] {
return &Node[T]{
data: data,
}
}

// NewNodeWithID creates a new node with ID.
func NewNodeWithID[T any](id int, data T) *Node[T] {
return &Node[T]{
id: id,
data: data,
}
}

// Get retrieves id and data from node.
func (n *Node[T]) Get() (int, T) {
return n.id, n.data
// GetData retrieves data from node.
func (n *Node[T]) GetData() T {
return n.data
}

// GetID retrieves id from node.
func (n *Node[T]) GetID() int {
return n.id
}

// GetPrevious retrieves the next nodes.
Expand Down
Loading

0 comments on commit fc6fa88

Please sign in to comment.