Skip to main content

Mojo struct

IntervalTree

struct IntervalTree[T: IntervalElement, U: Copyable & Movable & Stringable & EqualityComparable & LessThanComparable & GreaterThanComparable & LessThanOrEqualComparable & GreaterThanOrEqualComparable]

An interval tree data structure for efficient range queries.

Parameters

Implemented traits

AnyType, Defaultable, UnknownDestructibility, Writable

Methods

__init__

__init__(out self)

Initializes an empty IntervalTree.

insert

insert(mut self, interval: Tuple[T, T], data: U)

Insert a new interval into the tree using a tuple representation.

Args:

  • interval (Tuple): A tuple containing the start and end values of the interval.
  • data (U): The data value to associate with this interval.

insert(mut self, interval: Interval[T], data: U)

Insert a new interval into the tree.

This method inserts a new interval and its associated data into the interval tree. It maintains the binary search tree property based on interval start times and updates the tree structure to preserve red-black tree properties.

Args:

  • interval (Interval): The interval to insert into the tree.
  • data (U): The data value to associate with this interval.

__str__

__str__(self) -> String

Returns a string representation of the interval tree.

Returns:

String: A string representation of the interval tree.

__repr__

__repr__(self) -> String

Returns a string representation of the interval tree suitable for debugging.

Returns:

String: A string representation of the interval tree.

write_to

write_to[w: Writer](self, mut writer: w)

Writes the interval tree to a writer.

Parameters:

  • w (Writer): The writer type that implements the Writer trait.

Args:

  • writer (w): The writer to write the interval tree to.

depth

depth(self) -> Int

Returns the depth of the interval tree.

Returns:

Int: The depth of the interval tree.

transplant

transplant(mut self, mut u: UnsafePointer[_IntervalNode[T, U]], mut v: UnsafePointer[_IntervalNode[T, U]])

Transplants the subtree rooted at node u with the subtree rooted at node v.

Args:

search(self, interval: Tuple[T, T]) -> List[U]

Searches for intervals overlapping with the given tuple.

Args:

  • interval (Tuple): The interval tuple (start, end).

Returns:

List: A list of data associated with overlapping intervals.

search(self, interval: Interval[T]) -> List[U]

Searches for intervals overlapping with the given interval.

Args:

  • interval (Interval): The interval to search.

Returns:

List: A list of data associated with overlapping intervals.

Was this page helpful?