Skip to content

<fix> Empty trees cannot be made #32

@JoshdRod

Description

@JoshdRod

An empty tree should be able to be made.
If so, adding to an empty tree for the first time should make that node the root.

Copilot description:
Tree’s default constructor arguments (body=[], root=0) currently fail because root=0 is out of range when the body is empty (0 > -1). Either default root to -1 for an empty tree, or adjust the root setter/constructor to allow root=0 only when body.length > 0.

	constructor (body = [], root) {
		this.body = body;

		// If no explicit root is provided, choose a default based on body length:
		// - For a non-empty body, default to 0 (first element), preserving existing behavior.
		// - For an empty body, use -1 to indicate "no root", avoiding out-of-range indices.
		if (root === undefined) {
			this.root = this.body.length > 0 ? 0 : -1;
		} else {
			this.root = root;
		}

Originally posted by @Copilot in #29 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions