Struct Tree

Source
pub struct Tree<T: Clone> { /* private fields */ }
Expand description

Stores all data and relationships between nodes

Implementations§

Source§

impl<T: Clone> Tree<T>

Source

pub fn new() -> Self

Returns a new empty tree

Source

pub fn is_valid(&self, node: NodeID) -> bool

Check if a node id points to a valid, non-deleted node in the tree. Returns true if the node id is valid.

§Arguments
  • node - the NodeID to check
Source

pub fn delete(&mut self, node: NodeID) -> usize

Mark a node in the tree, and all of its children as invalid. This allows the reuse of these indexes by other nodes created later. Returns the number of total nodes that were deleted.

Source

pub fn add_root(&mut self, val: T) -> NodeID

Add a root node and return its id

Source

pub fn get(&self, node: NodeID) -> Option<&T>

Returns the value for a node (if it exists)

Source

pub fn get_mut(&mut self, node: NodeID) -> Option<&mut T>

Returns the mutable value for a node (if it exists)

Source

pub fn get_unchecked(&self, node: NodeID) -> &T

Returns the value for a node, and panic if it doesn’t exist

Source

pub fn get_unchecked_mut(&mut self, node: NodeID) -> &mut T

Returns the mutable value for a node, and panic if it doesn’t exist

Source

pub fn set(&mut self, node: NodeID, val: T)

Set the value for a node (if it already exists)

Source

pub fn add_child(&mut self, node: NodeID, val: T) -> Option<NodeID>

Add a child to the node and return its id (if successful)

Source

pub fn add_child_unchecked(&mut self, node: NodeID, val: T) -> NodeID

Add a child to the node and return its id. Panics if node does not exist

Source

pub fn children_ids_unchecked(&self, node: NodeID) -> &Vec<NodeID>

Returns the children_ids for a node Panics if the node doesn’t exist

Source

pub fn root_ids(&self) -> &Vec<NodeID>

Returns the root nodes of the tree

Source

pub fn parent_id(&self, node: NodeID) -> Option<NodeID>

Returns the parent id for the node

Source

pub fn iter_depth(&self) -> DepthIter<'_, T>

Returns a depth-first iterator This iterates a single node until a leaf is reached and then moves onto the next one

Source

pub fn iter_breadth(&self) -> BreadthIter<'_, T>

Returns a breadth-first iterator This iterates an entire depth-level before moving onto the next deepest nodes

Source

pub fn path(&self, node: NodeID) -> Vec<NodeID>

Returns the node sequence from a root node to the given node

Source

pub fn path_values_ref(&self, node: NodeID) -> Vec<&T>

Returns references to values in the node sequence from a root node to the given node

Source

pub fn path_values(&self, node: NodeID) -> Vec<T>

Returns cloned values in the node sequence from a root node to the given node

Trait Implementations§

Source§

impl<T: Clone + Clone> Clone for Tree<T>

Source§

fn clone(&self) -> Tree<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: PartialEq + Clone> PartialEq for Tree<T>

Source§

fn eq(&self, other: &Tree<T>) -> 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 + Clone> Eq for Tree<T>

Source§

impl<T: Clone> StructuralPartialEq for Tree<T>

Auto Trait Implementations§

§

impl<T> Freeze for Tree<T>

§

impl<T> RefUnwindSafe for Tree<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tree<T>
where T: Send,

§

impl<T> Sync for Tree<T>
where T: Sync,

§

impl<T> Unpin for Tree<T>
where T: Unpin,

§

impl<T> UnwindSafe for Tree<T>
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.