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

heaps.py supports list, tuple and pydatastructs.Array #218

Merged
merged 5 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pydatastructs/trees/heaps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydatastructs.utils.misc_util import _check_type, NoneType, TreeNode, BinomialTreeNode
from pydatastructs.linear_data_structures.arrays import (ArrayForTrees,
DynamicOneDimensionalArray)
DynamicOneDimensionalArray, Array)
from pydatastructs.miscellaneous_data_structures.binomial_trees import BinomialTree

__all__ = [
Expand All @@ -24,7 +24,7 @@ class DHeap(Heap):
Parameters
==========

elements : list, tuple
elements : list, tuple, Array
Optional, by default 'None'.
List/tuple of initial TreeNode in Heap.

Expand Down Expand Up @@ -84,9 +84,15 @@ def __new__(cls, elements=None, heap_property="min", d=4):
raise ValueError("%s is invalid heap property"%(heap_property))
if elements is None:
elements = DynamicOneDimensionalArray(TreeNode, 0)
else:
elif type(elements) in (list,tuple):
if not all(map(lambda x: _check_type(x, TreeNode), elements)):
raise ValueError("Expect a list/tuple of TreeNode got %s"%(elements))
raise ValueError("Expected a list/tuple/Array of TreeNode got %s"%(elements))
elements = DynamicOneDimensionalArray(TreeNode, len(elements), elements)
elif _check_type(elements, Array):
if(not _check_type(elements[0], TreeNode)):
raise ValueError(f'Expected a list/tuple/Array of TreeNode got {type(elements[0])}')
else:
raise ValueError(f'Expected a list/tuple/Array of TreeNode got {type(elements)}')
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
obj.heap = elements
obj._last_pos_filled = obj.heap._last_pos_filled
obj._build()
Expand Down Expand Up @@ -326,7 +332,7 @@ class BinomialHeap(Heap):
Parameters
==========

root_list: list/tuple
root_list: list/tuple/Array
By default, []
The list of BinomialTree object references
in sorted order.
Expand Down
15 changes: 13 additions & 2 deletions pydatastructs/trees/tests/test_heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_BinaryHeap():
TreeNode(1, 1), TreeNode(2, 2), TreeNode(3, 3),
TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36)
]
min_heap = BinaryHeap(elements=DynamicOneDimensionalArray(TreeNode, 9, elements), heap_property="min")
min_heap = BinaryHeap(elements=elements, heap_property="min")
assert min_heap.extract().key == 1

expected_sorted_elements = [2, 3, 7, 17, 19, 25, 36, 100]
Expand All @@ -55,6 +55,17 @@ def test_BinaryHeap():
]
assert raises(ValueError, lambda:
BinaryHeap(elements = non_TreeNode_elements, heap_property='min'))

non_TreeNode_elements = DynamicOneDimensionalArray(int, 0)
non_TreeNode_elements.append(1)
non_TreeNode_elements.append(2)
assert raises(ValueError, lambda:
BinaryHeap(elements = non_TreeNode_elements, heap_property='min'))

non_heapable = "[1, 2, 3]"
assert raises(ValueError, lambda:
BinaryHeap(elements = non_heapable, heap_property='min'))

def test_TernaryHeap():
max_heap = TernaryHeap(heap_property="max")
assert raises(IndexError, lambda: max_heap.extract())
Expand Down Expand Up @@ -86,7 +97,7 @@ def test_TernaryHeap():
TreeNode(1, 1), TreeNode(2, 2), TreeNode(3, 3),
TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36)
]
min_heap = TernaryHeap(elements=DynamicOneDimensionalArray(TreeNode, 9, elements), heap_property="min")
min_heap = TernaryHeap(elements=elements, heap_property="min")
expected_extracted_element = min_heap.heap[0].key
assert min_heap.extract().key == expected_extracted_element

Expand Down