Add randomized heap. by Phil9l · Pull Request #3241 · TheAlgorithms/Python · GitHub
Skip to content

Add randomized heap. - #3241

Merged
cclauss merged 3 commits into
TheAlgorithms:masterfrom
Phil9l:randomized-heap
Oct 14, 2020
Merged

cclauss merged 3 commits into
TheAlgorithms:masterfrom
Phil9l:randomized-heap

Conversation

@Phil9l

@Phil9l Phil9l commented Oct 12, 2020

Copy link
Copy Markdown
Contributor

Add randomized heap data structure

A data structure that allows inserting a new value and to pop the smallest values. Both operations take O(logN) time where N is the size of the structure.
Wiki: https://en.wikipedia.org/wiki/Randomized_meldable_heap

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@Phil9l
Phil9l requested a review from cclauss as a code owner October 12, 2020 21:10
@TravisBuddy

Copy link
Copy Markdown

"""
self._root = None

def to_sorted_list(self) -> List[T]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread data_structures/heap/randomized_heap.py
"""
result = []
while self:
result.append(self.pop())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would surprise me as a user of this class that requesting a list would remove all data from the heap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you suggest to add all the items back to the heap at the end of the method?

@cclauss cclauss Oct 14, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would suggest that you replace .to_sorted_list() with .__iter__() and that method should not use .pop() but should just yield values as it walks thru the heap.

Please search this repo for instances of __intr__() to see how this is done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I mean I know how __iter__ works in python. But we can't iterate over heap to get sorted values. In BST we know where is the next element but in the heap, we can only pop the smallest. We can't get the second element.

I can do it like this since __iter__ can work in time larger than O(1). And we don't need the structure to be thread-safe, we can modify it. So poping and pushing back works.

WDYT?

    def __iter__(self) -> Iterator[T]:
        """
        Returns sorted list containing all the values in the heap.

        >>> sh = RandomizedHeap([3, 1, 3, 7])
        >>> list(sh)
        [1, 3, 3, 7]
        """
        result = []
        while self:
            result.append(self.pop())

        # Pushing items back to the heap not to clear it.
        for item in result:
            self.insert(item)

        return iter(result)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good enough for now. Thanks for doing this!

structure.
Wiki: https://en.wikipedia.org/wiki/Randomized_meldable_heap

>>> RandomizedHeap([2, 3, 1, 5, 1, 7]).to_sorted_list()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change

Should pass if .__iter__() is properly implemented.

@Phil9l Phil9l mentioned this pull request Oct 14, 2020
14 tasks

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well done!!

@cclauss
cclauss merged commit f0aa63f into TheAlgorithms:master Oct 14, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
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

Successfully merging this pull request may close these issues.

3 participants