Skip to content

Commit

Permalink
add MaxPQ MinPQ
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Dec 19, 2017
1 parent f4f7078 commit 1a283dc
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
43 changes: 43 additions & 0 deletions algs4/max_pq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class MaxPQ:

def __init__(self):
self.pq = []

def insert(self, v):
self.pq.append(v)
self.swim(len(self.pq) - 1)

def max():
return self.pq[1]

def del_max(self, ):
m = self.pq[0]
self.pq[0], self.pq[-1] = self.pq[-1], self.pq[0]
self.pq = self.pq[:-1]
self.sink(0)
return m

def is_empty(self, ):
return not self.pq

def size(self, ):
return len(self.pq)

def swim(self, k):
while k > 0 and self.pq[(k - 1) // 2] < self.pq[k]:
self.pq[k], self.pq[
(k - 1) // 2] = self.pq[(k - 1) // 2], self.pq[k]
k = k // 2

def sink(self, k):
N = len(self.pq)

while 2 * k + 1 <= N - 1:
j = 2 * k + 1
if j < N - 1 and self.pq[j] < self.pq[j + 1]:
j += 1

if self.pq[k] > self.pq[j]:
break
self.pq[k], self.pq[j] = self.pq[j], self.pq[k]
k = j
44 changes: 44 additions & 0 deletions algs4/min_pq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class MinPQ:

def __init__(self):
self.pq = []

def insert(self, v):
self.pq.append(v)
self.swim(len(self.pq) - 1)

def min(self):
return self.pq[1]

def del_min(self, ):
m = self.pq[0]
self.pq[0], self.pq[-1] = self.pq[-1], self.pq[0]
self.pq = self.pq[:-1]
self.sink(0)
return m

def is_empty(self, ):
return not self.pq

def size(self, ):
return len(self.pq)

def swim(self, k):
while k > 0 and self.pq[(k - 1) // 2] > self.pq[k]:
self.pq[k], self.pq[
(k - 1) // 2] = self.pq[(k - 1) // 2], self.pq[k]
k = (k - 1) // 2

def sink(self, k):
N = len(self.pq)

while 2 * k + 1 <= N - 1:
j = 2 * k + 1
if j < N - 1 and self.pq[j] > self.pq[j + 1]:
j += 1

if self.pq[k] < self.pq[j]:
break

self.pq[k], self.pq[j] = self.pq[j], self.pq[k]
k = j
19 changes: 19 additions & 0 deletions algs4/top_m.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys

from algs4.min_pq import MinPQ
from algs4.stack import Stack
from algs4.transaction import Transaction

M = int(sys.argv[1])
pq = MinPQ()
for line in sys.stdin:
Transaction(line)
pq.insert(Transaction(line))
if pq.size() > M:
pq.del_min()

stack = Stack()
while not pq.is_empty():
stack.push(pq.del_min())
for t in stack:
print(t)
26 changes: 26 additions & 0 deletions algs4/transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Transaction:

def __init__(self, l):
who, when, amount = l.split()
self.who = who
self.when = when
self.amount = float(amount)

def __str__(self):
return "%-10s %10s %8.2f" % (self.who, self.when, float(self.amount))

def __lt__(self, other):
return self.amount < other.amount

def __gt__(self, other):
return self.amount > other.amount

def __eq__(self, other):
return self.amount == other.amount

def hashcode(self):
hashcode = 1
hashcode = 31 * hashcode + hash(self.who)
hashcode = 31 * hashcode + hash(self.when)
hashcode = 31 * hashcode + hash(self.amount)
return hashcode

0 comments on commit 1a283dc

Please sign in to comment.