pub struct Tree<Q, T>{ /* 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>
impl<Q, T> Tree<Q, T>
sourcepub fn add_node(&mut self, node: Node<Q, T>, parent_id: Option<Q>) -> Q
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. IfNone
, 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);
sourcepub fn get_node(&self, node_id: Q) -> Option<Node<Q, T>>
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));
sourcepub fn get_root_node(&self) -> Option<Node<Q, T>>
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));
sourcepub fn remove_node(&mut self, node_id: Q, strategy: NodeRemovalStrategy)
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);
sourcepub fn get_subtree(&self, node_id: Q, generations: Option<i32>) -> SubTree<Q, T>
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. IfNone
, 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);
sourcepub fn add_subtree(&mut self, node_id: Q, subtree: SubTree<Q, T>)
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);