Skip to content

TreeNode check added for heap.py #135

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 14 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ __pycache__/
.idea/
build/
dist/
venv/
6 changes: 5 additions & 1 deletion pydatastructs/trees/heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class DHeap(Heap):

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


heap_property : str
If the key stored in each node is
Expand Down Expand Up @@ -83,6 +84,9 @@ 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:
if not all(map(lambda x: _check_type(x, TreeNode), elements)):
raise ValueError("Expect a list/tuple of TreeNode got %s"%(elements))
obj.heap = elements
obj._last_pos_filled = obj.heap._last_pos_filled
obj._build()
Expand Down
7 changes: 7 additions & 0 deletions pydatastructs/trees/tests/test_heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def test_BinaryHeap():
sorted_elements = [min_heap.extract().key for _ in range(8)]
assert expected_sorted_elements == sorted_elements

non_TreeNode_elements = [
(7, 7), TreeNode(25, 25), TreeNode(100, 100),
TreeNode(1, 1), (2, 2), TreeNode(3, 3),
TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36)
]
assert raises(ValueError, lambda:
BinaryHeap(elements = non_TreeNode_elements, heap_property='min'))
def test_TernaryHeap():
max_heap = TernaryHeap(heap_property="max")
assert max_heap.extract() is None
Expand Down