codezonediitj / pydatastructs

A python package for data structures and algorithms
https://pydatastructs.readthedocs.io/en/stable/
Other
201 stars 269 forks source link

Add binary heap #38

Closed czgdp1807 closed 4 years ago

czgdp1807 commented 4 years ago

Description of the problem

A new file is to be created under the trees directory named, heaps.py and in that implementation of binary heap is to be added. Note that before making a PR first propose an API and class structure below. Once finalized, then move on to make a PR.

Example of the problem

References/Other comments

https://en.m.wikipedia.org/wiki/Binary_heap

czgdp1807 commented 4 years ago

Passing an array of n inputs to the element is a good idea for implementing this. Heap operations only consist of insert and extract, so I think we should implement only those methods for the public interface. We can create a private method for _max_heapify for max-heap and _min_heapify for min-heap. This suggests the following classes and their methods,

  1. BinaryHeap - Abstract class containing __new__, insert and extract methods.
  2. MaxHeap - Containing the _heapify and inherits BinaryHeap.
  3. MinHeap - Containing the _heapify and inherits BinaryHeap.

Moreover, __new__ of BinaryHeap can look like,

def __new__(elements):
    ...

What do you say @23umesh ?

23umesh commented 4 years ago

I am working on it, but as i am now working on C++ so it will take some time to create a final file and also your help. As for elements, maxsize,OneDimensinalArray(from pydatastructs) or a python list and type(min/max) would do the work. Or something else needed?

czgdp1807 commented 4 years ago

Please code the binary heap in Python 3.5.

23umesh commented 4 years ago

I am working on the coding part using a python list. Should I do this part using OneDimensionalArray or DynamicOneDimensionalArray.Can you add the KWoC label to this issue?

czgdp1807 commented 4 years ago

Fixed by #46