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

Method build_new

publication/code/chapter08/binary_heap.rs:69–88  ·  view source on GitHub ↗

构建新堆

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

Source from the content-addressed store, hash-verified

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

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