Skip to content

Commit

Permalink
Merge pull request #2 from lestrrat-go/implement-first
Browse files Browse the repository at this point in the history
Add First() to node
  • Loading branch information
lestrrat authored Oct 13, 2024
2 parents 814d613 + 649e90b commit ac25977
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,25 @@ type Trie[L any, K cmp.Ordered, V any] struct {

// Node represents an individual node in the trie.
type Node[K cmp.Ordered, V any] interface {
// Key returns the key associated with this node
Key() K

// Value returns the value associated with this node
Value() V

// Children returns the children of this node
Children() iter.Seq[Node[K, V]]

// First returns the first child of this node
First() Node[K, V]

// AddChild adds a child to this node
AddChild(Node[K, V])

// Parent returns the parent of this node
Parent() Node[K, V]

// Ancestors returns a sequence of ancestors of this node
Ancestors() iter.Seq[Node[K, V]]
}

Expand Down Expand Up @@ -266,6 +280,13 @@ func (n *node[K, V]) Children() iter.Seq[Node[K, V]] {
}
}

func (n *node[K, V]) First() Node[K, V] {
if len(n.children) == 0 {
return nil
}
return n.children[0]
}

func (n *node[K, V]) AddChild(child Node[K, V]) {
n.mu.Lock()
// This is kind of gross, but we're only covering *node[T] with
Expand Down

0 comments on commit ac25977

Please sign in to comment.