You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63-23Lines changed: 63 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,40 +11,61 @@ A beautiful tree 🌳 with convenient and efficient growth, mutation and travers
11
11
12
12
[`Tree`](https://docs.rs/orx-tree/latest/orx_tree/struct.Tree.html) is generic over variants that define the way the children are stored:
13
13
14
-
*[`DynTree<T>`](https://docs.rs/orx-tree/latest/orx_tree/type.DynTree.html), or equivalently `Tree<Dyn<T>>`, is a tree where each node can contain any number of children stored as a vector.
15
-
*[`DaryTree<D, T>`](https://docs.rs/orx-tree/latest/orx_tree/type.DaryTree.html), or equivalently `Tree<DaryTree<D, T>>`, is a tree where each node can contain at most `D` children stored inlined as an array.
16
-
*[`BinaryTree<T>`](https://docs.rs/orx-tree/latest/orx_tree/type.BinaryTree.html) is simply a shorthand for `DaryTree<2, T>`.
14
+
*[`DynTree<T>`](https://docs.rs/orx-tree/latest/orx_tree/type.DynTree.html), or equivalently **Tree<Dyn<T>>**, is a tree where each node may contain references to any number of children stored as a vector.
15
+
*[`DaryTree<D, T>`](https://docs.rs/orx-tree/latest/orx_tree/type.DaryTree.html), or equivalently **Tree<DaryTree<D, T>>**, is a tree where each node may contain at most **D** child references stored inlined as an array.
16
+
*[`BinaryTree<T>`](https://docs.rs/orx-tree/latest/orx_tree/type.BinaryTree.html) is simply a shorthand for **DaryTree<2, T>**.
17
17
18
18
### Recursive Nature of Trees
19
19
20
-
Note that [`Tree`](https://docs.rs/orx-tree/latest/orx_tree/struct.Tree.html) 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`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html) and [`NodeMut`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html), which are immutable and mutable nodes, respectively.
20
+
Note that [`Tree`](https://docs.rs/orx-tree/latest/orx_tree/struct.Tree.html) has only few methods which mainly allow access to the root or to any node using node indices. Since every node represents a subtree with itself being the root of, the core tree functionalities are provided as methods of [`NodeRef`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html) and [`NodeMut`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html), which are immutable and mutable nodes, respectively.
21
21
22
22
### Traversals
23
23
24
-
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:
24
+
We can iterate over all nodes of a subtree in various ways. In other words, we can *walk* the nodes of any subtree using a generic parameter which defines the order of traversal.
25
+
26
+
To illustrate, let `node` be any node of the tree. Then:
25
27
26
28
*[`node.walk::<Bfs>()`](https://docs.rs/orx-tree/latest/orx_tree/traversal/struct.Bfs.html) creates an iterator that visits all the nodes belonging to the subtree rooted at the *node* in the breadth-first order.
27
-
*[`node.walk_mut::<Dfs>()`](https://docs.rs/orx-tree/latest/orx_tree/traversal/struct.Dfs.html) creates a mutable iterator, this time in the (pre-order) depth-first order.
29
+
*[`node.walk_mut::<Dfs>()`](https://docs.rs/orx-tree/latest/orx_tree/traversal/struct.Dfs.html) creates a mutable iterator, this time in depth-first (pre-)order.
28
30
*[`node_into_walk::<PostOrder>()`](https://docs.rs/orx-tree/latest/orx_tree/traversal/struct.PostOrder.html), on the other hand, takes the subtree rooted at the *node* out of the tree and yields the elements in post-order.
29
31
32
+
We can iterate over the data of the nodes, or over the nodes themselves with access to children, parent, siblings, etc. Further, just like *enumerate* appends the iteration order in a regular iterator, we can append tree-specific values to the iteration elements. Specifically, we can add the depth and/or the sibling position of each yield node. These more specialized traversals can be created conveniently using the [`Traversal`](https://docs.rs/orx-tree/latest/orx_tree/traversal/struct.Traversal.html) builder type.
33
+
34
+
35
+
```rust
36
+
useorx_tree::*;
37
+
38
+
letmuttree=DynTree::new(1);
39
+
let [id2, _] =tree.root_mut().push_children([2, 3]);
40
+
tree.node_mut(&id2).push_child(4);
41
+
42
+
// create a re-usable BFS traverser: over nodes, appending depth and sibling-idx
Abovementioned traverser kinds can be used to create other specialized iterators as well:
55
+
In addition to iterators over all nodes of a subtree, we can create specialized iterators as well:
33
56
34
57
*[`node.leaves::<Bfs>()`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html#method.leaves) yields the leaf nodes in the subtree rooted at *node* in breadth-first order.
35
58
*[`node.paths::<Dfs>()`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html#method.paths) yields all the paths or sequences of nodes connecting the *node* to all of its leaves in the depth-first order.
59
+
*[`node.ancestors()`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html#method.ancestors) provides an upward iterator from the *node* to the root of the tree.
36
60
37
-
On the other hand, [`node.ancestors()`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html#method.ancestors) provides an upward iterator from the *node* to the root of the tree.
38
-
39
-
We also can walk the tree in an alternative desired order by using methods such as:
61
+
Alternatively, we can walk the tree freely using methods to step the links in different ways, such as:
*[`node.parent()`](https://docs.rs/orx-tree/latest/orx_tree/trait.NodeRef.html#method.parent), [`node.into_parent_mut()`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.into_parent_mut), etc.
43
65
44
-
The tree naturally implements [`Collection`](https://docs.rs/orx-iterable/latest/orx_iterable/trait.Collection.html) and [`CollectionMut`](https://docs.rs/orx-iterable/latest/orx_iterable/trait.CollectionMut.html) 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:
66
+
### Arbitrary Order Iterators
45
67
46
-
*`iter_mut` to map data of node; for instance, to double values of all nodes which happen to have an odd value.
47
-
*`iter` to make reductions; for instance, to get the sum of values of all nodes in a subtree.
68
+
The tree naturally implements [`Collection`](https://docs.rs/orx-iterable/latest/orx_iterable/trait.Collection.html) and [`CollectionMut`](https://docs.rs/orx-iterable/latest/orx_iterable/trait.CollectionMut.html) 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, which is useful in certain situations such as updating the values of the tree using a transformation or applying reductions.
48
69
49
70
### Constant Time Access to Nodes via Node Indices
50
71
@@ -55,46 +76,65 @@ On the other hand, it is more specific for the node due to the following:
55
76
* 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.
56
77
* 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.
57
78
79
+
Therefore, node access through node indices is safe. To demonstrate, assume we have the following command:
80
+
81
+
```rust ignore
82
+
letidx=tree.root_mut().push_child(42);
83
+
```
84
+
85
+
Here, `idx` does not have a lifetime attached to the `tree`, yet it refers to the node on this tree which currently holds value 42 (thanks to pinned element guarantees). This allows for a safe and efficient access to the nodes:
86
+
87
+
*`tree.node(&idx)` provides a constant time access to this particular node.
88
+
*`another_tree.node(&idx)` is an out-of-bounds error.
89
+
*`tree.node(&idx)` after removing the node from the tree, say by `tree.node_mut(&idx).prune()` call, is a removed-node error.
90
+
91
+
58
92
### Cache Locality
59
93
60
94
Nodes of the tree are stored in an underlying [`PinnedVec`](https://crates.io/crates/orx-pinned-vec) 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.
61
95
62
96
### Convenient Mutations
63
97
98
+
The tree aims to make every move on the tree possible, convenient and efficient.
99
+
64
100
#### Growth & Move Subtrees Around
65
101
66
-
There exist five methods that adds descendants to a node:
102
+
The following methods demonstrate downward growth by adding descendants to a node:
67
103
68
104
*[`push_child(value)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_child) => adds a single child
69
105
*[`push_children(values)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_children) => adds a constant number of children
70
106
*[`extend_children(values)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.extend_children) => adds a variable number of children provided by an iterator
71
107
*[`push_child_tree(subtree)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_child_tree) => appends the subtree as descendants of the *node* such that the root of the subtree is the child of the *node*
72
108
*[`push_child_tree_within(subtree)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_child_tree_within) => similar to the above except that the subtree belongs to the same tree, we might be moving or cloning the subtree
73
109
74
-
These methods have the *sibling* variants such as [`push_sibling`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_sibling) rather than *push_child* which allows to define the side of the new sibling.
110
+
These methods have the *sibling* variants such as [`push_sibling`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_sibling) rather than *push_child* which additionally allows to define the side of the new sibling (to the left or right).
75
111
76
112
Further, [`push_parent(value)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_parent) allows to push a node in between a node and its parent.
77
113
78
-
All together, these methods allow to insert nodes or subtrees at any position of the tree.
114
+
These methods aim to enable inserting nodes or subtrees at any position of the tree.
79
115
80
116
Note that all the growth methods return the indices of the created nodes allowing for a fluent growth of the tree.
81
117
82
-
Finally, the tree provides methods to [`swap_subtrees`](https://docs.rs/orx-tree/latest/orx_tree/struct.Tree.html#method.swap_subtrees)withing the tree.
118
+
Additionally, the tree provides methods for special moves such as [`swap_subtrees`](https://docs.rs/orx-tree/latest/orx_tree/struct.Tree.html#method.swap_subtrees)to swap components of the same tree.
83
119
84
120
#### Removals
85
121
86
122
We can take out a node from the tree, while connecting its parent to its children via the [`take_out`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.take_out) method.
87
123
88
-
Alternatively, we can [`prune`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.prune) by removing a subtree rooted at a particular node, and returns the value of the root node.
124
+
Alternatively, we can [`prune`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.prune) by removing a subtree rooted at a particular node, and receive the value of the root node of the removed subtree.
89
125
90
-
If we need the data of all nodes of the removed subtree, we can create an [`into_walk`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.into_walk) iterator from the node which will both remove the subtree and yield the data of removed nodes in the selected traversal order.
126
+
Alternatively, we can turn a mutable node into an [`into_walk`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.into_walk) iterator. Similar to *prune*, this will remove the subtree. However, we are flexible on what we do with the removed subtree:
127
+
128
+
* We can simply discard it. Then, *into_walk* behaves similar to *prune*.
129
+
* We can iterate over the removed nodes in the order of the generic traversal parameter and use the data however we need.
130
+
* Or we can attach the removed subtree at a desired position of another tree by passing it to methods such as [`push_child_tree(subtree)`](https://docs.rs/orx-tree/latest/orx_tree/struct.NodeMut.html#method.push_child_tree).
91
131
92
132
## Opt-in Features
93
133
94
134
***std**: This is a no-std crate by default, and hence, "std" feature needs to be included when necessary.
95
135
***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`](https://docs.rs/orx-tree/latest/orx_tree/struct.DepthFirstSequence.html). You may find de-serialization examples in the corresponding [test file](https://github.com/orxfun/orx-tree/blob/main/tests/serde.rs).
96
136
97
-
# Example
137
+
# Examples
98
138
99
139
The following example demonstrates the basic usage of the `Tree` by constructing and playing around with mutation and traversal methods.
100
140
@@ -324,14 +364,14 @@ let bfs: Vec<_> = tree2.root().walk::<Bfs>().copied().collect();
324
364
assert_eq!(bfs, [2, 4, 5, 8]);
325
365
326
366
// let's move subtree rooted at n7 to its own tree, this time a BinaryTree
0 commit comments