From 2de0567e5b9222fd7e2ea417d9e1cf094f2f4dc2 Mon Sep 17 00:00:00 2001 From: halilpython <65048618+halilpython@users.noreply.github.com> Date: Sat, 9 May 2020 01:38:01 +0300 Subject: [PATCH 1/6] Max heap implementation --- data_structures/heap/max_heap.py | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 data_structures/heap/max_heap.py diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py new file mode 100644 index 000000000000..599d5879298f --- /dev/null +++ b/data_structures/heap/max_heap.py @@ -0,0 +1,88 @@ +class BinaryHeap: + """ + A max-heap implementation in Python + >>> sample = BinaryHeap() + >>> sample.insert(6) + >>> sample.insert(10) + >>> sample.insert(15) + >>> sample.insert(12) + >>> print(sample.pop()) + 15 + >>> print(sample.pop()) + 12 + >>> print(sample.get_list) + [10, 6] + >>> print(sample.get_size) + 2 + """ + + def __init__(self): + self.__heap = [0] + self.__size = 0 + + def __swap_up(self, i): + """ Swap the element up """ + temporary = self.__heap[i] + while i // 2 > 0: + if self.__heap[i] > self.__heap[i // 2]: + self.__heap[i] = self.__heap[i // 2] + self.__heap[i // 2] = temporary + i //= 2 + + def insert(self, value): + """ Insert new element """ + self.__heap.append(value) + self.__size += 1 + self.__swap_up(self.__size) + + def __swap_down(self, i): + """ Swap the element down """ + while self.__size >= 2*i: + if 2 * i + 1 > self.__size: + bigger_child = 2 * i + else: + bigger_child = 2 * \ + i if self.__heap[2 * i] > self.__heap[2 * + i + 1] else 2 * i + 1 + temporary = self.__heap[i] + if self.__heap[i] < self.__heap[bigger_child]: + self.__heap[i] = self.__heap[bigger_child] + self.__heap[bigger_child] = temporary + i = bigger_child + + def pop(self): + """ Pop the root element """ + max_value = self.__heap[1] + self.__heap[1] = self.__heap[self.__size] + self.__size -= 1 + self.__heap.pop() + self.__swap_down(1) + return max_value + + @property + def get_list(self): + return self.__heap[1:] + + @property + def get_size(self): + return self.__size + + +# example +# create an instance of BinaryHeap object +sample = BinaryHeap() +# insert values +sample.insert(6) +sample.insert(10) +sample.insert(15) +sample.insert(12) +# pop root(max-values because it is max heap) +print(sample.pop()) # 15 +print(sample.pop()) # 12 +# get the list and size after operations +print(sample.get_list) +print(sample.get_size) + +if __name__ == "__main__": + import doctest + doctest.testmod() From 2641c22671022abb8015c4bee2d2725ec9635251 Mon Sep 17 00:00:00 2001 From: halilylm <65048618+halilylm@users.noreply.github.com> Date: Sun, 10 May 2020 13:26:52 +0300 Subject: [PATCH 2/6] Update max_heap.py --- data_structures/heap/max_heap.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index 599d5879298f..411f4ab685cc 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -20,7 +20,7 @@ def __init__(self): self.__heap = [0] self.__size = 0 - def __swap_up(self, i): + def __swap_up(self, i: int) -> None: """ Swap the element up """ temporary = self.__heap[i] while i // 2 > 0: @@ -29,13 +29,13 @@ def __swap_up(self, i): self.__heap[i // 2] = temporary i //= 2 - def insert(self, value): + def insert(self, value: int) -> None: """ Insert new element """ self.__heap.append(value) self.__size += 1 self.__swap_up(self.__size) - def __swap_down(self, i): + def __swap_down(self, i: int) -> None: """ Swap the element down """ while self.__size >= 2*i: if 2 * i + 1 > self.__size: @@ -50,7 +50,7 @@ def __swap_down(self, i): self.__heap[bigger_child] = temporary i = bigger_child - def pop(self): + def pop(self) -> int: """ Pop the root element """ max_value = self.__heap[1] self.__heap[1] = self.__heap[self.__size] From 0237139b6676d429048d501d6c94596780d475fc Mon Sep 17 00:00:00 2001 From: halilylm <65048618+halilylm@users.noreply.github.com> Date: Sun, 10 May 2020 13:37:22 +0300 Subject: [PATCH 3/6] Update max_heap.py --- data_structures/heap/max_heap.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index 411f4ab685cc..cd92d1cd7f48 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -37,13 +37,13 @@ def insert(self, value: int) -> None: def __swap_down(self, i: int) -> None: """ Swap the element down """ - while self.__size >= 2*i: + while self.__size >= 2 * i: if 2 * i + 1 > self.__size: bigger_child = 2 * i else: bigger_child = 2 * \ - i if self.__heap[2 * i] > self.__heap[2 * - i + 1] else 2 * i + 1 + i if self.__heap[2 * i] > self.__heap[2 * + i + 1] else 2 * i + 1 temporary = self.__heap[i] if self.__heap[i] < self.__heap[bigger_child]: self.__heap[i] = self.__heap[bigger_child] @@ -85,4 +85,5 @@ def get_size(self): if __name__ == "__main__": import doctest + doctest.testmod() From f896082eac18a1329f73a8495aee3e82f5d844e9 Mon Sep 17 00:00:00 2001 From: halilylm <65048618+halilylm@users.noreply.github.com> Date: Sun, 10 May 2020 13:49:06 +0300 Subject: [PATCH 4/6] Update max_heap.py --- data_structures/heap/max_heap.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index cd92d1cd7f48..ce0e7cad7da1 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -41,9 +41,10 @@ def __swap_down(self, i: int) -> None: if 2 * i + 1 > self.__size: bigger_child = 2 * i else: - bigger_child = 2 * \ - i if self.__heap[2 * i] > self.__heap[2 * - i + 1] else 2 * i + 1 + if self.__heap[2 * i] > self.__heap[2 * i + 1]: + bigger_child = 2 * i + else: + bigger_child = 2 * i + 1 temporary = self.__heap[i] if self.__heap[i] < self.__heap[bigger_child]: self.__heap[i] = self.__heap[bigger_child] From 857d09e58e28769c391b0473aa128692e55b6d39 Mon Sep 17 00:00:00 2001 From: halilylm <65048618+halilylm@users.noreply.github.com> Date: Sun, 10 May 2020 17:59:11 +0300 Subject: [PATCH 5/6] __len__ method added --- data_structures/heap/max_heap.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index ce0e7cad7da1..280b94d6d317 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -12,7 +12,7 @@ class BinaryHeap: 12 >>> print(sample.get_list) [10, 6] - >>> print(sample.get_size) + >>> print(len(sample)) 2 """ @@ -64,8 +64,8 @@ def pop(self) -> int: def get_list(self): return self.__heap[1:] - @property - def get_size(self): + def __len__(self): + """ Length of the array """ return self.__size @@ -82,7 +82,7 @@ def get_size(self): print(sample.pop()) # 12 # get the list and size after operations print(sample.get_list) -print(sample.get_size) +print(len(sample)) if __name__ == "__main__": import doctest From ab3e4081dfb00d7c165d41630c57c6c92ae490a9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 10 May 2020 17:45:37 +0200 Subject: [PATCH 6/6] Update max_heap.py --- data_structures/heap/max_heap.py | 45 +++++++++++++++----------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index 280b94d6d317..2a08f8fa2cd1 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -1,18 +1,18 @@ class BinaryHeap: """ A max-heap implementation in Python - >>> sample = BinaryHeap() - >>> sample.insert(6) - >>> sample.insert(10) - >>> sample.insert(15) - >>> sample.insert(12) - >>> print(sample.pop()) + >>> binary_heap = BinaryHeap() + >>> binary_heap.insert(6) + >>> binary_heap.insert(10) + >>> binary_heap.insert(15) + >>> binary_heap.insert(12) + >>> binary_heap.pop() 15 - >>> print(sample.pop()) + >>> binary_heap.pop() 12 - >>> print(sample.get_list) + >>> binary_heap.get_list [10, 6] - >>> print(len(sample)) + >>> len(binary_heap) 2 """ @@ -69,22 +69,19 @@ def __len__(self): return self.__size -# example -# create an instance of BinaryHeap object -sample = BinaryHeap() -# insert values -sample.insert(6) -sample.insert(10) -sample.insert(15) -sample.insert(12) -# pop root(max-values because it is max heap) -print(sample.pop()) # 15 -print(sample.pop()) # 12 -# get the list and size after operations -print(sample.get_list) -print(len(sample)) - if __name__ == "__main__": import doctest doctest.testmod() + # create an instance of BinaryHeap + binary_heap = BinaryHeap() + binary_heap.insert(6) + binary_heap.insert(10) + binary_heap.insert(15) + binary_heap.insert(12) + # pop root(max-values because it is max heap) + print(binary_heap.pop()) # 15 + print(binary_heap.pop()) # 12 + # get the list and size after operations + print(binary_heap.get_list) + print(len(binary_heap))