Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from TheAlgorithms:master #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from TheAlgorithms:master
Oct 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Cargo.toml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ doc_markdown = { level = "allow", priority = 1 }
explicit_deref_methods = { level = "allow", priority = 1 }
explicit_iter_loop = { level = "allow", priority = 1 }
float_cmp = { level = "allow", priority = 1 }
if_not_else = { level = "allow", priority = 1 }
implicit_clone = { level = "allow", priority = 1 }
implicit_hasher = { level = "allow", priority = 1 }
items_after_statements = { level = "allow", priority = 1 }
Expand Down Expand Up @@ -68,7 +67,6 @@ used_underscore_binding = { level = "allow", priority = 1 }
ref_option = { level = "allow", priority = 1 }
unnecessary_semicolon = { level = "allow", priority = 1 }
ignore_without_reason = { level = "allow", priority = 1 }
needless_for_each = { level = "allow", priority = 1 }
# restriction-lints:
absolute_paths = { level = "allow", priority = 1 }
arithmetic_side_effects = { level = "allow", priority = 1 }
Expand Down Expand Up @@ -169,5 +167,3 @@ doc_overindented_list_items = { level = "allow", priority = 1 }
# complexity-lints
precedence = { level = "allow", priority = 1 }
manual_div_ceil = { level = "allow", priority = 1 }
# perf-lints
cloned_ref_to_slice_refs = { level = "allow", priority = 1 }
6 changes: 3 additions & 3 deletions src/ciphers/transposition.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub fn transposition(decrypt_mode: bool, msg: &str, key: &str) -> String {

key_ascii.sort_by_key(|&(index, _)| index);

key_ascii
.into_iter()
.for_each(|(_, key)| key_order.push(key.into()));
for (_, key) in key_ascii {
key_order.push(key.into());
}

// Determines whether to encrypt or decrypt the message,
// and returns the result
Expand Down
6 changes: 3 additions & 3 deletions src/data_structures/b_tree.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl BTreeProps {
}
None => Vec::with_capacity(self.max_keys),
};
let right_children = if !child.is_leaf() {
Some(child.children.split_off(self.mid_key_index + 1))
} else {
let right_children = if child.is_leaf() {
None
} else {
Some(child.children.split_off(self.mid_key_index + 1))
};
let new_child_node: Node<T> = Node::new(self.degree, Some(right_keys), right_children);

Expand Down
196 changes: 97 additions & 99 deletions src/data_structures/rb_tree.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ impl<K: Ord, V> RBTree<K, V> {
}
}
node = Box::into_raw(Box::new(RBNode::new(key, value)));
if !parent.is_null() {
if (*node).key < (*parent).key {
(*parent).left = node;
} else {
(*parent).right = node;
}
} else {
if parent.is_null() {
self.root = node;
} else if (*node).key < (*parent).key {
(*parent).left = node;
} else {
(*parent).right = node;
}
(*node).parent = parent;
insert_fixup(self, node);
Expand Down Expand Up @@ -258,17 +256,17 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode

gparent = (*parent).parent;
tmp = (*gparent).right;
if parent != tmp {
/* parent = (*gparent).left */
if parent == tmp {
/* parent = (*gparent).right */
tmp = (*gparent).left;
if !tmp.is_null() && matches!((*tmp).color, Color::Red) {
/*
* Case 1 - color flips and recurse at g
*
* G g
* / \ / \
* p u --> P U
* / /
* n n
* G g
* / \ / \
* u p --> U P
* \ \
* n n
*/

(*parent).color = Color::Black;
Expand All @@ -278,46 +276,45 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode
parent = (*node).parent;
continue;
}
tmp = (*parent).right;
tmp = (*parent).left;
if node == tmp {
/* node = (*parent).right */
/*
* Case 2 - left rotate at p (then Case 3)
* Case 2 - right rotate at p (then Case 3)
*
* G G
* / \ / \
* p U --> n U
* \ /
* n p
* G G
* / \ / \
* U p --> U n
* / \
* n p
*/

left_rotate(tree, parent);
right_rotate(tree, parent);
parent = node;
}
/*
* Case 3 - right rotate at g
* Case 3 - left rotate at g
*
* G P
* / \ / \
* p U --> n g
* / \
* n U
* G P
* / \ / \
* U p --> g n
* \ /
* n U
*/

(*parent).color = Color::Black;
(*gparent).color = Color::Red;
right_rotate(tree, gparent);
left_rotate(tree, gparent);
} else {
/* parent = (*gparent).right */
tmp = (*gparent).left;
/* parent = (*gparent).left */
if !tmp.is_null() && matches!((*tmp).color, Color::Red) {
/*
* Case 1 - color flips and recurse at g
* G g
* / \ / \
* u p --> U P
* \ \
* n n
*
* G g
* / \ / \
* p u --> P U
* / /
* n n
*/

(*parent).color = Color::Black;
Expand All @@ -327,34 +324,35 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode
parent = (*node).parent;
continue;
}
tmp = (*parent).left;
tmp = (*parent).right;
if node == tmp {
/* node = (*parent).right */
/*
* Case 2 - right rotate at p (then Case 3)
* Case 2 - left rotate at p (then Case 3)
*
* G G
* / \ / \
* U p --> U n
* / \
* n p
* G G
* / \ / \
* p U --> n U
* \ /
* n p
*/

right_rotate(tree, parent);
left_rotate(tree, parent);
parent = node;
}
/*
* Case 3 - left rotate at g
* Case 3 - right rotate at g
*
* G P
* / \ / \
* U p --> g n
* \ /
* n U
* G P
* / \ / \
* p U --> n g
* / \
* n U
*/

(*parent).color = Color::Black;
(*gparent).color = Color::Red;
left_rotate(tree, gparent);
right_rotate(tree, gparent);
}
break;
}
Expand All @@ -377,7 +375,52 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut parent: *mut RBNo
* black node count that is 1 lower than other leaf paths.
*/
sibling = (*parent).right;
if node != sibling {
if node == sibling {
/* node = (*parent).right */
sibling = (*parent).left;
if matches!((*sibling).color, Color::Red) {
/*
* Case 1 - right rotate at parent
*/

right_rotate(tree, parent);
(*parent).color = Color::Red;
(*sibling).color = Color::Black;
sibling = (*parent).right;
}
sl = (*sibling).left;
sr = (*sibling).right;

if !sr.is_null() && matches!((*sr).color, Color::Red) {
/*
* Case 2 - left rotate at sibling and then right rotate at parent
*/

(*sr).color = (*parent).color;
(*parent).color = Color::Black;
left_rotate(tree, sibling);
right_rotate(tree, parent);
} else if !sl.is_null() && matches!((*sl).color, Color::Red) {
/*
* Case 3 - right rotate at parent
*/

(*sl).color = (*parent).color;
right_rotate(tree, parent);
} else {
/*
* Case 4 - color flip
*/

(*sibling).color = Color::Red;
if matches!((*parent).color, Color::Black) {
node = parent;
parent = (*node).parent;
continue;
}
(*parent).color = Color::Black;
}
} else {
/* node = (*parent).left */
if matches!((*sibling).color, Color::Red) {
/*
Expand Down Expand Up @@ -442,51 +485,6 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut parent: *mut RBNo
* Sl Sr Sl Sr
*/

(*sibling).color = Color::Red;
if matches!((*parent).color, Color::Black) {
node = parent;
parent = (*node).parent;
continue;
}
(*parent).color = Color::Black;
}
} else {
/* node = (*parent).right */
sibling = (*parent).left;
if matches!((*sibling).color, Color::Red) {
/*
* Case 1 - right rotate at parent
*/

right_rotate(tree, parent);
(*parent).color = Color::Red;
(*sibling).color = Color::Black;
sibling = (*parent).right;
}
sl = (*sibling).left;
sr = (*sibling).right;

if !sr.is_null() && matches!((*sr).color, Color::Red) {
/*
* Case 2 - left rotate at sibling and then right rotate at parent
*/

(*sr).color = (*parent).color;
(*parent).color = Color::Black;
left_rotate(tree, sibling);
right_rotate(tree, parent);
} else if !sl.is_null() && matches!((*sl).color, Color::Red) {
/*
* Case 3 - right rotate at parent
*/

(*sl).color = (*parent).color;
right_rotate(tree, parent);
} else {
/*
* Case 4 - color flip
*/

(*sibling).color = Color::Red;
if matches!((*parent).color, Color::Black) {
node = parent;
Expand Down
9 changes: 6 additions & 3 deletions src/general/huffman_encoding.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ impl<T: Clone + Copy + Ord> HuffmanDictionary<T> {
}
pub fn encode(&self, data: &[T]) -> HuffmanEncoding {
let mut result = HuffmanEncoding::new();
data.iter()
.for_each(|value| result.add_data(self.alphabet[value]));
for value in data.iter() {
result.add_data(self.alphabet[value]);
}
result
}
}
Expand Down Expand Up @@ -237,7 +238,9 @@ mod tests {
use super::*;
fn get_frequency(bytes: &[u8]) -> Vec<(u8, u64)> {
let mut cnts: Vec<u64> = vec![0; 256];
bytes.iter().for_each(|&b| cnts[b as usize] += 1);
for &b in bytes.iter() {
cnts[b as usize] += 1;
}
let mut result = vec![];
cnts.iter()
.enumerate()
Expand Down
2 changes: 1 addition & 1 deletion src/geometry/ramer_douglas_peucker.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod tests {

assert_eq!(ramer_douglas_peucker(&[], epsilon), vec![]);
assert_eq!(
ramer_douglas_peucker(&[a.clone()], epsilon),
ramer_douglas_peucker(std::slice::from_ref(&a), epsilon),
vec![a.clone()]
);
assert_eq!(
Expand Down
8 changes: 6 additions & 2 deletions src/graph/graph_enumeration.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ mod tests {
let mut result = enumerate_graph(&graph);
let expected = vec![vec![], vec![2, 3], vec![1, 3, 4], vec![1, 2], vec![2]];

result.iter_mut().for_each(|v| v.sort_unstable());
for v in result.iter_mut() {
v.sort_unstable();
}
assert_eq!(result, expected);
}

Expand All @@ -55,7 +57,9 @@ mod tests {
let mut result = enumerate_graph(&graph);
let expected = vec![vec![], vec![2, 3], vec![1, 3, 4], vec![1, 2], vec![2]];

result.iter_mut().for_each(|v| v.sort_unstable());
for v in result.iter_mut() {
v.sort_unstable();
}
assert_eq!(result, expected);
}
}
6 changes: 3 additions & 3 deletions src/graph/lee_breadth_first_search.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ pub fn lee(matrix: Vec<Vec<i32>>, source: (usize, usize), destination: (usize, u
}
}

if min_dist != isize::MAX {
min_dist
} else {
if min_dist == isize::MAX {
-1
} else {
min_dist
}
}

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /