Struct id_tree::TreeBuilder [] [src]

pub struct TreeBuilder<T> { /* fields omitted */ }

A Tree builder that provides more control over how a Tree is created.

Methods

impl<T> TreeBuilder<T>
[src]

Creates a new TreeBuilder with the default settings.

use id_tree::TreeBuilder;

let _tree_builder: TreeBuilder<i32> = TreeBuilder::new();

Sets the root Node of the TreeBuilder.

use id_tree::TreeBuilder;
use id_tree::Node;

let _tree_builder = TreeBuilder::new().with_root(Node::new(1));

Sets the node_capacity of the TreeBuilder.

Since Trees own their Nodes, they must allocate storage space as Nodes are inserted. Using this setting allows the Tree to pre-allocate space for Nodes ahead of time, so that the space allocations don't happen as the Nodes are inserted.

Use of this setting is recommended if you know the maximum number of Nodes that your Tree will contain at any given time.

use id_tree::TreeBuilder;

let _tree_builder: TreeBuilder<i32> = TreeBuilder::new().with_node_capacity(3);

Sets the swap_capacity of the TreeBuilder.

This is important because Trees attempt to save time by re-using storage space when Nodes are removed (instead of shuffling Nodes around internally). To do this, the Tree must store information about the space left behind when a Node is removed. Using this setting allows the Tree to pre-allocate this storage space instead of doing so as Nodes are removed from the Tree.

Use of this setting is recommended if you know the maximum "net number of removals" that have occurred at any given time.

For example:

In Scenario 1:

  • Add 3 Nodes, Remove 2 Nodes, Add 1 Node.

The most amount of nodes that have been removed at any given time is 2.

But in Scenario 2:

  • Add 3 Nodes, Remove 2 Nodes, Add 1 Node, Remove 2 Nodes.

The most amount of nodes that have been removed at any given time is 3.

use id_tree::TreeBuilder;

let _tree_builder: TreeBuilder<i32> = TreeBuilder::new().with_swap_capacity(3);

Build a Tree based upon the current settings in the TreeBuilder.

use id_tree::TreeBuilder;
use id_tree::Tree;
use id_tree::Node;

let _tree: Tree<i32> = TreeBuilder::new()
        .with_root(Node::new(5))
        .with_node_capacity(3)
        .with_swap_capacity(2)
        .build();