RoseTree

Struct RoseTree 

Source
pub struct RoseTree<N, Ix: IndexType = u32> { /* private fields */ }
Expand description

An indexable tree data structure with a variable and unbounded number of branches per node.

Also known as a multi-way tree.

See the wikipedia article on the “Rose tree” data structure.

Note: The following documentation is adapted from petgraph’s [Graph documentation] (http://bluss.github.io/petulant-avenger-graphlibrary/doc/petgraph/graph/struct.Graph.html).

RoseTree is parameterized over the node weight N and Ix which is the index type used.

A wrapper around petgraph’s Graph data structure with a refined API specifically targeted towards use as a RoseTree.

NodeIndex is a type that acts as a reference to nodes, but these are only stable across certain operations. Removing nodes may shift other indices. Adding kids to the RoseTree keeps all indices stable, but removing a node will force the last node to shift its index to take its place.

The fact that the node indices in the RoseTree are numbered in a compact interval from 0 to n-1 simplifies some graph algorithms.

The Ix parameter is u32 by default. The goal is that you can ignore this parameter completely unless you need a very large RoseTree – then you can use usize.

The RoseTree also offers methods for accessing the underlying Graph, which can be useful for taking advantage of petgraph’s various graph-related algorithms.

Implementations§

Source§

impl<N, Ix> RoseTree<N, Ix>
where Ix: IndexType,

Source

pub fn new(root: N) -> (Self, NodeIndex<Ix>)

Create a new RoseTree along with some root node. Returns both the RoseTree and an index into the root node in a tuple.

Source

pub fn with_capacity(nodes: usize, root: N) -> (Self, NodeIndex<Ix>)

Create a new RoseTree with estimated capacity and some root node. Returns both the RoseTree and an index into the root node in a tuple.

Source

pub fn node_count(&self) -> usize

The total number of nodes in the RoseTree.

Source

pub fn graph(&self) -> &PetGraph<N, Ix>

Borrow the RoseTree’s underlying PetGraph<N, Ix>. All existing NodeIndexs may be used to index into this graph the same way they may be used to index into the RoseTree.

Source

pub fn into_graph(self) -> PetGraph<N, Ix>

Take ownership of the RoseTree and return the internal PetGraph<N, Ix>. All existing NodeIndexs may be used to index into this graph the same way they may be used to index into the RoseTree.

Source

pub fn add_child(&mut self, parent: NodeIndex<Ix>, kid: N) -> NodeIndex<Ix>

Add a child node to the node at the given NodeIndex. Returns an index into the child’s position within the tree.

Computes in O(1) time.

Panics if the given parent node doesn’t exist.

Panics if the Graph is at the maximum number of edges for its index.

Source

pub fn node_weight(&self, node: NodeIndex<Ix>) -> Option<&N>

Borrow the weight from the node at the given index.

Source

pub fn node_weight_mut(&mut self, node: NodeIndex<Ix>) -> Option<&mut N>

Mutably borrow the weight from the node at the given index.

Source

pub fn index_twice_mut( &mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>, ) -> (&mut N, &mut N)

Index the RoseTree by two node indices.

Panics if the indices are equal or if they are out of bounds.

Source

pub fn remove_all_but_root(&mut self)

Remove all nodes in the RoseTree except for the root.

Source

pub fn remove_node(&mut self, node: NodeIndex<Ix>) -> Option<N>

Removes and returns the node at the given index.

The parent of node will become the new parent for all of its children.

The root node cannot be removed. If its index is given, None will be returned.

Note: this method may shift other node indices, invalidating previously returned indices!

Source

pub fn remove_node_with_children( &mut self, _node: NodeIndex<Ix>, ) -> Option<RoseTree<N, Ix>>

Removes the node at the given index along with all their children, returning them as a new RoseTree.

If there was no node at the given index, None will be returned.

Source

pub fn parent(&self, child: NodeIndex<Ix>) -> Option<NodeIndex<Ix>>

An index to the parent of the node at the given index if there is one.

Source

pub fn parent_recursion( &self, child: NodeIndex<Ix>, ) -> ParentRecursion<'_, N, Ix>

An iterator over the given child’s parent, that parent’s parent and so forth.

The returned iterator yields NodeIndex<Ix>s.

Source

pub fn children(&self, parent: NodeIndex<Ix>) -> Children<'_, Ix>

An iterator over all nodes that are children to the node at the given index.

The returned iterator yields NodeIndex<Ix>s.

Source

pub fn walk_children(&self, parent: NodeIndex<Ix>) -> WalkChildren<Ix>

A “walker” object that may be used to step through the children of the given parent node.

Unlike the Children type, WalkChildren does not borrow the RoseTree.

Source

pub fn siblings(&self, child: NodeIndex<Ix>) -> Siblings<'_, Ix>

An iterator over all nodes that are siblings to the node at the given index.

The returned iterator yields NodeIndex<Ix>s.

Source

pub fn walk_siblings(&self, child: NodeIndex<Ix>) -> WalkSiblings<Ix>

A “walker” object that may be used to step through the siblings of the given child node.

Unlike the Siblings type, WalkSiblings does not borrow the RoseTree.

Trait Implementations§

Source§

impl<N: Clone, Ix: Clone + IndexType> Clone for RoseTree<N, Ix>

Source§

fn clone(&self) -> RoseTree<N, Ix>

Returns a duplicate 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<N: Debug, Ix: Debug + IndexType> Debug for RoseTree<N, Ix>

Source§

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

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

impl<N, Ix> Index<NodeIndex<Ix>> for RoseTree<N, Ix>
where Ix: IndexType,

Source§

type Output = N

The returned type after indexing.
Source§

fn index(&self, index: NodeIndex<Ix>) -> &N

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

impl<N, Ix> IndexMut<NodeIndex<Ix>> for RoseTree<N, Ix>
where Ix: IndexType,

Source§

fn index_mut(&mut self, index: NodeIndex<Ix>) -> &mut N

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

Auto Trait Implementations§

§

impl<N, Ix> Freeze for RoseTree<N, Ix>

§

impl<N, Ix> RefUnwindSafe for RoseTree<N, Ix>

§

impl<N, Ix> Send for RoseTree<N, Ix>
where N: Send, Ix: Send,

§

impl<N, Ix> Sync for RoseTree<N, Ix>
where N: Sync, Ix: Sync,

§

impl<N, Ix> Unpin for RoseTree<N, Ix>
where N: Unpin, Ix: Unpin,

§

impl<N, Ix> UnwindSafe for RoseTree<N, Ix>
where N: UnwindSafe, Ix: 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.