update zig codes for Section 'Heap' (heap.zig)

This commit is contained in:
sjinzh 2023-01-16 00:05:56 +08:00
parent d8289580a5
commit 73121c2cb3
3 changed files with 5 additions and 39 deletions

View file

@ -18,13 +18,13 @@ fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap_push: anyty
var heap = heap_push;
try heap.add(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 {
var val = heap_pop.remove(); //
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
@ -73,10 +73,9 @@ pub fn main() !void {
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
//
// O(n) O(nlogn)
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
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();
_ = getchar;

View file

@ -59,16 +59,13 @@ pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHas
}
// print a heap (PriorityQueue)
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype, queue_flag: bool) !void {
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !void {
var arr = queue.items;
var len = queue.len;
std.debug.print("堆的数组表示:", .{});
printArray(T, arr[0..len]);
std.debug.print("\n堆的树状表示:\n", .{});
var root = if (queue_flag)
try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]) // through TailQueue
else
try TreeUtil.arrListToTree(T, mem_allocator, arr[0..len]); // through ArrayList to work as queue
var root = try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]); // through TailQueue
try printTree(root, null, false);
}

View file

@ -23,36 +23,6 @@ pub fn TreeNode(comptime T: type) type {
};
}
// Generate a binary tree with an array (through ArrayList to work as queue)
pub fn arrListToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
if (arr.len == 0) return null;
var root = try mem_allocator.create(TreeNode(T));
root.init(arr[0]);
var list = std.ArrayList(*TreeNode(T)).init(std.heap.page_allocator);
try list.append(root);
var index: usize = 0;
while (list.items.len > 0) {
var node = list.orderedRemove(0);
index += 1;
if (index >= arr.len) break;
if (index < arr.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.left = tmp;
try list.append(node.left.?);
}
index += 1;
if (index >= arr.len) break;
if (index < arr.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.right = tmp;
try list.append(node.right.?);
}
}
return root;
}
// Generate a binary tree with an array (through TailQueue)
pub fn arrQueToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
if (arr.len == 0) return null;