Skip to content

Commit

Permalink
Merge pull request #95 from madelinegordon/madgord/nits
Browse files Browse the repository at this point in the history
nits in avl.rs
  • Loading branch information
madelinegordon authored Dec 30, 2024
2 parents edb1d52 + f989f39 commit b791139
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
32 changes: 18 additions & 14 deletions phylo2vec/src/tree_vec/ops/avl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Node {
}
}

/// An AVL tree is a self-balancing binary search tree.
pub struct AVLTree {
root: Option<Box<Node>>,
}
Expand All @@ -43,7 +44,7 @@ impl AVLTree {
}
}

fn update(n: &mut Box<Node>) {
fn update_height_and_size(n: &mut Node) {
n.height = 1 + usize::max(Self::get_height(&n.left), Self::get_height(&n.right));
n.size = 1 + Self::get_size(&n.left) + Self::get_size(&n.right);
}
Expand All @@ -56,9 +57,9 @@ impl AVLTree {
x.right = Some(y_node);
x.right.as_mut().unwrap().left = t2;

// Update heights
Self::update(x.right.as_mut().unwrap());
Self::update(&mut x);
// Update height and size values
Self::update_height_and_size(x.right.as_mut().unwrap());
Self::update_height_and_size(&mut x);

return Some(x);
} else {
Expand All @@ -79,9 +80,9 @@ impl AVLTree {
y.left = Some(x_node);
y.left.as_mut().unwrap().right = t2;

// Update heights
Self::update(y.left.as_mut().unwrap());
Self::update(&mut y);
// Update height and size values
Self::update_height_and_size(y.left.as_mut().unwrap());
Self::update_height_and_size(&mut y);

return Some(y);
} else {
Expand All @@ -94,17 +95,18 @@ impl AVLTree {
}
}

fn get_balance(node: &Option<Box<Node>>) -> isize {
fn get_balance_factor(node: &Option<Box<Node>>) -> isize {
// Balance factor is the difference between the height of the left subtree and the right subtree.
match node {
Some(ref n) => Self::get_height(&n.left) as isize - Self::get_height(&n.right) as isize,
None => 0,
}
}

fn balance(node: &mut Option<Box<Node>>) -> Option<Box<Node>> {
let balance = Self::get_balance(node);
if balance > 1 {
if Self::get_balance(&node.as_ref().unwrap().left) >= 0 {
let balance_factor = Self::get_balance_factor(node);
if balance_factor > 1 {
if Self::get_balance_factor(&node.as_ref().unwrap().left) >= 0 {
return Self::right_rotate(node);
} else {
if let Some(ref mut n) = node {
Expand All @@ -113,8 +115,8 @@ impl AVLTree {
return Self::right_rotate(node);
}
}
if balance < -1 {
if Self::get_balance(&node.as_ref().unwrap().right) <= 0 {
if balance_factor < -1 {
if Self::get_balance_factor(&node.as_ref().unwrap().right) <= 0 {
return Self::left_rotate(node);
} else {
if let Some(ref mut n) = node {
Expand All @@ -123,6 +125,7 @@ impl AVLTree {
return Self::left_rotate(node);
}
}
// An AVL tree is balanced if its balance factor is -1, 0, or 1.
node.take()
}

Expand All @@ -143,7 +146,8 @@ impl AVLTree {
n.right = Self::insert_by_index(n.right.take(), value, index - left_size - 1);
}

Self::update(&mut n);
Self::update_height_and_size(&mut n);

return Self::balance(&mut Some(n));
}

Expand Down
2 changes: 1 addition & 1 deletion phylo2vec/src/tree_vec/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// A type alias for the Pair type, which is a tuple representing (child1, child2)
/// A type alias for the Pair type, which is a tuple representing (child1, child2)
pub type Pair = (usize, usize);

/// A type alias for the Ancestry type, which is a vector of vectors representing [child1, child2, parent]
Expand Down

0 comments on commit b791139

Please sign in to comment.