Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Heaps #155

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

Merged
TheSTL merged 3 commits into master from heaps
Nov 4, 2019
Merged

Heaps #155

TheSTL merged 3 commits into master from heaps
Nov 4, 2019

Conversation

Copy link
Member

@ashokdey ashokdey commented Nov 3, 2019

🏖 What's Inside?

  • Implementation of MaxHeap
  • Unit Tests with coverage of almost 95%

Jest Coverage

if (this.heap.length > 1) {
// move the leaf to the root
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.splice(this.heap.length - 1, 1);
Copy link
Member

Choose a reason for hiding this comment

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

Splice worst case should be O(n) . Instead of splice , pop will work in O(1).

return max;
}

return max;
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this line because it will never execute and it will increase code coverage.

Copy link
Member Author

@ashokdey ashokdey Nov 4, 2019

Choose a reason for hiding this comment

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

ok

}

if (this.heap.length === 1) {
this.heap.splice(this.heap.length - 1, 1);
Copy link
Member

Choose a reason for hiding this comment

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

splice will work here in O(1) because heap size is 1 but you can use pop.

ashokdey reacted with thumbs up emoji
Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

Changes required:

  • Use pop instead of splice.

  • To increase code coverage you can add this test case
    `
    [1, 34, 43, 54, 123].forEach(el => mh.add(el));

    mh.remove();
    mh.remove();
    mh.remove();
    mh.remove();
    mh.remove();
    mh.remove();
    expect(mh.getMax()).toEqual(null);
    `
    because it will covered, uncovered line 42, and uncovered branch 19,30.

Code coverage will be 100% after this.

ashokdey reacted with hooray emoji
Copy link
Member Author

ashokdey commented Nov 4, 2019

Changes required:

  • Use pop instead of splice.
  • To increase code coverage you can add this test case
    [1, 34, 43, 54, 123].forEach(el => mh.add(el)); mh.remove(); mh.remove(); mh.remove(); mh.remove(); mh.remove(); mh.remove(); expect(mh.getMax()).toEqual(null);
    because it will covered, uncovered line 42, and uncovered branch 19,30.

Code coverage will be 100% after this.

Why not you add this after merging this branch? ☺️

Copy link
Member

TheSTL commented Nov 4, 2019

@TheSTL TheSTL merged commit a7956bd into master Nov 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@TheSTL TheSTL TheSTL requested changes

Labels
enhancement New feature or request new-data-structure
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /