Skip to content

Commit

Permalink
Convert _heapify method to n iterative one (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpuru98 authored and czgdp1807 committed Dec 19, 2019
1 parent 8da8f36 commit 4a67e3f
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pydatastructs/trees/heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,24 @@ def _swap(self, idx1, idx2):
idx1_key, idx1_data

def _heapify(self, i):
target = i
l = 2*i + 1
r = 2*i + 2
while True:
target = i
l = 2*i + 1
r = 2*i + 2

if l <= self._last_pos_filled:
target = l if self._comp(self.heap[l].key, self.heap[target].key) \
if l <= self._last_pos_filled:
target = l if self._comp(self.heap[l].key, self.heap[target].key) \
else i
if r <= self._last_pos_filled:
target = r if self._comp(self.heap[r].key, self.heap[target].key) \
if r <= self._last_pos_filled:
target = r if self._comp(self.heap[r].key, self.heap[target].key) \
else target

if target != i:
self._swap(target, i)
i = target
self._heapify(i)
if target != i:
self._swap(target, i)
i = target
else:
break


def insert(self, key, data):
"""
Expand Down

0 comments on commit 4a67e3f

Please sign in to comment.