MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / build_new

Method build_new

code/chapter07/binary_heap.rs:73–92  ·  view source on GitHub ↗

构建新堆

(&mut self, arr: &[i32])

Source from the content-addressed store, hash-verified

71
72 // 构建新堆
73 fn build_new(&mut self, arr: &[i32]) {
74 // 删除原始数据
75 for _i in 0..self.size {
76 let _rm = self.data.pop();
77 }
78
79 // 添加新数据
80 for &val in arr {
81 self.data.push(val);
82 }
83 self.size = arr.len();
84
85 // 调整堆,使其为小顶堆
86 let size = self.size;
87 let mut p = parent!(size);
88 while p > 0 {
89 self.move_down(p);
90 p -= 1;
91 }
92 }
93
94 fn pop(&mut self) -> Option<i32> {
95 if 0 == self.size { // 没数据,返回 None

Callers 1

mainFunction · 0.45

Calls 4

popMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45
move_downMethod · 0.45

Tested by

no test coverage detected