Skip to content

Commit 73121c2

Browse files
committed
update zig codes for Section 'Heap' (heap.zig)
1 parent d828958 commit 73121c2

File tree

3 files changed

+5
-39
lines changed

3 files changed

+5
-39
lines changed

codes/zig/chapter_heap/heap.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap_push: anyty
1818
var heap = heap_push;
1919
try heap.add(val); //元素入堆
2020
std.debug.print("\n元素 {} 入堆后\n", .{val});
21-
try inc.PrintUtil.printHeap(T, mem_allocator, heap, true);
21+
try inc.PrintUtil.printHeap(T, mem_allocator, heap);
2222
}
2323

2424
fn testPop(comptime T: type, mem_allocator: std.mem.Allocator, heap_pop: anytype) !void {
2525
var val = heap_pop.remove(); //堆顶元素出堆
2626
std.debug.print("\n堆顶元素 {} 出堆后\n", .{val});
27-
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop, true);
27+
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop);
2828
}
2929

3030
// Driver Code
@@ -73,10 +73,9 @@ pub fn main() !void {
7373
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
7474

7575
// 输入列表并建堆
76-
// 时间复杂度为 O(n) ,而非 O(nlogn)
7776
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
7877
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
79-
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap, true);
78+
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
8079

8180
const getchar = try std.io.getStdIn().reader().readByte();
8281
_ = getchar;

codes/zig/include/PrintUtil.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,13 @@ pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHas
5959
}
6060

6161
// print a heap (PriorityQueue)
62-
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype, queue_flag: bool) !void {
62+
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !void {
6363
var arr = queue.items;
6464
var len = queue.len;
6565
std.debug.print("堆的数组表示:", .{});
6666
printArray(T, arr[0..len]);
6767
std.debug.print("\n堆的树状表示:\n", .{});
68-
var root = if (queue_flag)
69-
try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]) // through TailQueue
70-
else
71-
try TreeUtil.arrListToTree(T, mem_allocator, arr[0..len]); // through ArrayList to work as queue
68+
var root = try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]); // through TailQueue
7269
try printTree(root, null, false);
7370
}
7471

codes/zig/include/TreeNode.zig

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,6 @@ pub fn TreeNode(comptime T: type) type {
2323
};
2424
}
2525

26-
// Generate a binary tree with an array (through ArrayList to work as queue)
27-
pub fn arrListToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
28-
if (arr.len == 0) return null;
29-
var root = try mem_allocator.create(TreeNode(T));
30-
root.init(arr[0]);
31-
var list = std.ArrayList(*TreeNode(T)).init(std.heap.page_allocator);
32-
try list.append(root);
33-
var index: usize = 0;
34-
while (list.items.len > 0) {
35-
var node = list.orderedRemove(0);
36-
index += 1;
37-
if (index >= arr.len) break;
38-
if (index < arr.len) {
39-
var tmp = try mem_allocator.create(TreeNode(T));
40-
tmp.init(arr[index]);
41-
node.left = tmp;
42-
try list.append(node.left.?);
43-
}
44-
index += 1;
45-
if (index >= arr.len) break;
46-
if (index < arr.len) {
47-
var tmp = try mem_allocator.create(TreeNode(T));
48-
tmp.init(arr[index]);
49-
node.right = tmp;
50-
try list.append(node.right.?);
51-
}
52-
}
53-
return root;
54-
}
55-
5626
// Generate a binary tree with an array (through TailQueue)
5727
pub fn arrQueToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
5828
if (arr.len == 0) return null;

0 commit comments

Comments
 (0)