Skip to content

Commit b4cf132

Browse files
authored
Add Infix to Postfix Algorithm and Update DIRECTORY.md (TheAlgorithms#541)
1 parent d3d0a8a commit b4cf132

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

DIRECTORY.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/heap.rs)
4545
* [Lazy Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/lazy_segment_tree.rs)
4646
* [Linked List](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/linked_list.rs)
47+
* [Postfix Evaluation](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/postfix_evaluation.rs)
4748
* Probabilistic
4849
* [Bloom Filter](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/bloom_filter.rs)
4950
* [Count Min Sketch](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/probabilistic/count_min_sketch.rs)
@@ -55,6 +56,7 @@
5556
* [Treap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/treap.rs)
5657
* [Trie](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/trie.rs)
5758
* [Union Find](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/union_find.rs)
59+
* [Veb Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/veb_tree.rs)
5860
* Dynamic Programming
5961
* [Coin Change](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/coin_change.rs)
6062
* [Egg Dropping](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/egg_dropping.rs)
@@ -88,6 +90,10 @@
8890
* [Two Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/general/two_sum.rs)
8991
* Geometry
9092
* [Closest Points](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/closest_points.rs)
93+
* [Graham Scan](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/graham_scan.rs)
94+
* [Jarvis Scan](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/jarvis_scan.rs)
95+
* [Point](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/point.rs)
96+
* [Segment](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/segment.rs)
9197
* Graph
9298
* [Astar](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/astar.rs)
9399
* [Bellman Ford](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/bellman_ford.rs)
@@ -114,6 +120,8 @@
114120
* [Abs](https://github.com/TheAlgorithms/Rust/blob/master/src/math/abs.rs)
115121
* [Aliquot Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/aliquot_sum.rs)
116122
* [Amicable Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/amicable_numbers.rs)
123+
* [Area Of Polygon](https://github.com/TheAlgorithms/Rust/blob/master/src/math/area_of_polygon.rs)
124+
* [Area Under Curve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/area_under_curve.rs)
117125
* [Armstrong Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/armstrong_number.rs)
118126
* [Baby Step Giant Step](https://github.com/TheAlgorithms/Rust/blob/master/src/math/baby_step_giant_step.rs)
119127
* [Bell Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/bell_numbers.rs)
@@ -149,16 +157,21 @@
149157
* [Quadratic Residue](https://github.com/TheAlgorithms/Rust/blob/master/src/math/quadratic_residue.rs)
150158
* [Random](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs)
151159
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs)
160+
* [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs)
152161
* [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs)
153162
* [Simpson Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs)
154163
* [Sine](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sine.rs)
155164
* [Square Root](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_root.rs)
156165
* [Sum Of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs)
166+
* [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs)
157167
* [Trial Division](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trial_division.rs)
158168
* [Zellers Congruence Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/math/zellers_congruence_algorithm.rs)
159169
* Navigation
160170
* [Bearing](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/bearing.rs)
161171
* [Haversine](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/haversine.rs)
172+
* Number Theory
173+
* [Totient Function](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/compute_totient.rs)
174+
* [Kth Factor of N](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs)
162175
* Searching
163176
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)
164177
* [Binary Search Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs)
@@ -177,6 +190,7 @@
177190
* Sorting
178191
* [Bead Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bead_sort.rs)
179192
* [Bitonic Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bitonic_sort.rs)
193+
* [Binary Insertion Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/binary_insertion_sort.rs)
180194
* [Bogo Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bogo_sort.rs)
181195
* [Bubble Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bubble_sort.rs)
182196
* [Bucket Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bucket_sort.rs)
@@ -189,12 +203,14 @@
189203
* [Gnome Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/gnome_sort.rs)
190204
* [Heap Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/heap_sort.rs)
191205
* [Insertion Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/insertion_sort.rs)
206+
* [Intro Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/intro_sort.rs)
192207
* [Merge Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/merge_sort.rs)
193208
* [Odd Even Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/odd_even_sort.rs)
194209
* [Pancake Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/pancake_sort.rs)
195210
* [Patience Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/patience_sort.rs)
196211
* [Pigeonhole Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/pigeonhole_sort.rs)
197212
* [Quick Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort.rs)
213+
* [Quick Sort 3 Ways](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/quick_sort_3_ways.rs)
198214
* [Radix Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/radix_sort.rs)
199215
* [Selection Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/selection_sort.rs)
200216
* [Shell Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/shell_sort.rs)
@@ -219,5 +235,6 @@
219235
* [Reverse](https://github.com/TheAlgorithms/Rust/blob/master/src/string/reverse.rs)
220236
* [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/string/run_length_encoding.rs)
221237
* [Suffix Array](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_array.rs)
238+
* [Suffix Array Manber Myers](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_array_manber_myers.rs)
222239
* [Suffix Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/string/suffix_tree.rs)
223240
* [Z Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/string/z_algorithm.rs)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Function to convert infix expression to postfix expression
2+
fn infix_to_postfix(infix: &str) -> String {
3+
let mut postfix = String::new();
4+
let mut stack: Vec<char> = Vec::new();
5+
6+
// Define the precedence of operators
7+
let precedence = |op: char| -> i32 {
8+
match op {
9+
'+' | '-' => 1,
10+
'*' | '/' => 2,
11+
'^' => 3,
12+
_ => 0,
13+
}
14+
};
15+
16+
for token in infix.chars() {
17+
match token {
18+
c if c.is_alphanumeric() => {
19+
postfix.push(c);
20+
}
21+
'(' => {
22+
stack.push('(');
23+
}
24+
')' => {
25+
while let Some(top) = stack.pop() {
26+
if top == '(' {
27+
break;
28+
}
29+
postfix.push(top);
30+
}
31+
}
32+
'+' | '-' | '*' | '/' | '^' => {
33+
while let Some(top) = stack.last() {
34+
if *top == '(' || precedence(*top) < precedence(token) {
35+
break;
36+
}
37+
postfix.push(stack.pop().unwrap());
38+
}
39+
stack.push(token);
40+
}
41+
_ => {}
42+
}
43+
}
44+
45+
while let Some(top) = stack.pop() {
46+
if top == '(' {
47+
// If there are unmatched parentheses, it's an error.
48+
return "Error: Unmatched parentheses".to_string();
49+
}
50+
postfix.push(top);
51+
}
52+
53+
postfix
54+
}
55+
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
61+
#[test]
62+
fn test_infix_to_postfix() {
63+
assert_eq!(
64+
infix_to_postfix("a-b+c-d*e"),
65+
"ab-c+de*-".to_string()
66+
);
67+
assert_eq!(
68+
infix_to_postfix("a*(b+c)+d/(e+f)"),
69+
"abc+*def+/+".to_string()
70+
);
71+
assert_eq!(
72+
infix_to_postfix("(a-b+c)*(d+e*f)"),
73+
"ab-c+def*+*".to_string()
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)