Skip to content

Added Infix to Postfix Algorithm and Updated DIRECTORY.md #541

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
merged 13 commits into from
Oct 5, 2023
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
17 changes: 17 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/heap.rs)
* [Lazy Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/lazy_segment_tree.rs)
* [Linked List](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/linked_list.rs)
* [Postfix Evaluation](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/postfix_evaluation.rs)
* Probabilistic
* [Bloom Filter](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/bloom_filter.rs)
* [Count Min Sketch](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/count_min_sketch.rs)
Expand All @@ -55,6 +56,7 @@
* [Treap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/treap.rs)
* [Trie](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/trie.rs)
* [Union Find](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/union_find.rs)
* [Veb Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/veb_tree.rs)
* Dynamic Programming
* [Coin Change](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/coin_change.rs)
* [Egg Dropping](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/egg_dropping.rs)
Expand Down Expand Up @@ -88,6 +90,10 @@
* [Two Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/general/two_sum.rs)
* Geometry
* [Closest Points](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/closest_points.rs)
* [Graham Scan](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/graham_scan.rs)
* [Jarvis Scan](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/jarvis_scan.rs)
* [Point](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/point.rs)
* [Segment](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/segment.rs)
* Graph
* [Astar](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/astar.rs)
* [Bellman Ford](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/bellman_ford.rs)
Expand All @@ -114,6 +120,8 @@
* [Abs](https://github.com/TheAlgorithms/Rust/blob/master/src/math/abs.rs)
* [Aliquot Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/aliquot_sum.rs)
* [Amicable Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/amicable_numbers.rs)
* [Area Of Polygon](https://github.com/TheAlgorithms/Rust/blob/master/src/math/area_of_polygon.rs)
* [Area Under Curve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/area_under_curve.rs)
* [Armstrong Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/armstrong_number.rs)
* [Baby Step Giant Step](https://github.com/TheAlgorithms/Rust/blob/master/src/math/baby_step_giant_step.rs)
* [Bell Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/bell_numbers.rs)
Expand Down Expand Up @@ -149,16 +157,21 @@
* [Quadratic Residue](https://github.com/TheAlgorithms/Rust/blob/master/src/math/quadratic_residue.rs)
* [Random](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs)
* [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs)
* [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs)
* [Simpson Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs)
* [Sine](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sine.rs)
* [Square Root](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_root.rs)
* [Sum Of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs)
* [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs)
* [Trial Division](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trial_division.rs)
* [Zellers Congruence Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/math/zellers_congruence_algorithm.rs)
* Navigation
* [Bearing](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/bearing.rs)
* [Haversine](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/haversine.rs)
* Number Theory
* [Totient Function](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/compute_totient.rs)
* [Kth Factor of N](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs)
* Searching
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)
* [Binary Search Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs)
Expand All @@ -177,6 +190,7 @@
* Sorting
* [Bead Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bead_sort.rs)
* [Bitonic Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bitonic_sort.rs)
* [Binary Insertion Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/binary_insertion_sort.rs)
* [Bogo Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bogo_sort.rs)
* [Bubble Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bubble_sort.rs)
* [Bucket Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bucket_sort.rs)
Expand All @@ -189,12 +203,14 @@
* [Gnome Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/gnome_sort.rs)
* [Heap Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/heap_sort.rs)
* [Insertion Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/insertion_sort.rs)
* [Intro Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/intro_sort.rs)
* [Merge Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/merge_sort.rs)
* [Odd Even Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/odd_even_sort.rs)
* [Pancake Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/pancake_sort.rs)
* [Patience Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/patience_sort.rs)
* [Pigeonhole Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/pigeonhole_sort.rs)
* [Quick Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort.rs)
* [Quick Sort 3 Ways](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort_3_ways.rs)
* [Radix Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/radix_sort.rs)
* [Selection Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/selection_sort.rs)
* [Shell Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/shell_sort.rs)
Expand All @@ -219,5 +235,6 @@
* [Reverse](https://github.com/TheAlgorithms/Rust/blob/master/src/string/reverse.rs)
* [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/string/run_length_encoding.rs)
* [Suffix Array](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_array.rs)
* [Suffix Array Manber Myers](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_array_manber_myers.rs)
* [Suffix Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_tree.rs)
* [Z Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/string/z_algorithm.rs)
76 changes: 76 additions & 0 deletions src/data_structures/infix_to_postfix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Function to convert infix expression to postfix expression
fn infix_to_postfix(infix: &str) -> String {
let mut postfix = String::new();
let mut stack: Vec<char> = Vec::new();

// Define the precedence of operators
let precedence = |op: char| -> i32 {
match op {
'+' | '-' => 1,
'*' | '/' => 2,
'^' => 3,
_ => 0,
}
};

for token in infix.chars() {
match token {
c if c.is_alphanumeric() => {
postfix.push(c);
}
'(' => {
stack.push('(');
}
')' => {
while let Some(top) = stack.pop() {
if top == '(' {
break;
}
postfix.push(top);
}
}
'+' | '-' | '*' | '/' | '^' => {
while let Some(top) = stack.last() {
if *top == '(' || precedence(*top) < precedence(token) {
break;
}
postfix.push(stack.pop().unwrap());
}
stack.push(token);
}
_ => {}
}
}

while let Some(top) = stack.pop() {
if top == '(' {
// If there are unmatched parentheses, it's an error.
return "Error: Unmatched parentheses".to_string();
}
postfix.push(top);
}

postfix
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_infix_to_postfix() {
assert_eq!(
infix_to_postfix("a-b+c-d*e"),
"ab-c+de*-".to_string()
);
assert_eq!(
infix_to_postfix("a*(b+c)+d/(e+f)"),
"abc+*def+/+".to_string()
);
assert_eq!(
infix_to_postfix("(a-b+c)*(d+e*f)"),
"ab-c+def*+*".to_string()
);
}
}