From a53d15cd8eebd04985cbe1d407642a8e12057723 Mon Sep 17 00:00:00 2001 From: Madeline Date: Fri, 27 Dec 2024 12:40:43 -0800 Subject: [PATCH 1/5] nits and separated update_height from update_size --- phylo2vec/src/tree_vec/ops/avl.rs | 34 +++++++++++++++++++------------ phylo2vec/src/tree_vec/types.rs | 2 +- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/phylo2vec/src/tree_vec/ops/avl.rs b/phylo2vec/src/tree_vec/ops/avl.rs index bab9b88..fcea451 100644 --- a/phylo2vec/src/tree_vec/ops/avl.rs +++ b/phylo2vec/src/tree_vec/ops/avl.rs @@ -20,6 +20,7 @@ impl Node { } } +/// An AVL tree is a self-balancing binary search tree. pub struct AVLTree { root: Option>, } @@ -43,8 +44,11 @@ impl AVLTree { } } - fn update(n: &mut Box) { + fn update_height(n: &mut Box) { n.height = 1 + usize::max(Self::get_height(&n.left), Self::get_height(&n.right)); + } + + fn update_size(n: &mut Box) { n.size = 1 + Self::get_size(&n.left) + Self::get_size(&n.right); } @@ -57,8 +61,8 @@ impl AVLTree { x.right.as_mut().unwrap().left = t2; // Update heights - Self::update(x.right.as_mut().unwrap()); - Self::update(&mut x); + Self::update_height(x.right.as_mut().unwrap()); + Self::update_height(&mut x); return Some(x); } else { @@ -80,8 +84,8 @@ impl AVLTree { y.left.as_mut().unwrap().right = t2; // Update heights - Self::update(y.left.as_mut().unwrap()); - Self::update(&mut y); + Self::update_height(y.left.as_mut().unwrap()); + Self::update_height(&mut y); return Some(y); } else { @@ -94,7 +98,8 @@ impl AVLTree { } } - fn get_balance(node: &Option>) -> isize { + fn get_balance_factor(node: &Option>) -> 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, @@ -102,9 +107,9 @@ impl AVLTree { } fn balance(node: &mut Option>) -> Option> { - 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 { @@ -113,8 +118,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 { @@ -123,6 +128,7 @@ impl AVLTree { return Self::left_rotate(node); } } + // An AVL tree is balanced if its balance factor is -1, 0, or 1. node.take() } @@ -143,7 +149,9 @@ impl AVLTree { n.right = Self::insert_by_index(n.right.take(), value, index - left_size - 1); } - Self::update(&mut n); + Self::update_height(&mut n); + Self::update_size(&mut n); + return Self::balance(&mut Some(n)); } @@ -190,4 +198,4 @@ impl AVLTree { pub fn get_pairs(&self) -> Vec { self.inorder_traversal() } -} +} \ No newline at end of file diff --git a/phylo2vec/src/tree_vec/types.rs b/phylo2vec/src/tree_vec/types.rs index 5b78761..bbdcdae 100644 --- a/phylo2vec/src/tree_vec/types.rs +++ b/phylo2vec/src/tree_vec/types.rs @@ -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] From 4d53d31479837fcd02dd249ea69bc57778d06c0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Dec 2024 21:26:36 +0000 Subject: [PATCH 2/5] style: pre-commit fixes --- phylo2vec/src/tree_vec/ops/avl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phylo2vec/src/tree_vec/ops/avl.rs b/phylo2vec/src/tree_vec/ops/avl.rs index fcea451..7086d20 100644 --- a/phylo2vec/src/tree_vec/ops/avl.rs +++ b/phylo2vec/src/tree_vec/ops/avl.rs @@ -151,7 +151,7 @@ impl AVLTree { Self::update_height(&mut n); Self::update_size(&mut n); - + return Self::balance(&mut Some(n)); } @@ -198,4 +198,4 @@ impl AVLTree { pub fn get_pairs(&self) -> Vec { self.inorder_traversal() } -} \ No newline at end of file +} From 8c563fb7f9cffc56a3313a315efbb1795b90b22c Mon Sep 17 00:00:00 2001 From: Madeline Date: Fri, 27 Dec 2024 13:34:55 -0800 Subject: [PATCH 3/5] rust convention nit --- phylo2vec/src/tree_vec/ops/avl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phylo2vec/src/tree_vec/ops/avl.rs b/phylo2vec/src/tree_vec/ops/avl.rs index fcea451..bc1f822 100644 --- a/phylo2vec/src/tree_vec/ops/avl.rs +++ b/phylo2vec/src/tree_vec/ops/avl.rs @@ -44,11 +44,11 @@ impl AVLTree { } } - fn update_height(n: &mut Box) { + fn update_height(n: &mut Node) { n.height = 1 + usize::max(Self::get_height(&n.left), Self::get_height(&n.right)); } - fn update_size(n: &mut Box) { + fn update_size(n: &mut Node) { n.size = 1 + Self::get_size(&n.left) + Self::get_size(&n.right); } From 0f499093733ebd901f4cefff20c64952cd7e425c Mon Sep 17 00:00:00 2001 From: Maddie Gordon <50681653+madelinegordon@users.noreply.github.com> Date: Fri, 27 Dec 2024 13:41:13 -0800 Subject: [PATCH 4/5] Update avl.rs From f989f3962847ca00e84c6b21eeafecc8358583a3 Mon Sep 17 00:00:00 2001 From: Madeline Date: Fri, 27 Dec 2024 14:25:58 -0800 Subject: [PATCH 5/5] update size during rotations as well as height --- phylo2vec/src/tree_vec/ops/avl.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/phylo2vec/src/tree_vec/ops/avl.rs b/phylo2vec/src/tree_vec/ops/avl.rs index 661ca22..35698f9 100644 --- a/phylo2vec/src/tree_vec/ops/avl.rs +++ b/phylo2vec/src/tree_vec/ops/avl.rs @@ -44,11 +44,8 @@ impl AVLTree { } } - fn update_height(n: &mut Node) { + fn update_height_and_size(n: &mut Node) { n.height = 1 + usize::max(Self::get_height(&n.left), Self::get_height(&n.right)); - } - - fn update_size(n: &mut Node) { n.size = 1 + Self::get_size(&n.left) + Self::get_size(&n.right); } @@ -60,9 +57,9 @@ impl AVLTree { x.right = Some(y_node); x.right.as_mut().unwrap().left = t2; - // Update heights - Self::update_height(x.right.as_mut().unwrap()); - Self::update_height(&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 { @@ -83,9 +80,9 @@ impl AVLTree { y.left = Some(x_node); y.left.as_mut().unwrap().right = t2; - // Update heights - Self::update_height(y.left.as_mut().unwrap()); - Self::update_height(&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 { @@ -149,8 +146,7 @@ impl AVLTree { n.right = Self::insert_by_index(n.right.take(), value, index - left_size - 1); } - Self::update_height(&mut n); - Self::update_size(&mut n); + Self::update_height_and_size(&mut n); return Self::balance(&mut Some(n)); }