Skip to content

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
merged 3 commits into from
Nov 4, 2019
Merged

Heaps #155

merged 3 commits into from
Nov 4, 2019

Conversation

ashokdey
Copy link
Member

@ashokdey ashokdey commented Nov 3, 2019

🏖 What's Inside?

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

Jest Coverage

@ashokdey ashokdey added enhancement New feature or request new-data-structure labels Nov 3, 2019
@ashokdey ashokdey requested a review from TheSTL November 3, 2019 06:14
@ashokdey ashokdey self-assigned this Nov 3, 2019
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

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.

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
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? ☺️

@TheSTL
Copy link
Member

TheSTL commented Nov 4, 2019

@ashokdey ok

@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
Labels
enhancement New feature or request new-data-structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants