-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
177 lines (147 loc) · 4.56 KB
/
main.rs
File metadata and controls
177 lines (147 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::collections::VecDeque;
fn main() {
println!("\n*** Chapter 16 ***\n");
let mut h: Heap<i32> = Heap::new();
h.insert(10);
h.insert(15);
h.insert(25);
h.insert(8);
h.insert(20);
dbg!(&h);
println!("root_node: {:?}", h.root_node());
println!("last_node: {:?}\n", h.last_node());
h.delete();
dbg!(&h);
println!("root_node: {:?}", h.root_node());
println!("last_node: {:?}", h.last_node());
}
#[derive(Debug)]
struct Heap<T> {
data: VecDeque<T>,
}
impl<T: PartialOrd> Heap<T> {
fn new() -> Self {
Self {
data: VecDeque::new(),
}
}
fn root_node(&self) -> Option<&T> {
self.data.front()
}
fn last_node(&self) -> Option<&T> {
self.data.back()
}
fn insert(&mut self, value: T) {
self.data.push_back(value);
let mut node_idx = self.data.len() - 1;
// When node_idx is 0, parent_index returns None.
while let Some(parent_idx) = Self::parent_index(node_idx) {
if self.data[node_idx] > self.data[parent_idx] {
self.data.swap(node_idx, parent_idx);
node_idx = parent_idx;
} else {
break;
}
}
}
fn delete(&mut self) {
if let Some(last) = self.data.pop_back() {
if !self.data.is_empty() {
self.data[0] = last;
} else {
return;
}
} else {
return;
}
let mut trickle_idx = 0;
while let Some(greater_idx) = self.greater_child_index(trickle_idx) {
self.data.swap(trickle_idx, greater_idx);
trickle_idx = greater_idx;
}
}
fn greater_child_index(&self, i: usize) -> Option<usize> {
let val = &self.data.get(i)?;
let left_idx = Self::left_child_index(i)?;
let right_idx = Self::right_child_index(i)?;
let left_val = &self.data.get(left_idx);
let right_val = &self.data.get(right_idx);
match (left_val, right_val) {
(Some(l), Some(r)) if r > l && r > val => Some(right_idx),
(Some(l), _) if l > val => Some(left_idx),
_ => None,
}
}
fn left_child_index(i: usize) -> Option<usize> {
i.checked_mul(2)?.checked_add(1)
}
fn right_child_index(i: usize) -> Option<usize> {
i.checked_mul(2)?.checked_add(2)
}
fn parent_index(i: usize) -> Option<usize> {
i.checked_sub(1)?.checked_div(2)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_heap() {
let mut heap = Heap::new();
heap.insert("X");
heap.insert("Y");
heap.insert("Z");
// heap now contains Z X Y
assert_eq!(heap.root_node().unwrap(), &"Z");
assert_eq!(heap.last_node().unwrap(), &"Y");
let mut heap = Heap::new();
heap.insert(1);
heap.insert(2);
heap.insert(3);
heap.insert(4);
heap.insert(5);
// heap now contains 5 4 2 1 3
assert_eq!(heap.root_node().unwrap(), &5);
assert_eq!(heap.last_node().unwrap(), &3);
}
#[test]
fn test_delete() {
let mut heap = Heap::<i32>::new();
heap.delete();
assert!(heap.data.is_empty());
heap.insert(1);
heap.delete();
assert!(heap.data.is_empty());
heap.insert(1);
heap.insert(2);
heap.delete();
assert_eq!(heap.root_node().unwrap(), &1);
assert_eq!(heap.last_node().unwrap(), &1);
heap.insert(2);
heap.insert(3);
// heap now contains 3 1 2
heap.delete();
assert_eq!(heap.root_node().unwrap(), &2);
assert_eq!(heap.last_node().unwrap(), &1);
}
#[test]
fn test_left_child_index() {
assert_eq!(Heap::<i8>::left_child_index(0).unwrap(), 1);
assert_eq!(Heap::<i8>::left_child_index(4).unwrap(), 9);
assert_eq!(Heap::<i8>::left_child_index(5).unwrap(), 11);
}
#[test]
fn test_right_child_index() {
assert_eq!(Heap::<i8>::right_child_index(0).unwrap(), 2);
assert_eq!(Heap::<i8>::right_child_index(4).unwrap(), 10);
assert_eq!(Heap::<i8>::right_child_index(5).unwrap(), 12);
}
#[test]
fn test_parent_index() {
assert_eq!(Heap::<i8>::parent_index(1).unwrap(), 0);
assert_eq!(Heap::<i8>::parent_index(2).unwrap(), 0);
assert_eq!(Heap::<i8>::parent_index(4).unwrap(), 1);
assert_eq!(Heap::<i8>::parent_index(9).unwrap(), 4);
assert_eq!(Heap::<i8>::parent_index(10).unwrap(), 4);
}
}