Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add First() to node #2

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
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