|
|
@ -18,13 +18,13 @@ fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap_push: anyty
|
|
|
|
var heap = heap_push;
|
|
|
|
var heap = heap_push;
|
|
|
|
try heap.add(val); //元素入堆
|
|
|
|
try heap.add(val); //元素入堆
|
|
|
|
std.debug.print("\n元素 {} 入堆后\n", .{val});
|
|
|
|
std.debug.print("\n元素 {} 入堆后\n", .{val});
|
|
|
|
try inc.PrintUtil.printHeap(T, mem_allocator, heap, true);
|
|
|
|
try inc.PrintUtil.printHeap(T, mem_allocator, heap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn testPop(comptime T: type, mem_allocator: std.mem.Allocator, heap_pop: anytype) !void {
|
|
|
|
fn testPop(comptime T: type, mem_allocator: std.mem.Allocator, heap_pop: anytype) !void {
|
|
|
|
var val = heap_pop.remove(); //堆顶元素出堆
|
|
|
|
var val = heap_pop.remove(); //堆顶元素出堆
|
|
|
|
std.debug.print("\n堆顶元素 {} 出堆后\n", .{val});
|
|
|
|
std.debug.print("\n堆顶元素 {} 出堆后\n", .{val});
|
|
|
|
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop, true);
|
|
|
|
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Driver Code
|
|
|
|
// Driver Code
|
|
|
@ -73,10 +73,9 @@ pub fn main() !void {
|
|
|
|
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
|
|
|
|
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
|
|
|
|
|
|
|
|
|
|
|
|
// 输入列表并建堆
|
|
|
|
// 输入列表并建堆
|
|
|
|
// 时间复杂度为 O(n) ,而非 O(nlogn)
|
|
|
|
|
|
|
|
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
|
|
|
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
|
|
|
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
|
|
|
|
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
|
|
|
|
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap, true);
|
|
|
|
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
|
|
|
|
|
|
|
|
|
|
|
|
const getchar = try std.io.getStdIn().reader().readByte();
|
|
|
|
const getchar = try std.io.getStdIn().reader().readByte();
|
|
|
|
_ = getchar;
|
|
|
|
_ = getchar;
|
|
|
|