From 6f21488321602a1806e483368a57c0955772c92c Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Mon, 6 Apr 2020 17:16:17 +0530 Subject: [PATCH 1/4] data is optional in BinaryHeap --- pydatastructs/trees/heaps.py | 5 ++++- pydatastructs/trees/tests/test_heaps.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pydatastructs/trees/heaps.py b/pydatastructs/trees/heaps.py index 4d6eb1ad5..76a1313c4 100644 --- a/pydatastructs/trees/heaps.py +++ b/pydatastructs/trees/heaps.py @@ -129,7 +129,7 @@ def _heapify(self, i): else: break - def insert(self, key, data): + def insert(self, key, data=None): """ Insert a new element to the heap according to heap property. @@ -146,6 +146,9 @@ def insert(self, key, data): None """ + if data is None: + data = key + new_node = TreeNode(key, data) self.heap.append(new_node) self._last_pos_filled += 1 diff --git a/pydatastructs/trees/tests/test_heaps.py b/pydatastructs/trees/tests/test_heaps.py index dece2f132..1d0a4e8ba 100644 --- a/pydatastructs/trees/tests/test_heaps.py +++ b/pydatastructs/trees/tests/test_heaps.py @@ -11,7 +11,7 @@ def test_BinaryHeap(): assert raises(IndexError, lambda: max_heap.extract()) - max_heap.insert(100, 100) + max_heap.insert(100) max_heap.insert(19, 19) max_heap.insert(36, 36) max_heap.insert(17, 17) From e8cdc3fe063d2cc12552bf0bef027b747b0fa6da Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 6 Apr 2020 19:26:20 +0530 Subject: [PATCH 2/4] Update pydatastructs/trees/heaps.py --- pydatastructs/trees/heaps.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pydatastructs/trees/heaps.py b/pydatastructs/trees/heaps.py index 76a1313c4..9b6ed2814 100644 --- a/pydatastructs/trees/heaps.py +++ b/pydatastructs/trees/heaps.py @@ -146,9 +146,6 @@ def insert(self, key, data=None): None """ - if data is None: - data = key - new_node = TreeNode(key, data) self.heap.append(new_node) self._last_pos_filled += 1 From f4a389a3ef984baac13362860c861692f6f98c18 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 6 Apr 2020 19:27:08 +0530 Subject: [PATCH 3/4] Update pydatastructs/trees/tests/test_heaps.py --- pydatastructs/trees/tests/test_heaps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydatastructs/trees/tests/test_heaps.py b/pydatastructs/trees/tests/test_heaps.py index 1d0a4e8ba..dece2f132 100644 --- a/pydatastructs/trees/tests/test_heaps.py +++ b/pydatastructs/trees/tests/test_heaps.py @@ -11,7 +11,7 @@ def test_BinaryHeap(): assert raises(IndexError, lambda: max_heap.extract()) - max_heap.insert(100) + max_heap.insert(100, 100) max_heap.insert(19, 19) max_heap.insert(36, 36) max_heap.insert(17, 17) From 37233168e1d2d615429016907faede70312a39d5 Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Mon, 6 Apr 2020 20:29:29 +0530 Subject: [PATCH 4/4] diff file applied --- pydatastructs/trees/binary_trees.py | 6 +++--- pydatastructs/trees/heaps.py | 2 +- pydatastructs/trees/m_ary_trees.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pydatastructs/trees/binary_trees.py b/pydatastructs/trees/binary_trees.py index e197d63ba..b475e6cab 100644 --- a/pydatastructs/trees/binary_trees.py +++ b/pydatastructs/trees/binary_trees.py @@ -61,7 +61,7 @@ def __new__(cls, key=None, root_data=None, comp=None, obj.is_order_statistic = is_order_statistic return obj - def insert(self, key, data): + def insert(self, key, data=None): """ Inserts data by the passed key using iterative algorithm. @@ -206,7 +206,7 @@ def _update_size(self, start_idx): self.right_size(self.tree[walk]) + 1) walk = self.tree[walk].parent - def insert(self, key, data): + def insert(self, key, data=None): res = self.search(key) if res is not None: self.tree[res].data = data @@ -715,7 +715,7 @@ def _balance_insertion(self, curr, last): path.append(walk), path.append(last) walk = self.tree[walk].parent - def insert(self, key, data): + def insert(self, key, data=None): super(AVLTree, self).insert(key, data) self._balance_insertion(self.size - 1, self.tree[self.size-1].parent) diff --git a/pydatastructs/trees/heaps.py b/pydatastructs/trees/heaps.py index 76a1313c4..1f30cf6e9 100644 --- a/pydatastructs/trees/heaps.py +++ b/pydatastructs/trees/heaps.py @@ -443,7 +443,7 @@ def merge(self, other_heap): j += 1 self.root_list = new_root_list - def insert(self, key, data): + def insert(self, key, data=None): """ Inserts new node with the given key and data. diff --git a/pydatastructs/trees/m_ary_trees.py b/pydatastructs/trees/m_ary_trees.py index f0a4d389a..75b622515 100644 --- a/pydatastructs/trees/m_ary_trees.py +++ b/pydatastructs/trees/m_ary_trees.py @@ -59,7 +59,7 @@ def __new__(cls, key=None, root_data=None, comp=None, obj.is_order_statistic = is_order_statistic return obj - def insert(self, key, data): + def insert(self, key, data=None): """ Inserts data by the passed key using iterative algorithm.