Skip to content

Commit

Permalink
chore(exercise): update
Browse files Browse the repository at this point in the history
  • Loading branch information
Whth committed Sep 24, 2024
1 parent a91ec20 commit 001335c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 16 deletions.
19 changes: 13 additions & 6 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
graph
This problem requires you to implement a basic graph functio
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE

use std::collections::{HashMap, HashSet};
use std::fmt;
Expand All @@ -29,7 +28,15 @@ impl Graph for UndirectedGraph {
&self.adjacency_table
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
let (from_node, to_node, weight) = edge;
self.adjacency_table_mutable()
.entry(from_node.to_string())
.or_insert(Vec::new())
.push((to_node.to_string(), weight));
self.adjacency_table_mutable()
.entry(to_node.to_string())
.or_insert(Vec::new())
.push((from_node.to_string(), weight));
}
}
pub trait Graph {
Expand All @@ -38,7 +45,7 @@ pub trait Graph {
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool {
//TODO
true
true
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
Expand Down Expand Up @@ -81,4 +88,4 @@ mod test_undirected_graph {
assert_eq!(graph.edges().contains(edge), true);
}
}
}
}
91 changes: 81 additions & 10 deletions exercises/algorithm/algorithm9.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
heap
This question requires you to implement a binary heap function
heap
This question requires you to implement a binary heap function
*/
// I AM NOT DONE

use std::cmp::Ord;
use std::default::Default;
use std::fmt::{Debug, Display};

pub struct Heap<T>
where
Expand All @@ -14,6 +14,7 @@ where
count: usize,
items: Vec<T>,
comparator: fn(&T, &T) -> bool,
current: usize,
}

impl<T> Heap<T>
Expand All @@ -25,6 +26,7 @@ where
count: 0,
items: vec![T::default()],
comparator,
current: 1,
}
}

Expand All @@ -37,7 +39,29 @@ where
}

pub fn add(&mut self, value: T) {
//TODO
self.items.push(value);
self.count += 1;
self.rectify()
}

fn rectify(&mut self) {
let mut need_rectify = self.count > 1;
while need_rectify {
need_rectify = false;
for i in (2..self.count + 1).rev() {
let parent_idx = self.parent_idx(i);

if (self.comparator)(&self.items[i], &self.items[parent_idx]) {
println!("swap {} and {}", i, parent_idx);
self.items.swap(i, parent_idx);
need_rectify = true;

if parent_idx == 1 {
self.current = 1
}
}
}
}
}

fn parent_idx(&self, idx: usize) -> usize {
Expand All @@ -57,8 +81,28 @@ where
}

fn smallest_child_idx(&self, idx: usize) -> usize {
//TODO
0
let smallest_child = if self.children_present(idx) {
let left_child_idx = self.left_child_idx(idx);
let right_child_idx = self.right_child_idx(idx);
if right_child_idx <= self.count
&& (self.comparator)(&self.items[left_child_idx], &self.items[right_child_idx])
{
left_child_idx
} else {
right_child_idx
}
} else {
idx
};
smallest_child
}
fn show(&self)
where
T: Display + Debug,
{
for element in &self.items {
println!("{}", element);
}
}
}

Expand All @@ -79,13 +123,24 @@ where

impl<T> Iterator for Heap<T>
where
T: Default,
T: Default + Copy,
{
type Item = T;

fn next(&mut self) -> Option<T> {
//TODO
None
println!("cur:{}", self.current);
if self.count == 0 {
return None;
}
let val = self.items[self.current];
if self.count % 2 == 0 {
self.current += 1;
} else {
self.current -= 1;
self.current *= 2;
}

return Some(val);
}
}

Expand Down Expand Up @@ -122,18 +177,32 @@ mod tests {
assert_eq!(heap.next(), None);
}

#[test]
fn test_heap_with_one_element() {
let mut heap = MaxHeap::new();
heap.add(4);
for i in heap.items.iter() {
println!("{}", i);
}
assert_eq!(heap.len(), 1);
assert_eq!(heap.next(), Some(4));
}

#[test]
fn test_min_heap() {
let mut heap = MinHeap::new();
heap.add(4);
heap.add(2);
heap.add(9);
heap.add(11);
heap.show();

assert_eq!(heap.len(), 4);
assert_eq!(heap.next(), Some(2));
assert_eq!(heap.next(), Some(4));
assert_eq!(heap.next(), Some(9));
heap.add(1);
heap.show();
assert_eq!(heap.next(), Some(1));
}

Expand All @@ -144,11 +213,13 @@ mod tests {
heap.add(2);
heap.add(9);
heap.add(11);
heap.show();
assert_eq!(heap.len(), 4);
assert_eq!(heap.next(), Some(11));
assert_eq!(heap.next(), Some(9));
assert_eq!(heap.next(), Some(4));
heap.add(1);

assert_eq!(heap.next(), Some(2));
}
}
}

0 comments on commit 001335c

Please sign in to comment.