SegTree

Struct SegTree 

Source
pub struct SegTree<Spec: SegTreeSpec> { /* private fields */ }
Expand description

A generic Segment Tree data structure.

A segment tree is a complete binary tree stored in a flat array that enables efficient range queries and point updates on sequences of elements. The tree supports any associative operation defined by the SegTreeSpec trait.

§Internal Structure

  • Uses 1-based indexing where the root is at index 1
  • Leaf nodes start at index max_size (next power of 2 ≥ size)
  • For any node at index i, its children are at 2*i and 2*i+1
  • Total space used is 2 * max_size

§Type Parameters

  • Spec - A type implementing SegTreeSpec that defines the operation and element type

§Examples

use array_range_query::{SegTree, SegTreeSpec};

struct MaxSpec;
impl SegTreeSpec for MaxSpec {
    type T = i32;
    const ID: Self::T = i32::MIN;
    fn op(a: &mut Self::T, b: &Self::T) { *a = (*a).max(*b); }
}

let values = vec![3, 1, 4, 1, 5, 9, 2];
let tree = SegTree::<MaxSpec>::from_vec(values);
assert_eq!(tree.query(2..5), 5); // max(4, 1, 5) = 5

Implementations§

Source§

impl<Spec: SegTreeSpec> SegTree<Spec>

Source

pub fn new(size: usize) -> Self

Creates a new segment tree with all elements initialized to Spec::ID.

§Time Complexity

O(n)

Source

pub fn from_slice(values: &[Spec::T]) -> Self

Creates a new segment tree from a slice of values.

§Time Complexity

O(n)

Source

pub fn from_vec(vec: Vec<Spec::T>) -> Self

Creates a new segment tree from a vector of values.

§Time Complexity

O(n)

Source

pub fn query<R: RangeBounds<usize>>(&self, range: R) -> Spec::T

Queries the aggregated value over the given range.

§Example
use array_range_query::helpers::SegTreeMax;

let mut tree = SegTreeMax::<i32>::from_vec(vec![1, 2, 3, 4, 5]);
assert_eq!(tree.query(..), 5);
§Time Complexity

O(log n)

§Panics

Panics if the range is invalid or out of bounds.

Source

pub fn update(&mut self, index: usize, value: Spec::T)

Updates the value at the given index.

§Example
use array_range_query::helpers::SegTreeMax;

let mut tree = SegTreeMax::<i32>::from_vec(vec![1, 2, 3, 4, 5]);
assert_eq!(tree.query(..), 5);
tree.update(2, 6);
assert_eq!(tree.query(..), 6);
§Time Complexity

O(log n)

§Panics

Panics if index is out of bounds.

Auto Trait Implementations§

§

impl<Spec> Freeze for SegTree<Spec>

§

impl<Spec> RefUnwindSafe for SegTree<Spec>
where Spec: RefUnwindSafe, <Spec as SegTreeSpec>::T: RefUnwindSafe,

§

impl<Spec> Send for SegTree<Spec>
where Spec: Send, <Spec as SegTreeSpec>::T: Send,

§

impl<Spec> Sync for SegTree<Spec>
where Spec: Sync, <Spec as SegTreeSpec>::T: Sync,

§

impl<Spec> Unpin for SegTree<Spec>
where Spec: Unpin,

§

impl<Spec> UnwindSafe for SegTree<Spec>
where Spec: UnwindSafe, <Spec as SegTreeSpec>::T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.