Struct outils::tree::generic::Forest[][src]

pub struct Forest<V> where
    V: ValueType
{ /* fields omitted */ }

Forest<V> is a general purpose data structure for holding a forest of trees. Its tree nodes are held in a memory arena and are addressed through their associated NodeIndex.

Forest is parameterized over:

  • Associated values of type V, where V must implement the trait ValueType

The tree nodes of the forest are organized in a first child/next-sibling representation, augmented by last child/previous sibling links. In other words, the children of tree node are maintained in doubly linked list, where the head and tail of the list are the first and last child values of the children's parent node.

Therefore, no allocations and re-allocations are necessary when adding children to nodes. Re-allocation will only take place if the underlying memory arena has reached its capacity.

However, care must be taken when accessing a child node by its position, that is, by using the method child(node, pos) of the Traversal trait, because to access a child by its position in the list, the list has to be iterated from the beginning. Using access by position is therefore not very efficient. This caveat also applies to the generic traversal iterators provided by the traversal module, which are build on access by position.

In order to iterate over the children of node, the trait Children is available.

To illustrate the above explations see the following example:

use outils::prelude::*;
use outils::tree::traversal::GeneralDfsValues;
let mut forest = Forest::new(10);

// Create a root with 9 child nodes.
let root = forest.insert(9);
for i in (0..9) {
    forest.insert_child(root, i);
}

// Inefficient iteration:
for pos in (0..9) {
    let index = forest.child(root, pos).expect("Should not fail here!"); // Inefficient!
    let value = forest.value(index).expect("Should not fail here!");
    assert_eq!(*value, pos);
}

// Also inefficient is using the provided, generic traversals:
let seq: Vec<&usize> = GeneralDfsValues::new(&forest, root).collect(); // Will use child()!
assert_eq!(seq, vec![&9, &0, &1, &2, &3, &4, &5, &6, &7, &8]);

// Efficient iteration:
let mut pos = 0;
for child in forest.children(root) {
    let value = forest.value(child).expect("Should not fail here!");
    assert_eq!(*value, pos);
    pos += 1;
}

Methods

impl<V> Forest<V> where
    V: ValueType
[src]

Construct a new empty Forest with an initial capacity of size.

Returns the index of the first child node of the tree node indexed by node. If the node has no children or node is not a valid index, None is returned.

use outils::prelude::*;

let mut tree = Forest::new(10);
let root = tree.insert(1);
let first_child = tree.insert_child(root, 2).expect("Should not fail here");
let second_child = tree.insert_child(root, 3).expect("Should not fail here");

assert_eq!(tree.first_child(root), Some(first_child));

Returns the index of the last child node of the tree node indexed by node. If the node has no children or node is not a valid index, None is returned.

use outils::prelude::*;

let mut tree = Forest::new(10);
let root = tree.insert(1);
let first_child = tree.insert_child(root, 2).expect("Should not fail here");
let second_child = tree.insert_child(root, 3).expect("Should not fail here");

assert_eq!(tree.last_child(root), Some(second_child));

Returns the index of the previous sibling node of the tree node indexed by node. If the node is the first child of its parent or node is not a valid index, None is returned.

use outils::prelude::*;

let mut tree = Forest::new(10);
let root = tree.insert(1);
let first_child = tree.insert_child(root, 2).expect("Should not fail here");
let second_child = tree.insert_child(root, 3).expect("Should not fail here");

assert_eq!(tree.prev_sibling(second_child), Some(first_child));

Returns the index of the next sibling node of the tree node indexed by node. If the node is the last child of its parent or node is not a valid index, None is returned.

use outils::prelude::*;

let mut tree = Forest::new(10);
let root = tree.insert(1);
let first_child = tree.insert_child(root, 2).expect("Should not fail here");
let second_child = tree.insert_child(root, 3).expect("Should not fail here");

assert_eq!(tree.next_sibling(first_child), Some(second_child));

Returns the list of root node indices of this forest. The values are not returned in any particular order.

use outils::prelude::*;

let mut tree = Forest::new(10);
let first_root = tree.insert(1);
let second_root = tree.insert(2);

let roots = tree.roots();
assert!(roots.contains(&first_root));
assert!(roots.contains(&second_root));

Trait Implementations

impl<V: Clone> Clone for Forest<V> where
    V: ValueType
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<V: Debug> Debug for Forest<V> where
    V: ValueType
[src]

Formats the value using the given formatter. Read more

impl<'slf, V> Children<'slf> for Forest<V> where
    V: 'slf + ValueType
[src]

Returns a boxed iterator over the children of the tree node node.

impl<V> Index<NodeIndex> for Forest<V> where
    V: ValueType
[src]

The returned type after indexing.

Performs the indexing (container[index]) operation.

impl<V> IndexMut<NodeIndex> for Forest<V> where
    V: ValueType
[src]

Performs the mutable indexing (container[index]) operation.

impl<V> Traversable<V> for Forest<V> where
    V: ValueType
[src]

Returns the index of the root node of the tree containing the tree node indexed by node.

Immutably access the value stored in the tree node indexed by node.

Mutably access the value stored in the tree node indexed by node.

Returns the index of parent node tree node indexed by node.

Returns the index of the child node at position pos of the tree node indexed by node.

Returns the number of child nodes of the tree node indexed by node.

Returns the total number of tree nodes of the tree self.

impl<'slf, V> Values<'slf, V> for Forest<V> where
    V: 'slf + ValueType
[src]

Returns a boxed iterator over the stored values and their corresponding tree node indices held by self.

impl<V> GenericForest<V> for Forest<V> where
    V: ValueType
[src]

Inserts value into the forest as a new root node and return the assigned NodeIndex.

Inserts value into the forest as a new node. which will be the last child of the node indexed by parent. If the operation has been completed successfully, the index of the new child is returned. Otherwise, in particular if parent is not a valid node index, an error is returned.

Inserts value into the forest as a new node. which will be a child of the node indexed by parent at the position specified by pos. If pos is greater than or equal to the number of children of parent, the new child will be the new last child. If the operation has been completed successfully, the index of the new child is returned. Otherwise, if parent is not a valid node index, an error is returned.

Removes the tree node indexed by node, returning its content in case of a valid index. If the removed node has children, they will become children of the parent of the removed node, replacing the removed node. If the removed node has no parent, its children will become roots.

Removes the tree node indexed by node and its subtree, returning the contents of the removed nodes in case of a valid index. The returned values will be collected in pre-order.

Adds the root node child as the new last child of the node indexed by parent. If the operation has been completed successfully, Ok(()) is returned. If the forest has not been changed, an error is returned. This will be the case if:

  • the node indexed by child is not a tree root, i.e. has a parent.
  • the node indexed by parent is a node in the tree rooted in child.
  • either parent or child is not a valid node index.

Adds the root node child as a child of the node indexed by parent at the position specified by pos. If pos is greater than or equal to the number of children of parent, the new child will be the new last child. If the operation has been completed successfully, Ok(()) is returned. If the forest has not been changed, an error is returned. This will be the case if:

  • the node indexed by child is not a tree root, i.e. has a parent.
  • the node indexed by parent is a node in the tree rooted in child.
  • either parent or child is not a valid node index.

Removes the node indexed by node as a child of its parent, thus making it a new root node of the forest. If the operation has been completed successfully, Ok(()) is returned. If node is not a valid not index, an error is returned.

impl<V> Tgf for Forest<V> where
    V: ValueType
[src]

Returns a TGF representation of Self.

Auto Trait Implementations

impl<V> Send for Forest<V> where
    V: Send

impl<V> Sync for Forest<V> where
    V: Sync