Skip to content

Commit

Permalink
version 0.4.1
Browse files Browse the repository at this point in the history
1. [doc] update README.md to reflect changes in version 0.4.
  • Loading branch information
oooutlk committed Jan 25, 2021
1 parent e1e74b4 commit 29d1b2f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trees"
version = "0.4.0"
version = "0.4.1"
edition = "2018"
authors = ["oooutlk <oooutlk@outlook.com>"]
license = "MIT/Apache-2.0"
Expand Down
85 changes: 28 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,52 @@
This project provides various implementations of trees serving for general purpose.
This project provides trees data structure serving for general purpose.

# Features

- Traversal

Each node in a tree provides standard forward iterators for visiting its children nodes. Tree traversal can be done using these iterators in recursive function calls.

Depth-first search and breadth-first iterators are also provided.


- Operation

The methods for adding or removing child tree at the front/back of a node’s children list are guaranteed constant time. Accessing, inserting or removing nodes in any position are linear time.

The `potted` trees constructed in batch mode can randomly access the child nodes in constant time.

The library users does not need any `unsafe` code to use this library.


- Notation

Tow compact notations of tree construction has been developed by overloading sub and div operators. They make complex tree composition look like literal expression, reducing a bit of syntax noise.
## Quickstart

The first notation is using operator overloading. Using operator '-' to express sibling relationship, and '/' to express parent-child relationship.
Something like `root /( first_child - second_child )`.
Impatient readers can start with the
[notations](https://oooutlk.github.io/trees/notations.html).

The second notation is using Rust tuple, something like `( root, first_child, second_child )`.

See the example section for more.

# Implementations

Currently this library provides three different trees, implemented in raw-pointer-linked or vec-backed nodes.
# Features

- `linked::singly`
Two pointers per node. Hold no size infomation. Note that constant time `pop_back` is not supported.
1. Step-by-step
[creating, reading, updating, deleting](https://oooutlk.github.io/trees/crud.html)
and iterating nodes with assocated data items.

- `linked::singly`
Four pointers plus two `u32`s per node. Hold children count and node count.
2. Compact notations to express trees: `-`,`/` encoded or tuple encoded trees.

- `potted`
Seven `u32`s per node. Hold children count, node count and adjoined children count.
3. Depth first search cursor.

# Prominent types
4. Breadth first search iterators.

Tree, Forest and Node are the big three types in this library.
5. Trees can be built by stages, with nodes stored scatteredly among memory.

- Tree is a collection of owned nodes in hierarchical structure, with one top-level node named root.
6. Trees can be built once through, with nodes stored contiguously.

- Forest is similar to Tree, except that it has no root node.
7. Support exclusive ownership with static borrow check.

- Node is the underlying storage type and **opaque** to the library users. Instead, `&Node`/`&mut Node` or `NodeRef`/`NodeMut` are exposed.
8. Support shared ownership with dynamic borrow check.

# Examples

- notation of a literal tree

```rust
let linked_tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
let potted_tree = potted::Tree::from(( 0, (1,2,3), (4,5,6) ));
use trees::tr;

let scattered_tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
let piled_tree = trees::Tree::from(( 0, (1,2,3), (4,5,6) ));
```

They both encode a tree drawn as follows:

```text
```text
.............
. 0 .
. / \ .
. 1 4 .
. / \ / \ .
.2 3 5 6.
.............
```
```

- use tree notation to reduce syntax noise, quoted from crate `reflection_derive`, [version 0.1.1](https://github.com/oooutlk/reflection/blob/master/reflection_derive/src/lib.rs#L202):

Expand All @@ -98,23 +73,23 @@ Tree, Forest and Node are the big three types in this library.
- use iterators if the tree travesal is a "driving wheel"( you can iterate over the tree on your own ).

```rust
use trees::{tr,Node};
use trees::{Node, tr};
use std::fmt::Display;

let tree = tr(0)
/( tr(1) /tr(2)/tr(3) )
/( tr(4) /tr(5)/tr(6) );

fn tree_to_string<T:Display>( node: &Node<T> ) -> String {
if node.is_leaf() {
if node.has_no_child() {
node.data.to_string()
} else {
format!( "{}( {})", node.data,
format!( "{}( {})", node.data,
node.iter().fold( String::new(),
|s,c| s + &tree_to_string(c) + &" " ))
}
}

assert_eq!( tree_to_string( &tree ), "0( 1( 2 3 ) 4( 5 6 ) )" );
```

Expand All @@ -136,7 +111,3 @@ Tree, Forest and Node are the big three types in this library.
# License

Under Apache License 2.0 or MIT License, at your will.

# Quickstart

API document and quickstart here: [docs.rs]( https://docs.rs/trees/ )

0 comments on commit 29d1b2f

Please sign in to comment.