pub struct GenTree<T> { /* private fields */ }Implementations§
Source§impl<T> GenTree<T>
impl<T> GenTree<T>
Sourcepub fn new_with_capacity(capacity: usize) -> GenTree<T>
pub fn new_with_capacity(capacity: usize) -> GenTree<T>
Creates a new GenTree that pre-allocates to the given capacity.
pub fn mut_root(&mut self, data: T)
Sourcepub fn is_root(&self, position: &Position) -> bool
pub fn is_root(&self, position: &Position) -> bool
Indicates whether the given Position is the tree’s root.
Sourcepub fn is_some(&self, position: &Position) -> bool
pub fn is_some(&self, position: &Position) -> bool
Indicates whether the given Position contains data.
Sourcepub fn is_none(&self, position: &Position) -> bool
pub fn is_none(&self, position: &Position) -> bool
Indicates whether the given Position contains data.
Sourcepub fn num_children(&self, position: &Position) -> usize
pub fn num_children(&self, position: &Position) -> usize
Returns the number of children for a Node at the given Position.
Sourcepub fn get_data(&self, position: &Position) -> Option<&T>
pub fn get_data(&self, position: &Position) -> Option<&T>
Returns an immutable reference to the data at the given Position, if Some.
Sourcepub fn get_for_pos(&self, position: &Position) -> Option<&T>
pub fn get_for_pos(&self, position: &Position) -> Option<&T>
Returns an immutable reference to the data at the given Position, if Some.
Sourcepub fn add_child(&mut self, position: &Position, data: T) -> Position
pub fn add_child(&mut self, position: &Position, data: T) -> Position
Adds a child to the given Position and returns its Position.
Sourcepub fn children(&self, position: &Position) -> &Vec<Position>
pub fn children(&self, position: &Position) -> &Vec<Position>
Returns a reference to the list of child Positions for the given Position.
Sourcepub fn remove(&mut self, position: Position) -> Option<T>
pub fn remove(&mut self, position: Position) -> Option<T>
Removes a node at the given Position and returns its data.
If the removed Position has a parent, all the deleted node’s children
get pushed to the deleted Position’s parent. If the deleted Position
has no parent (root) a new parent is designated as the first child of
the deleted node, e.g. this is a tree, not a forest. Ordering for the
designation of the new root is not guaranteed, and depends on when child
Positions were added. For this reason, caution is advised when
potentially deleting root nodes.
Runs in O(s + c) time where:
sis the number of the deleted node’s siblings (i.e. the number of its parent’s children),cis the number of the deleted node’s children.
Precondition:
3
/ \
5 9 (marked for deleteion)
/ \ / \
8 12 11 14
Postcondition:
3
/ | \
5 11 14
/ \
8 12
Precondition:
3 (marked for deletion)
/ | \
5 11 14
/ \
8 12
Postcondition:
5
/ / \ \
8 12 11 14
Precondition:
3 (marked for deletion)
/ \
9 5
/ \ / \
8 12 11 14
Postcondition:
9
/ | \
8 12 5
/ \
11 14