Skip to content

Releases: orxfun/orx-tree

Parallel Walks

07 Jun 19:05
ddb2d0b
Compare
Choose a tag to compare

Parallelized Walks

  • The main contribution of this release is enabling parallel iterators over various tree iterators and walks with any of the traversals. Parallelization is obtained simply by adding _par suffix to name of the sequential counterpart. These methods return a orx_parallel::ParIter which allows for significant improvements in computation time. The following is the list of new methods returning parallel iterators:
    • children_par
    • ancestors_par
    • custom_walk_par
    • walk_par
    • walk_with_par
    • paths_par
    • paths_with_par
    • leaves_par
    • leaves_with_par

Custom Walks

  • custom_walk(next_node) method is implemented. This method returns an iterator over the nodes with custom traversal strategy. Traversal strategy is determined by the argument next_node. next_node is a function with the signature Fn(Node) -> Option<Node>. Iteration starts with the self and terminates when next_node returns None. This provides a convenient and flexible way to create custom walks. Notice that iteration in all directions can be defined and infinite iterations can be created. Mutable version custom_walk_mut(next_node) is also implemented for NodeMut.

Allocation-Free Paths Iterator

  • paths was returning impl Iterator<Item = impl Iterator<&V::Item>>. Now it returns impl Iterator<Item = impl Iterator<&V::Item> + Clone>. In other words, each path element is a cheaply cloneable iterator. This allows for converting the path into_iterable which can be iterated over multiple times, and hence, provides the means to avoid allocation and collecting the paths into collections.
  • Examples for different iterators, walks are added and linked from the documentation.

Parallelization

29 May 08:06
6d3f76a
Compare
Choose a tag to compare

This release provides the initial support for parallelization over trees:

  • Introduces par and into_par methods on the Tree. These methods create parallel iterators over all nodes of the tree in arbitrary order. In other words, they are the parallel counterpart of the iter and into_iter methods of the tree.
  • These implementations use direct parallelization over the underlying pinned vector, and hence, result in efficient gains in computation time. Benchmarks and examples are added to test and experiment parallel computations.
  • Parallel execution is handled by the orx-parallel crate which is added as an optional dependency. Importantly note that "orx-parallel" requires "std". Therefore, for no-std use cases this features must be excluded. Since "orx-parallel" is added as a default feature, it must be excluded by --no-default-features.

recursive_set

27 Apr 17:42
847af89
Compare
Choose a tag to compare

recursive_set method is defined and implemented for NodeMut. This method provides an expressive way to update the values of a tree where value of a node is a function of its prior value and values of its children. Since the values of its children subsequently depend on their own children, it immediately follows that the value of the node depends on values of all of its descendants that must be computed to be able to compute the node's value.

In addition mutable_recursive_traversal example is created to demonstrate different ways to approach to this problem.

Fixes 159.

2024edition

11 Apr 08:40
812b6e0
Compare
Choose a tag to compare

Migration to edition2024.

CI action is added.

Upgrade PseudoDefault dependency

11 Feb 16:07
861d69d
Compare
Choose a tag to compare
Merge pull request #156 from orxfun/Upgrade-PseudoDefault-dependency

Upgrade PseudoDefault dependency

Dual license

08 Feb 17:49
fa9ca5c
Compare
Choose a tag to compare
Merge pull request #155 from orxfun/dual-licenses

dual licenses

Use of Collection constrained PinnedVecs

29 Jan 18:33
2d0b092
Compare
Choose a tag to compare

Upgrade to pinned vectors v3.12.

Plant the Tree

25 Jan 21:25
Compare
Choose a tag to compare

orx-tree

orx-tree crate
orx-tree documentation

A beautiful tree 🌳 with convenient and efficient growth, mutation and traversal features.

Features

Generic Variants

Tree is generic over variants that define the way the children are stored:

  • DynTree<T>, or equivalently Tree<Dyn<T>>, is a tree where each node can contain any number of children stored as a vector.
  • DaryTree<D, T>, or equivalently Tree<DaryTree<D, T>>, is a tree where each node can contain at most D children stored inlined as an array.

Recursive Nature of Trees

Note that Tree has only few methods which mainly allow access to the root or to any node using node indices. Since every node represents a subtree rooted at itself, the core tree functionalities are provided as methods of NodeRef and NodeMut, which are immutable and mutable nodes, respectively.

Traversals

