BTree

Struct BTree 

Source
pub struct BTree<T, const N: usize> {
    pub l_nodes: Array<isize, N>,
    pub r_nodes: Array<isize, N>,
    pub values: Array<T, N>,
}
Expand description

Statically sized binary tree representation for fast traversal, suitable for dense trees. Special implementation for binary tree is offered for faster traversal times over the generalized k tree, where an array of arrays and indices checks happen.

Fields§

§l_nodes: Array<isize, N>§r_nodes: Array<isize, N>§values: Array<T, N>

Implementations§

Source§

impl<T, const N: usize> BTree<T, N>

Source

pub fn new(l_nodes: [isize; N], r_nodes: [isize; N], values: [T; N]) -> Self

Constructs a new tree from array representation

§Examples
        use treesome::sized::BTree;
        let left = [1, 3, 5, -1, -1, -1, -1];
        let right = [2, 4, 6, -1, -1, -1, -1];
        let values = [10, 51, 36, 90, 32, 16, 5];
        let tree = BTree::new(left, right, values);
Source

pub fn is_leaf_node(&self, node_id: usize) -> bool

True if given node_id is a leaf node (no children), false otherwise.

Source

pub fn children(&self, node_id: usize) -> Children

Returns left and right child of a node. The value of -1 means no child in that direction.

§Examples
        use treesome::sized::BTree;
        let left = [1, 3, 5, -1, -1, -1, -1];
        let right = [2, 4, 6, -1, -1, -1, -1];
        let values = [10, 51, 36, 90, 32, 16, 5];
        let tree = BTree::new(left, right, values);
        assert_eq!(tree.children(0), (1, 2).into());
Source

pub fn parent(&self, node_id: isize) -> Option<isize>

Return’s node_id of its parent, if it exists. If there’s no parent (root node, non-existent node_id) for given node, None is returned. Computational complexity of the lookup is O(1), as the formula used calculates the exact position of the parent node.

§Examples
        use treesome::sized::BTree;
        let left = [1, 3, 5, -1, -1, -1, -1];
        let right = [2, 4, 6, -1, -1, -1, -1];
        let values = [10, 51, 36, 90, 32, 16, 5];
        let tree = BTree::new(left, right, values);

        assert_eq!(tree.parent(0), None);
        assert_eq!(tree.parent(3).unwrap(), 1);

Trait Implementations§

Source§

impl<T: Debug, const N: usize> Debug for BTree<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: usize> Index<usize> for BTree<T, N>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: PartialEq, const N: usize> PartialEq for BTree<T, N>

Source§

fn eq(&self, other: &BTree<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, const N: usize> Eq for BTree<T, N>

Source§

impl<T, const N: usize> StructuralPartialEq for BTree<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for BTree<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for BTree<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for BTree<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for BTree<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for BTree<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for BTree<T, N>
where 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.