Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/johnfercher/go-tree/node"

"github.com/johnfercher/go-tree/tree"
)
Expand All @@ -10,16 +11,16 @@ import (
func main() {
tr := tree.New[string]()

tr.AddRoot(tree.NewNodeWithID(0, "0.0"))
tr.AddRoot(node.New("0.0").WithID(0))

tr.Add(0, tree.NewNodeWithID(1, "0.1"))
tr.Add(0, tree.NewNodeWithID(2, "0.2"))
tr.Add(0, node.New("0.1").WithID(1))
tr.Add(0, node.New("0.2").WithID(2))

tr.Add(1, tree.NewNodeWithID(3, "1.3"))
tr.Add(1, tree.NewNodeWithID(4, "1.4"))
tr.Add(1, node.New("1.3").WithID(3))
tr.Add(1, node.New("1.4").WithID(4))

tr.Add(2, tree.NewNodeWithID(5, "2.5"))
tr.Add(2, tree.NewNodeWithID(6, "2.6"))
tr.Add(2, node.New("2.5").WithID(5))
tr.Add(2, node.New("2.6").WithID(6))

root, ok := tr.GetRoot()
fmt.Println(ok) // true
Expand Down
16 changes: 7 additions & 9 deletions tree/node.go → node/node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tree
package node

import (
"fmt"
Expand All @@ -13,19 +13,17 @@ type Node[T any] struct {
nexts []*Node[T]
}

// NewNode creates a new node.
func NewNode[T any](data T) *Node[T] {
// New creates a new node.
func New[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,
}
// WithID retrieves data from node.
func (n *Node[T]) WithID(id int) *Node[T] {
n.id = id
return n
}

// GetData retrieves data from node.
Expand Down
Loading