emirpasic/gods · error

Invalid order, should be at least 3

Error message

Invalid order, should be at least 3

What it means

A constructor guard panic raised by btree.NewWith (reachable via btree.New) when the order argument is less than 3. In a B-tree the order m is the maximum number of children per node; a valid tree requires at least 2 children in the root and ⌈m/2⌉ (at least 1) in other non-leaf nodes, so an order of 2 or lower cannot satisfy B-tree invariants and node-splitting logic would break, hence the constructor rejects it up front.

Source

Thrown at trees/btree/btree.go:61

	Entries  []*Entry[K, V] // Contained keys in node
	Children []*Node[K, V]  // Children nodes
}

// Entry represents the key-value pair contained within nodes
type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

// New instantiates a B-tree with the order (maximum number of children) and the built-in comparator for K
func New[K cmp.Ordered, V any](order int) *Tree[K, V] {
	return NewWith[K, V](order, cmp.Compare[K])
}

// NewWith instantiates a B-tree with the order (maximum number of children) and a custom key comparator.
func NewWith[K comparable, V any](order int, comparator utils.Comparator[K]) *Tree[K, V] {
	if order < 3 {
		panic("Invalid order, should be at least 3")
	}
	return &Tree[K, V]{m: order, Comparator: comparator}
}

// Put inserts key-value pair node into the tree.
// If key already exists, then its value is updated with the new value.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree[K, V]) Put(key K, value V) {
	entry := &Entry[K, V]{Key: key, Value: value}

	if tree.Root == nil {
		tree.Root = &Node[K, V]{Entries: []*Entry[K, V]{entry}, Children: []*Node[K, V]{}}
		tree.size++
		return
	}

	if tree.insert(tree.Root, entry) {
		tree.size++

View on GitHub (pinned to 1d83d5ae39)

Solutions

  1. Pass an order of at least 3 when calling btree.New / btree.NewWith (in practice values like 3..1024 are typical; Knuth's classic choice is 2*ceil(ln(m)) etc., e.g. order 32 or 64 for disk-backed use).
  2. Validate the order before constructing: if order < 3 { return fmt.Errorf(...) } and surface it to the caller instead of panicking.
  3. Check where the order value comes from (config file, CLI flag, constant) and correct the source of the too-small value.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at trees/btree/btree.go:61 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of emirpasic/gods@1d83d5ae39 (2026-09-03). Data as JSON: /api/errors/0fa82c0375637f52. Report an issue: GitHub.