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

add avl tree and heap part cpp code #320

Merged
merged 7 commits into from
Feb 4, 2023
Merged

add avl tree and heap part cpp code #320

merged 7 commits into from
Feb 4, 2023

Conversation

what-is-me
Copy link
Contributor

  • the code was tested.
  • most of them were translated from Java version.
  • heap.cpp wasn't translated well, but I have no better idea.

@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 7:52AM (UTC)

docs/chapter_heap/heap.md Outdated Show resolved Hide resolved
@@ -255,7 +297,23 @@ comments: true
=== "C++"

```cpp title="my_heap.cpp"
// 使用vector而非数组,这样无需考虑扩容问题
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 vector 而非数组

Add a space between English char and Chinese char.

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Please address the comments.

@krahets
Copy link
Owner

krahets commented Feb 4, 2023

Could you help paste the console output to this PR?

@what-is-me
Copy link
Contributor Author

avl_tree.cpp

插入结点 1 后,AVL 树为
——— 1

插入结点 2 后,AVL 树为
    /——— 2
——— 1

插入结点 3 后,AVL 树为
    /——— 3
——— 2
    \——— 1

插入结点 4 后,AVL 树为
        /——— 4
    /——— 3
——— 2
    \——— 1

插入结点 5 后,AVL 树为
        /——— 5
    /——— 4
   |    \——— 3
——— 2
    \——— 1

插入结点 8 后,AVL 树为
        /——— 8
    /——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

插入结点 7 后,AVL 树为
        /——— 8
    /——— 7
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

插入结点 9 后,AVL 树为
            /——— 9
        /——— 8
    /——— 7
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

插入结点 10 后,AVL 树为
            /——— 10
        /——— 9
       |    \——— 8
    /——— 7
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

插入结点 6 后,AVL 树为
            /——— 10
        /——— 9
       |    \——— 8
    /——— 7
   |   |    /——— 6
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

插入结点 7 后,AVL 树为
            /——— 10
        /——— 9
       |    \——— 8
    /——— 7
   |   |    /——— 6
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

删除结点 8 后,AVL 树为
            /——— 10
        /——— 9
    /——— 7
   |   |    /——— 6
   |    \——— 5
——— 4
   |    /——— 3
    \——— 2
        \——— 1

删除结点 5 后,AVL 树为
            /——— 10
        /——— 9
    /——— 7
   |    \——— 6
——— 4
   |    /——— 3
    \——— 2
        \——— 1

删除结点 4 后,AVL 树为
        /——— 10
    /——— 9
   |    \——— 7
——— 6
   |    /——— 3
    \——— 2
        \——— 1

查找到的结点对象为 0x2aae3d73c60,结点值 = 7

heap.cpp

以下测试样例为大顶堆

元素 1 入堆后
堆的数组表示:
[1]
堆的树状表示:
——— 1

元素 3 入堆后
堆的数组表示:
[3, 1]
堆的树状表示:
——— 3
    \——— 1

元素 2 入堆后
堆的数组表示:
[3, 1, 2]
堆的树状表示:
    /——— 2
——— 3
    \——— 1

元素 5 入堆后
堆的数组表示:
[5, 3, 2, 1]
堆的树状表示:
    /——— 2
——— 5
    \——— 3
        \——— 1

元素 4 入堆后
堆的数组表示:
[5, 4, 2, 1, 3]
堆的树状表示:
    /——— 2
——— 5
   |    /——— 3
    \——— 4
        \——— 1

堆顶元素为 5

元素 5 出堆后
堆的数组表示:
[4, 3, 2, 1]
堆的树状表示:
    /——— 2
——— 4
    \——— 3
        \——— 1

元素 4 出堆后
堆的数组表示:
[3, 1, 2]
堆的树状表示:
    /——— 2
——— 3
    \——— 1

元素 3 出堆后
堆的数组表示:
[2, 1]
堆的树状表示:
——— 2
    \——— 1

元素 2 出堆后
堆的数组表示:
[1]
堆的树状表示:
——— 1

元素 1 出堆后
堆的数组表示:
[]
堆的树状表示:

堆元素数量为 0

堆是否为空 1

输入列表并建立小顶堆后
堆的数组表示:
[1, 3, 2, 5, 4]
堆的树状表示:
    /——— 2
——— 1
   |    /——— 4
    \——— 3
        \——— 5

myheap.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, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2, 7]
堆的树状表示:
        /——— 2
    /——— 6
   |   |    /——— 7
   |    \——— 5
   |        \——— 2
——— 9
   |        /——— 6
   |    /——— 7
   |   |    \——— 3
    \——— 8
       |    /——— 4
        \——— 6
            \——— 1

堆顶元素 9 出堆后
堆的数组表示:
[8, 7, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]
堆的树状表示:
        /——— 2
    /——— 6
   |    \——— 5
   |        \——— 2
——— 8
   |        /——— 6
   |    /——— 7
   |   |    \——— 3
    \——— 7
       |    /——— 4
        \——— 6
            \——— 1

堆元素数量为 12

堆是否为空 0

I will modify my commit soon.

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@krahets krahets merged commit e5ae3e1 into krahets:master Feb 4, 2023
@krahets krahets added the code Code-related label Feb 4, 2023
krahets added a commit that referenced this pull request Feb 4, 2023
@krahets
Copy link
Owner

krahets commented Feb 4, 2023

Hi! I found some mistakes in the my_heap.cpp. Please check the latest code in #321

@what-is-me
Copy link
Contributor Author

Sorry, I just copied all the name of class and function from the Java code.

@krahets
Copy link
Owner

krahets commented Feb 4, 2023

Except for the naming, the code worked incorrectly.

For example, this result is not correct.

元素 7 入堆后
堆的数组表示:
[9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2, 7]
堆的树状表示:
        /——— 2
    /——— 6
   |   |    /——— 7
   |    \——— 5
   |        \——— 2
——— 9
   |        /——— 6
   |    /——— 7
   |   |    \——— 3
    \——— 8
       |    /——— 4
        \——— 6
            \——— 1

@what-is-me
Copy link
Contributor Author

The bug caused by the function swap?
Sorry I didn't paid attention to it.
I thought the swap should match the class method.

@what-is-me
Copy link
Contributor Author

I'm so sorry I made something wrong in avl_tree.md.
I ignored that my code editor wrongly formated the markdown file and now the page went wrong.
I fixed it in my new rp #323

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.

2 participants