Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part Heap : Code Translation into C++ (my_heap.cpp) #321

Merged
merged 11 commits into from
Feb 4, 2023

Conversation

longranger2
Copy link
Contributor

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

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

我已经完成了my_heap.cpp,实现效果如下:

输入数组并建堆后
堆的数组表示:
[9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]
堆的树状表示:
        /——— 2
    /——— 6
   |    \——— 5
   |        \——— 2
——— 9
   |        /——— 6
   |    /——— 7
   |   |    \——— 3
    \——— 8
       |    /——— 4
        \——— 6
            \——— 1
堆顶元素为 9
元素 7 入堆后
堆的数组表示:
[9, 8, 7, 6, 7, 6, 2, 1, 4, 3, 6, 2, 5]
堆的树状表示:
        /——— 2
    /——— 7
   |   |    /——— 5
   |    \——— 6
   |        \——— 2
——— 9
   |        /——— 6
   |    /——— 7
   |   |    \——— 3
    \——— 8
       |    /——— 4
        \——— 6
            \——— 1
堆顶元素 7 出堆后
堆的数组表示:
[8, 7, 7, 6, 6, 6, 2, 1, 4, 3, 5, 2]
堆的树状表示:
        /——— 2
    /——— 7
   |    \——— 6
   |        \——— 2
——— 8
   |        /——— 5
   |    /——— 6
   |   |    \——— 3
    \——— 7
       |    /——— 4
        \——— 6
            \——— 1
堆顶元素数量为 12
堆是否为空 0

image

@vercel
Copy link

vercel bot commented Feb 4, 2023

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

1 Ignored Deployment
Name Status Preview Comments Updated
hello-algo ⬜️ Ignored (Inspect) Feb 4, 2023 at 8:33AM (UTC)

@longranger2
Copy link
Contributor Author

I want to ask a question: the name of function need to be aligned with the java implementation? The name of function in my code is aligned with the C++ priority_queue
image

@krahets krahets merged commit 0b16a9f into krahets:master Feb 4, 2023
@krahets krahets added the code Code-related label Feb 4, 2023
@krahets
Copy link
Owner

krahets commented Feb 4, 2023

I want to ask a question: the name of function need to be aligned with the java implementation? The name of function in my code is aligned with the C++ priority_queue image

We follow the naming style of Java. Because the function name is likely to be shown in the articles, we want to make all the names consistent.

@krahets
Copy link
Owner

krahets commented Feb 4, 2023

I fine-tuned the codes and comments. Please check it.

Thanks for the PR!

@longranger2
Copy link
Contributor Author

Got it. I will pay attention next time I contribute a PR 😄

@what-is-me
Copy link
Contributor

Hello, I think you should add
freeMemoryTree(root);
to the end of the method MaxHeap::print() in line 116 in my_heap.cpp
and also to the function printHeap()
or there will be a little memory leak.

@longranger2
Copy link
Contributor Author

Hello, I think you should add freeMemoryTree(root); to the end of the method MaxHeap::print() in line 116 in my_heap.cpp and also to the function printHeap() or there will be a little memory leak.

Great! Thank you for your advice. I will fix it later.

@what-is-me
Copy link
Contributor

Another suggestion:
I saw you write

// 判空处理
if (empty()) {
    cout << "Error:堆为空" << endl;
    return;
}

to deal with heap_empty error.
However, in java or other language, when the error occurred, the procedure may exit.
Maybe a better way is to use std::exception like

// 判空处理
if (empty()) throw out_of_range("堆为空");

@longranger2
Copy link
Contributor Author

Another suggestion: I saw you write

// 判空处理
if (empty()) {
    cout << "Error:堆为空" << endl;
    return;
}

to deal with heap_empty error. However, in java or other language, when the error occurred, the procedure may exit. Maybe a better way is to use std::exception like

// 判空处理
if (empty()) throw out_of_range("堆为空");

Thank you for your advice! I will fix it later!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants