-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from azizkayumov/connected
Connected
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::{ | ||
access::access, | ||
node::{Node, Parent}, | ||
}; | ||
|
||
pub fn connected(forest: &mut Vec<Node>, v: usize, w: usize) -> bool { | ||
assert!( | ||
v < forest.len() || w < forest.len(), | ||
"splay: node_idx out of bounds" | ||
); | ||
access(forest, v); // v is now the root of the tree | ||
access(forest, w); | ||
// if v is not the root of the tree anymore, then v and w are connected: | ||
!matches!(forest[v].parent, Parent::Root) || v == w | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::node::{Node, Parent}; | ||
|
||
fn create_nodes(n: usize) -> Vec<Node> { | ||
(0..n).map(|i| Node::new(i, 0.0)).collect() | ||
} | ||
|
||
#[test] | ||
pub fn base_case() { | ||
// check two nodes, should return false | ||
let mut forest = create_nodes(2); | ||
assert!(!super::connected(&mut forest, 0, 1)); | ||
} | ||
|
||
#[test] | ||
pub fn connected_with_root() { | ||
// check three nodes, one is root: | ||
// 0 | ||
// / \ | ||
// 1 2 | ||
let mut forest = create_nodes(3); | ||
forest[0].left = Some(1); | ||
forest[0].right = Some(2); | ||
forest[1].parent = Parent::Node(0); | ||
forest[2].parent = Parent::Node(0); | ||
|
||
assert!(super::connected(&mut forest, 0, 1)); | ||
assert!(super::connected(&mut forest, 0, 2)); | ||
assert!(super::connected(&mut forest, 1, 2)); | ||
assert!(super::connected(&mut forest, 0, 0)); | ||
assert!(super::connected(&mut forest, 1, 1)); | ||
assert!(super::connected(&mut forest, 2, 2)); | ||
} | ||
|
||
#[test] | ||
pub fn connected_with_path_pointers() { | ||
// check two trees that are connected by a path pointer | ||
// 0 | ||
// / \ | ||
// 1 2 | ||
// | | ||
// 3 | ||
let mut forest = create_nodes(4); | ||
forest[0].left = Some(1); | ||
forest[0].right = Some(2); | ||
forest[1].parent = Parent::Node(0); | ||
forest[2].parent = Parent::Node(0); | ||
forest[3].parent = Parent::Path(2); | ||
|
||
assert!(super::connected(&mut forest, 0, 3)); | ||
assert!(super::connected(&mut forest, 1, 3)); | ||
assert!(super::connected(&mut forest, 2, 3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![allow(dead_code, clippy::module_name_repetitions)] // yes, I want to name my structs with the same name as the file | ||
mod access; | ||
mod connected; | ||
mod node; | ||
mod splay; |