Struct tree_ds::prelude::Tree

source ·
pub struct Tree<Q, T>
where Q: PartialEq + Eq + Clone, T: PartialEq + Eq + Clone,
{ /* private fields */ }
Expand description

A tree data structure.

This struct represents a tree data structure. A tree is a data structure that consists of nodes connected by edges. Each node has a parent node and zero or more child nodes. The tree has a root node that is the topmost node in the tree. The tree can be used to represent hierarchical data structures such as file systems, organization charts, and family trees.

§Example


let tree: Tree<i32, i32> = Tree::new();

Implementations§

source§

impl<Q, T> Tree<Q, T>
where Q: PartialEq + Eq + Clone + Display, T: PartialEq + Eq + Clone,

source

pub fn new() -> Self

Create a new tree.

This method creates a new tree with no nodes.

§Returns

A new tree with no nodes.

§Example

let tree: Tree<i32, i32> = Tree::new();
source

pub fn add_node(&mut self, node: Node<Q, T>, parent_id: Option<Q>) -> Q

Add a node to the tree.

This method adds a node to the tree. The node is added as a child of the parent node with the given parent id. If the parent id is None, the node is added as a root node. The node id is used to identify the node and the value is the value of the node. The value can be used to store any data that you want to associate with the node.

§Arguments
  • node_id - The id of the node.
  • value - The value of the node.
  • parent_id - The id of the parent node. If None, the node is added as a root node.
§Returns

The id of the node that was added to the tree.

§Example

let mut tree: Tree<i32, i32> = Tree::new();
let node_id = tree.add_node(Node::new(1, Some(2)), None);
source

pub fn get_node(&self, node_id: Q) -> Option<Node<Q, T>>

Get a node in the tree.

This method gets the node with the given node id in the tree.

§Arguments
  • node_id - The id of the node.
§Returns

The node with the given node id in the tree or None if the node is not found.

§Example

let mut tree: Tree<i32, i32> = Tree::new();

let node = Node::new(1, Some(2));
tree.add_node(node.clone(), None);

assert_eq!(tree.get_node(1), Some(node));
source

pub fn get_root_node(&self) -> Option<Node<Q, T>>

Get the root node of the tree.

This method gets the root node of the tree. The root node is the topmost node in the tree. The root node has no parent node.

§Returns

The root node of the tree or None if the tree has no root node.

§Example

let mut tree: Tree<i32, i32> = Tree::new();

let node = Node::new(1, Some(2));
tree.add_node(node.clone(), None);

assert_eq!(tree.get_root_node(), Some(node));
source

pub fn get_nodes(&self) -> Vec<Node<Q, T>>

Get the nodes in the tree.

This method gets the nodes in the tree.

§Returns

The nodes in the tree.

§Example

let mut tree: Tree<i32, i32> = Tree::new();

let node = Node::new(1, Some(2));
tree.add_node(node.clone(), None);

assert_eq!(tree.get_nodes().len(), 1);
source

pub fn remove_node(&mut self, node_id: Q, strategy: NodeRemovalStrategy)

Remove a node from the tree.

This method removes a node from the tree. The node is removed using the given removal strategy. The removal strategy determines how the node and its children are removed from the tree. The RetainChildren strategy retains the children of the node when the node is removed. The RemoveNodeAndChildren strategy removes the node and its children when the node is removed.

§Arguments
  • node_id - The id of the node to remove.
  • strategy - The strategy to use when removing the node.
§Example

let mut tree: Tree<i32, i32> = Tree::new();

let node = Node::new(1, Some(2));
tree.add_node(node.clone(), None);
let node_2 = Node::new(2, Some(3));
tree.add_node(node_2.clone(), Some(1));
let node_3 = Node::new(3, Some(6));
tree.add_node(node_3.clone(), Some(2));

tree.remove_node(2, NodeRemovalStrategy::RetainChildren);
assert_eq!(tree.get_nodes().len(), 2);
source

pub fn get_subtree(&self, node_id: Q, generations: Option<i32>) -> SubTree<Q, T>

Get a subsection of the tree.

This method gets a subsection of the tree starting from the node with the given node id. The subsection is a list of nodes that are descendants of the node with the given node id upto the given number of descendants. If the number of descendants is None, all the descendants of the node are included in the subsection.

§Arguments
  • node_id - The id of the node to get the subsection from.
  • generations - The number of descendants to include in the subsection. If None, all the descendants of the node are included in the subsection.
§Returns

The subsection of the tree starting from the node with the given node id.

§Example


let node = Node::new(1, Some(2));
tree.add_node(node.clone(), None);
let node_2 = Node::new(2, Some(3));
tree.add_node(node_2.clone(), Some(1));
let node_3 = Node::new(3, Some(6));
tree.add_node(node_3.clone(), Some(2));

let subsection = tree.get_subtree(2, None);
assert_eq!(subsection.get_nodes().len(), 2);
source

pub fn add_subtree(&mut self, node_id: Q, subtree: SubTree<Q, T>)

Add a subsection to the tree.

This method adds a subsection to the tree. The subsection is a list of nodes that are descendants of the node with the given node id. The subsection is added as children of the node with the given node id.

§Arguments
  • node_id - The id of the node to add the subsection to.
  • subtree - The subsection to add to the tree.
§Example

let mut tree: Tree<i32, i32> = Tree::new();
let node_id = tree.add_node(Node::new(1, Some(2)), None);
let mut subtree = SubTree::new();
subtree.add_node(Node::new(2, Some(3)), None);
subtree.add_node(Node::new(3, Some(6)), Some(2));
tree.add_subtree(node_id, subtree);
assert_eq!(tree.get_nodes().len(), 3);

Trait Implementations§

source§

impl<Q, T> Clone for Tree<Q, T>
where Q: PartialEq + Eq + Clone + Clone, T: PartialEq + Eq + Clone + Clone,

source§

fn clone(&self) -> Tree<Q, 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<Q, T> Debug for Tree<Q, T>
where Q: PartialEq + Eq + Clone + Debug, T: PartialEq + Eq + Clone + Debug,

source§

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

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

impl<Q, T> Default for Tree<Q, T>
where Q: PartialEq + Eq + Clone, T: PartialEq + Eq + Clone,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Q, T> Display for Tree<Q, T>
where Q: PartialEq + Eq + Clone + Display, T: PartialEq + Eq + Clone + Display,

source§

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

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

impl<Q, T> PartialEq for Tree<Q, T>

source§

fn eq(&self, other: &Tree<Q, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Q, T> Eq for Tree<Q, T>
where Q: PartialEq + Eq + Clone + Eq, T: PartialEq + Eq + Clone + Eq,

source§

impl<Q, T> StructuralPartialEq for Tree<Q, T>
where Q: PartialEq + Eq + Clone, T: PartialEq + Eq + Clone,

Auto Trait Implementations§

§

impl<Q, T> Freeze for Tree<Q, T>

§

impl<Q, T> !RefUnwindSafe for Tree<Q, T>

§

impl<Q, T> !Send for Tree<Q, T>

§

impl<Q, T> !Sync for Tree<Q, T>

§

impl<Q, T> Unpin for Tree<Q, T>

§

impl<Q, T> !UnwindSafe for Tree<Q, T>

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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.