Skip to content

Commit

Permalink
Merge pull request #27 from azizkayumov/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
azizkayumov authored Oct 12, 2023
2 parents 72fb1bf + b38a953 commit 9b0697b
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,47 @@
[![crates.io](https://img.shields.io/crates/v/lctree)](https://crates.io/crates/lctree)

# lctree
Rust implementation of [Link-Cut-Tree](https://dl.acm.org/doi/10.1145/253262.253347](https://dl.acm.org/doi/pdf/10.1145/800076.802464)): self-balancing data structure to maintain a forest of rooted trees.
Rust implementation of [Link-Cut-Tree](https://dl.acm.org/doi/10.1145/253262.253347](https://dl.acm.org/doi/pdf/10.1145/800076.802464)): self-balancing data structure to maintain a forest of rooted trees through linking and cutting edges.

## Example
This example shows how to link and cut edges:
```rust
use srtree::SRTree;
use lctree::LinkCutTree;

fn main() {
// We form a link-cut tree from a rooted tree with the following structure:
// 0
// / \
// 1 4
// / \ \
// 2 3 5
// /
// 6
let mut lctree = super::LinkCutTree::new(7);
lctree.link(1, 0);
lctree.link(2, 1);
lctree.link(3, 1);
lctree.link(4, 0);
lctree.link(5, 4);
lctree.link(6, 5);
// We form a link-cut tree from a rooted tree with the following structure:
// 0
// / \
// 1 4
// / \ \
// 2 3 5
// /
// 6
let mut lctree = lctree::LinkCutTree::new(7);
lctree.link(1, 0);
lctree.link(2, 1);
lctree.link(3, 1);
lctree.link(4, 0);
lctree.link(5, 4);
lctree.link(6, 5);

// Checking connectivity:
for i in 0..7 {
for j in 0..7 {
assert!(lctree.connected(i, j));
}
}
// Checking connectivity:
assert!(lctree.connected(2, 6)); // connected

// We cut node 4 from its parent 0:
lctree.cut(4, 0);
// We cut node 4 from its parent 0:
lctree.cut(4, 0);

// The forest should now look like this:
// 0
// /
// 1 4
// / \ \
// 2 3 5
// /
// 6
// The forest should now look like this:
// 0
// /
// 1 4
// / \ \
// 2 3 5
// /
// 6

// We check connectivity again for the two trees:
for i in 0..4 {
for j in 0..4 {
assert!(lctree.connected(i, j));
}
}
for i in 4..7 {
for j in 4..7 {
assert!(lctree.connected(i, j));
}
}
for i in 0..4 {
for j in 4..7 {
assert!(!lctree.connected(i, j));
}
}
// We check connectivity again:
assert!(!lctree.connected(2, 6)); // not connected anymore
}
```

Expand Down

0 comments on commit 9b0697b

Please sign in to comment.