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

Removal of Elements leaving duplicate elements #2

Open
nfrankeln opened this issue May 1, 2023 · 0 comments
Open

Removal of Elements leaving duplicate elements #2

nfrankeln opened this issue May 1, 2023 · 0 comments

Comments

@nfrankeln
Copy link

The current removal method doesn't properly remove an element from the storage array.
Example if the heap is [ 0, 1, 5, 15, 45, 14, 10, 34 ]
calling removeMin results in [1, 15, 5, 34, 45, 14, 10, 34]
I fixed this by adding this.storage.pop() to the remove min method so that it correctly returns [1, 15, 5, 34, 45, 14, 10]

new removeMin code

removeMin(){
       if(this.size === 0){
           throw new Error("Empty Heap")
       }
       let data = this.storage[0]
       this.storage[0] = this.storage[this.size -1]
       this.storage.pop()
       this.size -= 1
       this.heapifyDown(0)
       return data
   }

Can I make a pull request with these changes?

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

No branches or pull requests

1 participant