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 6 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/
8 changes: 7 additions & 1 deletion pydatastructs/trees/heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DHeap(Heap):

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

heap_property : str
If the key stored in each node is
Expand Down Expand Up @@ -83,6 +83,12 @@ 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:
#to check if all elements provided are TreeNodes
check_node = map(lambda x: type(x)==type(TreeNode(1,1)),elements)
is_node = all(check_node)
if(not is_node):
raise TypeError("Elements have to be list/tuple of TreeNode")
obj.heap = elements
obj._last_pos_filled = obj.heap._last_pos_filled
obj._build()
Expand Down
11 changes: 9 additions & 2 deletions pydatastructs/trees/tests/test_heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def test_BinaryHeap():
assert max_heap.extract().key == 100

expected_sorted_elements = [36, 25, 19, 17, 7, 3, 2, 1]
l = max_heap.heap[0].left
l = max_heap.heap[0].right
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might have accidentally removed them. It was showing a warning if I remember correctly

Copy link
Member

Choose a reason for hiding this comment

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

I think the first line can be removed safely, though do it in a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The variable is never being used, so idk whats the point of having it.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, feel free to remove it in a separate PR.

sorted_elements = []
for _ in range(8):
sorted_elements.append(max_heap.extract().key)
Expand All @@ -48,6 +46,15 @@ def test_BinaryHeap():
sorted_elements = [min_heap.extract().key for _ in range(8)]
assert expected_sorted_elements == sorted_elements

#To test ValueError on non TreeNode list
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(TypeError, 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