hello-algo icon indicating copy to clipboard operation
hello-algo copied to clipboard

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

Open coderonion opened this issue 2 years ago • 5 comments

If this PR is related to coding or code translation, please fill out the checklist.

  • [x] I've tested the code and ensured the outputs are the same as the outputs of reference codes.
  • [x] I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
  • [x] The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

coderonion avatar Jan 14 '23 14:01 coderonion

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
hello-algo ⬜️ Ignored (Inspect) Jan 15, 2023 at 10:16PM (UTC)

vercel[bot] avatar Jan 14 '23 14:01 vercel[bot]

@krahets The bug( https://github.com/krahets/hello-algo/pull/249#issuecomment-1382698086) in Section 'Heap' (heap.zig) is solved.

@krahets Now, the codes of this PR were updated. Now, it already supports TailQueue, and also support ArrayList to work as queue, please see "arrListToTree" and "arrQueToTree" function in "zig/include/TreeNode.zig" file for more details. The output of "zig/chapter_heap/heap.zig" using TailQueue is the same as using ArrayList to work as queue. The readers can select one of the options trough "queue_flag" parameter in "printHeap" function in "zig/include/PrintUtil.zig" file, and both options can output the same result.

The output of "zig/chapter_heap/heap.zig" is shown below, 01 02

coderonion avatar Jan 14 '23 14:01 coderonion

Please address the comments. Thanks!

@krahets Now, the codes of this PR were updated. Now, it already supports TailQueue, and also support ArrayList to work as queue, please see "arrListToTree" and "arrQueToTree" function in "zig/include/TreeNode.zig" file for more details. The output of "zig/chapter_heap/heap.zig" using TailQueue is the same as using ArrayList to work as queue. The readers can select one of the options trough "queue_flag" parameter in "printHeap" function in "zig/include/PrintUtil.zig" file, and both options can output the same result.

coderonion avatar Jan 15 '23 12:01 coderonion

Please address the comments. Thanks!

@krahets Now, the codes of this PR were updated. Now, it already supports TailQueue, and also support ArrayList to work as queue, please see "arrListToTree" and "arrQueToTree" function in "zig/include/TreeNode.zig" file for more details. The output of "zig/chapter_heap/heap.zig" using TailQueue is the same as using ArrayList to work as queue. The readers can select one of the options trough "queue_flag" parameter in "printHeap" function in "zig/include/PrintUtil.zig" file, and both options can output the same result.

Thanks for the hard work. But I meant that you should replace the arraylist with the queue, not keep both of them. Because we execute a breadth-first search in the arrToTree(), in which Queue is the best choice to implement "First In First Out".

By the way, which language do you select as the reference?

krahets avatar Jan 15 '23 15:01 krahets

Please address the comments. Thanks!

@krahets Now, the codes of this PR were updated. Now, it already supports TailQueue, and also support ArrayList to work as queue, please see "arrListToTree" and "arrQueToTree" function in "zig/include/TreeNode.zig" file for more details. The output of "zig/chapter_heap/heap.zig" using TailQueue is the same as using ArrayList to work as queue. The readers can select one of the options trough "queue_flag" parameter in "printHeap" function in "zig/include/PrintUtil.zig" file, and both options can output the same result.

Thanks for the hard work. But I meant that you should replace the arraylist with the queue, not keep both of them. Because we execute a breadth-first search in the arrToTree(), in which Queue is the best choice to implement "First In First Out".

By the way, which language do you select as the reference?

  1. OK, i have deleted the 'arraylist' in the latest commit. Now we can just use TailQueue.
  2. Java language codes are as the reference. Thanks @krahets ([email protected]).
  3. The output of the latest commit is shown below, 01 02

coderonion avatar Jan 15 '23 16:01 coderonion

Sorry that I have to say the codes are a little bit hard to review.

Before translation, you should ensure that you know how the reference codes work and behave. Moreover, you'd be better to check the codes more carefully and make them as clean, and consistent with the reference code as possible.

The codes are written for a book, thus the normative and simplicity are important. We do not want to convey wrong programming habits and content to our readers. So the update frequency is not vital, first of all, we should ensure the correctness and quality of the content.

I don't code in zig, so there likely exist mistakes in the previous PR. I hope that you can check all the zig codes and polish them as well as possible.

Thanks for the contribution.

It's my pleasure to contribute zig codes for this project.

  1. First, all the zig codes refer to java, cpp and csharp codes.
  2. Second, all the zig codes refer to the zig official style guide.

You know zig is a new language, these exist some differences with other languages in style or idea. I will do my best to check and refine these codes.

coderonion avatar Jan 15 '23 22:01 coderonion