init
This commit is contained in:
commit
5aa6d2be89
14 changed files with 5093 additions and 0 deletions
37
examples/tree.rs
Normal file
37
examples/tree.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use noders::{
|
||||
display::display,
|
||||
graph::{Graph, NodeId},
|
||||
};
|
||||
use rand::{SeedableRng, prelude::*};
|
||||
use rand_xorshift::XorShiftRng;
|
||||
|
||||
fn main() {
|
||||
let mut rng = XorShiftRng::seed_from_u64(111111111112111122);
|
||||
let mut graph = Graph::empty();
|
||||
let root = graph.node("ROOT");
|
||||
branch(&mut graph, &mut rng, root, vec![], 7, 5);
|
||||
dbg!(graph.nodes.len());
|
||||
display(graph);
|
||||
}
|
||||
|
||||
pub fn branch(
|
||||
graph: &mut Graph,
|
||||
rng: &mut XorShiftRng,
|
||||
parent: NodeId,
|
||||
path: Vec<usize>,
|
||||
max_children: usize,
|
||||
max_depth: usize,
|
||||
) {
|
||||
if max_depth == 0 {
|
||||
return;
|
||||
}
|
||||
let child_count = rng.random_range(0..max_children);
|
||||
for index in 1..child_count {
|
||||
let mut path = path.clone();
|
||||
path.push(index);
|
||||
let label = path.iter().map(ToString::to_string).collect::<Vec<_>>().join(".");
|
||||
let child = graph.node(label);
|
||||
graph.link(child, parent);
|
||||
branch(graph, rng, child, path, max_children, max_depth - 1);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue