heapordered

package module
v0.0.0-...-5ef0db8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2024 License: GPL-3.0 Imports: 1 Imported by: 1

Documentation

Overview

Package heapordered provides Tree: a generic min-heap-ordered tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

type Tree[E any] struct {

	// Priority value for the node.
	//
	// Fix, Down or UpdatePriority should be called when the Prioirty value changes.
	Priority float64
	// E is a generic element payload.
	E E
	// contains filtered or unexported fields
}

Tree represents a node in the tree among heap-ordered children.

Tree keeps track of its own index in the child slice so we can call Fix when the Priority changes.

func NewTree

func NewTree[E any](e E, priority float64, children ...*Tree[E]) *Tree[E]

NewTree creates a new tree node with children.

NewTree initializes a heap for the children.

func (*Tree[E]) At

func (n *Tree[E]) At(i int) *Tree[E]

At returns the element at i.

func (*Tree[E]) Down

func (n *Tree[E]) Down()

Down repairs the heap property when the priority value has increased.

Fix or ReplaceElem should be called when the node's Prioirty value changes.

func (*Tree[E]) EachChild

func (n *Tree[E]) EachChild(f func(*Tree[E]))

EachChild calls f on each child of n.

func (*Tree[E]) Fix

func (n *Tree[E]) Fix()

Fix repairs the heap property for the current node in the parent's child heap.

Fix or ReplaceElem should be called when the node's Prioirty value changes.

func (*Tree[E]) Grow

func (n *Tree[E]) Grow(cap int)

Grow ensures capacity for the given number of additional children.

func (*Tree[E]) Init

func (n *Tree[E]) Init()

Init fixes the child heap for this node.

func (*Tree[E]) Len

func (n *Tree[E]) Len() int

Len returns the number of children for this node.

func (*Tree[E]) Min

func (n *Tree[E]) Min() *Tree[E]

Min returns the minimum element or nil if none is available.

func (*Tree[E]) NewChild

func (parent *Tree[E]) NewChild(e E, priority float64) *Tree[E]

NewChild creates a new child node in the parent.

NewChild places the child on the heap.

func (*Tree[E]) NewChildTree

func (parent *Tree[E]) NewChildTree(n *Tree[E])

NewChild creates a new child node in the parent.

NewChild places the child on the heap.

func (*Tree[E]) Parent

func (n *Tree[E]) Parent() *Tree[E]

Parent returns the parent for this node or nil.

func (*Tree[E]) Pop

func (n *Tree[E]) Pop() (min *Tree[E])

Pop removes the best node from the child heap.

Pop panics if n has no children.

func (*Tree[E]) Remove

func (n *Tree[E]) Remove()

Remove the current node from the parent's child heap.

Remove panics if n is not part of a parent heap.

func (*Tree[E]) UpdatePriority

func (n *Tree[E]) UpdatePriority(v float64) (oldValue float64)

UpdatePriority replaces the priority for the current node and calls Fix to repair the heap property. Returns the old element.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL