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

Part Heap : Code Translation into C++ (heap.cpp)

Open longranger2 opened this issue 2 years ago • 2 comments

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).

我已经把heap章节C++版本关于heap的相关操作实现了,文件为heap.cpp,同时在PrintUtil.hpp添加了printHeap函数来实现打印heap的功能 ,相关输出如下:

以下测试样例为大顶堆
元素 1 入堆后
堆的数组表示:
[1]
堆的树状表示:
——— 1
元素 3 入堆后
堆的数组表示:
[3, 1]
堆的树状表示:
——— 3
    \——— 1
元素 2 入堆后
堆的数组表示:
[3, 2, 1]
堆的树状表示:
    /——— 1
——— 3
    \——— 2
元素 5 入堆后
堆的数组表示:
[5, 3, 2, 1]
堆的树状表示:
    /——— 2
——— 5
    \——— 3
        \——— 1
元素 4 入堆后
堆的数组表示:
[5, 4, 3, 2, 1]
堆的树状表示:
    /——— 3
——— 5
   |    /——— 1
    \——— 4
        \——— 2
堆顶元素为: 5
堆顶元素 5 出堆后
堆的数组表示:
[4, 3, 2, 1]
堆的树状表示:
    /——— 2
——— 4
    \——— 3
        \——— 1
堆顶元素 4 出堆后
堆的数组表示:
[3, 2, 1]
堆的树状表示:
    /——— 1
——— 3
    \——— 2
堆顶元素 3 出堆后
堆的数组表示:
[2, 1]
堆的树状表示:
——— 2
    \——— 1
堆顶元素 2 出堆后
堆的数组表示:
[1]
堆的树状表示:
——— 1
堆顶元素 1 出堆后
堆的数组表示:
[]
堆的树状表示:
堆元素数量为: 0
堆是否为空: 1
输入列表并建立小顶堆后
堆的数组表示:
[1, 2, 3, 4, 5]
堆的树状表示:
    /——— 3
——— 1
   |    /——— 5
    \——— 2
        \——— 4

效果如下: image image

longranger2 avatar Jan 19 '23 09:01 longranger2

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 6:23AM (UTC)

vercel[bot] avatar Jan 19 '23 09:01 vercel[bot]

my_heap.cpp将会在这几天搞定

longranger2 avatar Jan 19 '23 09:01 longranger2

Could you help implement my_heap.cpp? Thanks! @longranger2

krahets avatar Feb 04 '23 06:02 krahets

Sure! Actually I've finished it in the morning, but I tried to submit to this PR only to find that it had been merged haha. And I will submit a new PR.

longranger2 avatar Feb 04 '23 07:02 longranger2