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>
impl<Q, T> Node<Q, T>
Sourcepub fn new(node_id: Q, value: Option<T>) -> Self
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));
Sourcepub fn add_child(&self, child: Node<Q, T>) -> Result<()>
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();
Sourcepub fn remove_child(&self, child: Node<Q, T>) -> Result<()>
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();
Sourcepub fn get_node_id(&self) -> Result<Q>
pub fn get_node_id(&self) -> Result<Q>
Sourcepub fn get_children_ids(&self) -> Result<Vec<Q>>
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);
Sourcepub fn get_parent_id(&self) -> Result<Option<Q>>
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());
Sourcepub fn set_parent(&self, parent: Option<Node<Q, T>>) -> Result<()>
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()));
Sourcepub fn sort_children(&self, compare: impl Fn(&Q, &Q) -> Ordering) -> Result<()>where
Q: Debug,
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> FromIterator<Node<Q, T>> for Nodes<Q, T>
impl<Q, T> FromIterator<Node<Q, T>> for Nodes<Q, T>
Source§fn from_iter<I: IntoIterator<Item = Node<Q, T>>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = Node<Q, T>>>(iter: I) -> Self
Create a nodes list from an iterator.