diff --git a/.gitignore b/.gitignore index 32018e91e..f156e5656 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,4 @@ __pycache__/ .idea/ build/ dist/ +venv/ diff --git a/pydatastructs/trees/heaps.py b/pydatastructs/trees/heaps.py index 3fef1a642..3aeda0f74 100644 --- a/pydatastructs/trees/heaps.py +++ b/pydatastructs/trees/heaps.py @@ -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 @@ -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() diff --git a/pydatastructs/trees/tests/test_heaps.py b/pydatastructs/trees/tests/test_heaps.py index 940620522..3d1ae04d1 100644 --- a/pydatastructs/trees/tests/test_heaps.py +++ b/pydatastructs/trees/tests/test_heaps.py @@ -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