Struct Node

Source
pub struct Node<Q, T>(/* private fields */)
where
    Q: PartialEq + Eq + Clone,
    T: PartialEq + Eq + Clone;
Expand description

A node in a tree.

This struct represents a node in a tree. The node has a unique id, a value, children and a parent. The unique id is used to identify the node. The value is the value of the node. The children are the children of the node and the parent is the parent of the node.

§Type Parameters

  • Q - The type of the unique id of the node. Odd, I know but this is for flexibility. Some people might want to use a string as the unique id of the node. Others might want to use an integer. This is why the unique id is a generic type.
  • T - The type of the value of the node.

§Fields

  • node_id - The unique id of the node.
  • value - The value of the node.
  • children - The children of the node.
  • parent - The parent of the node.

§Example


let node: Node<i32, i32> = Node::new(1, Some(2));

Implementations§

Source§

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

Source

pub fn new(node_id: Q, value: Option<T>) -> Self

Create a new node.

This method creates a new node with the given node id and value. 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.
§Returns

A new node with the given node id and value.

§Example

let node = Node::new(1, Some(2));
Source

pub fn add_child(&self, child: Node<Q, T>) -> Result<()>

Add a child to the node.

This method adds a child to the node. The child is added to the children of the node and the parent of the child is set to the node.

§Arguments
  • child - The child to add to the node.
§Example

let parent_node = Node::new(1, Some(2));
parent_node.add_child(Node::new(2, Some(3))).unwrap();
Source

pub fn remove_child(&self, child: Node<Q, T>) -> Result<()>

Remove a child from the node.

This method removes a child from the node. The child is removed from the children of the node and the parent of the child is set to None. The reason as to why we pass the child as an argument instead of the node id is because we want to ensure that the parent of the child is set to None when the child is removed from this parent. If we were to pass the node id of the child as an argument, we would have to get the child from the tree and then set the parent to None. And at this level we have no knowledge of the tree.

§Arguments
  • child - The child to remove from the node.
§Example

let parent_node = Node::new(1, Some(2));
let child_node = Node::new(2, Some(3));
parent_node.add_child(child_node.clone()).unwrap();
parent_node.remove_child(child_node).unwrap();
Source

pub fn get_node_id(&self) -> Result<Q>

Get the unique Id of the node.

This method returns the unique Id of the node. The unique Id is used to identify the node.

§Returns

The unique Id of the node.

§Example

let node = Node::new(1, Some(2));
assert_eq!(node.get_node_id().unwrap(), 1);
Source

pub fn get_children_ids(&self) -> Result<Vec<Q>>

Get the ids of the children of the node.

This method returns the ids of the children of the node.

§Returns

The ids of the children of the node.

§Example

let node = Node::new(1, Some(2));
let child = Node::new(2, Some(3));
node.add_child(child).unwrap();

// In this case Getting the children ids should not
// panic as we are using the sync version of the node.

// |>Note that the async version of the node might panic
// due to poisoning so handle accordingly.
assert_eq!(node.get_children_ids().unwrap().len(), 1);
Source

pub fn get_parent_id(&self) -> Result<Option<Q>>

Get the node id of the parent of the node.

This method returns the node_id of the parent of the node. In the case where the node is a root node in a tree, the parent of the node will be None.

§Returns

The optional node_id of the parent of the node.

§Example

let parent_node = Node::new(1, Some(2));
let child_node = Node::new(2, Some(3));
parent_node.add_child(child_node.clone()).unwrap();
assert_eq!(child_node.get_parent_id().unwrap().as_ref(), Some(&parent_node.get_node_id().unwrap()));
assert!(parent_node.get_parent_id().unwrap().is_none());
Source

pub fn get_value(&self) -> Result<Option<T>>

Get the value of the node.

This method returns the value of the node. If the node has no value, None is returned.

§Returns

The value of the node.

§Example

let node = Node::new(1, Some(2));
assert_eq!(node.get_value().unwrap(), Some(2));
Source

pub fn set_value(&self, value: Option<T>) -> Result<()>

Set the value of the node.

This method sets the value of the node.

§Arguments
  • value - The value to set.
§Example

let node = Node::new(1, Some(2));
assert_eq!(node.get_value().unwrap(), Some(2));
node.set_value(Some(3)).unwrap();
assert_eq!(node.get_value().unwrap(), Some(3));
Source

pub fn update_value(&self, modifier: impl FnOnce(&mut Option<T>)) -> Result<()>

Update the value of the node.

This method updates the value of the node using a closure.

§Arguments
  • modifier - The closure to use for updating the value.
§Example

let node = Node::new(1, Some(2));
node.update_value(|value| *value = Some(3)).unwrap();
assert_eq!(node.get_value().unwrap(), Some(3));
Source

pub fn set_parent(&self, parent: Option<Node<Q, T>>) -> Result<()>

Set the parent of the node.

This method sets the parent of the node.

§Arguments
  • parent - The parent to set.
§Example

let parent_node = Node::new(1, Some(2));
let child_node = Node::new(2, Some(3));
child_node.set_parent(Some(parent_node.clone())).unwrap();
assert_eq!(child_node.get_parent_id().unwrap().as_ref(), Some(&parent_node.get_node_id().unwrap()));
Source

pub fn sort_children(&self, compare: impl Fn(&Q, &Q) -> Ordering) -> Result<()>
where Q: Debug,

Sort the children of the node by an ordering closure.

§Arguments
  • compare - The closure to use for comparison.
§Example

let parent_node = Node::new(1, Some(2));
let child1 = Node::new(2, Some(3));
let child2 = Node::new(3, Some(4));
parent_node.add_child(child1).unwrap();
parent_node.add_child(child2).unwrap();

parent_node.sort_children(|a, b| a.cmp(&b).reverse()).unwrap();
assert_eq!(parent_node.get_children_ids().unwrap(), vec![3, 2]);

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Node<Q, T>

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<Q, T> Debug for Node<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> Display for Node<Q, T>
where Q: PartialEq + Eq + Clone + Display, T: PartialEq + Eq + Clone + Display + Default,

Source§

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

Display the node.

Source§

impl<Q, T> FromIterator<Node<Q, T>> for Nodes<Q, T>
where Q: PartialEq + Eq + Clone, T: PartialEq + Eq + Clone,

Source§

fn from_iter<I: IntoIterator<Item = Node<Q, T>>>(iter: I) -> Self

Create a nodes list from an iterator.

Source§

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

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Hash the node.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Compare two nodes for equality.

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<Q, T> Eq for Node<Q, T>
where Q: PartialEq + Eq + Clone + Eq, T: PartialEq + Eq + Clone + Eq,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<Q, T> !UnwindSafe for Node<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> 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> ToString for T
where T: Display + ?Sized,

Source§

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

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.