We can walk all nodes of a subtree rooted at any node using a generic traversal parameter. For instance, let node be a node of the tree, then:

  • node.walk::<Bfs>() creates an iterator that visits all the nodes belonging to the subtree rooted at the node in the breadth-first order.
  • node.walk_mut::<Dfs>() creates a mutable iterator, this time in the (pre-order) depth-first order.
  • node_into_walk::<PostOrder>(), on the other hand, takes the subtree rooted at the node out of the tree and yields the elements in post-order.

Special Iterators

Abovementioned traverser kinds can be used to create other specialized iterators as well:

  • node.leaves::<Bfs>() yields the leaf nodes in the subtree rooted at node in breadth-first order.
  • node.paths::<Dfs>() yields all the paths or sequences of nodes connecting the node to all of its leaves in the depth-first order.

On the other hand, node.ancestors() provides an upward iterator from the node to the root of the tree.

We also can walk the tree in an alternative desired order by using methods such as:

The tree naturally implements Collection and CollectionMut providing iterators via iter and iter_mut methods. Since the tree is not a linear data structure, these iterators yield elements in an arbitrary (but deterministic) order. The following are some example cases where the traversal order is not important, and hence, these iterators are useful:

  • iter_mut to map data of node; for instance, to double values of all nodes which happen to have an odd value.
  • iter to make reductions; for instance, to get the sum of values of all nodes in a subtree.

Constant Time Access to Nodes via Node Indices

A NodeIdx for a Tree is similar to usize for a slice in that it allows constant time access to the node it is created for.

On the other hand, it is more specific for the node due to the following:

  • usize represents a position of the slice. Say we have the slice [a, b, c]. Currently, index 0 points to element a. However, if we swap the first and third elements, index 0 will now be pointing to c because the usize represents a position on the slice.
  • A node index represents the node it is created for. If the index is created for node a, it will always point to this node no matter how many times we move the node in the tree. Further, we cannot use this node index on another tree and it does not allow access to another node if node a is removed from the tree.

Cache Locality

Nodes of the tree are stored in an underlying PinnedVec with pinned element guarantees. This allows for keeping the nodes close to each other improving cache locality while still providing with constant time mutation methods.

Convenient Mutations

Growth & Move Subtrees Around

There exist five methods that adds descendants to a node:

These methods have the sibling variants such as push_sibling rather than push_child which allows to define the side of the new sibling.

Further, push_parent(value) allows to push a node in between a node and its parent.

All together, these methods allow to insert nodes or subtrees at any position of the tree.

Note that all the growth methods return the indices of the created nodes allowing for a fluent growth of the tree.

Finally, the tree provides methods to swap_subtrees withing the tree.

Removals

We can take out a node from the tree, while connecting its parent to its children via the take_out method.

Alternatively, we can prune by removing a subtree rooted at a particular node, and returns the value of the root node.

If we need the data of all nodes of the removed subtree, we can create an into_walk iterator from the node which will both remove the subtree and yield the data of removed nodes in the selected traversal order.

Opt-in Features

  • std: This is a no-std crate by default, and hence, "std" feature needs to be included when necessary.
  • serde: Tree implements Serialize and Deserialize traits; the "serde" feature needs to be added when required. It uses a linearized representation of the tree as a DepthFirstSequence. You may find de-serialization examples in the corresponding test file.

Example

The following example demonstrates the basic usage of the Tree by constructing and playing around with mutation and traversal methods.

use orx_tree::*;

// # A. BUILDING A TREE

//      1
//     ╱ ╲
//    ╱   ╲
//   2     3
//  ╱ ╲   ╱ ╲
// 4   5 6   7
// |     |  ╱ ╲
// 8     9 10  11

let mut tree = DynTree::new(1i32);

let mut root = tree.root_mut();
let [id2, id3] = root.push_children([2, 3]);
let [id4, _] = tree.node_mut(&id2).push_children([4, 5]);
let id8 = tree.node_mut(&id4).push_child(8);
let [id6, id7] = tree.node_mut(&id3).push_children([6, 7]);
let id9 = tree.node_mut(&id6).push_child(9);
tree.node_mut(&id7).push_children([10, 11]);
println!("{}", &tree);
// 1
// ├──2
// │  ├──4
// │  │  └──8
// │  └──5
// └──3
//     ├──6
//     │  └──9
//     └──7
//         ├──10
//         └──11

// B. NODE

let node4 = tree.node(&id4);

assert!(!node4.is_leaf());
assert!(!node4.is_root());
assert_eq!(node4.depth(), 2);
assert_eq!(node4.height(), 1);
assert_eq!(node4.sibling_idx(), 0);
assert_eq!(node4.parent(), Some(tree.node(&id2)));
assert_eq!(node4.num_children(), 1);
assert...
Read more