def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if self.N == len(self.storage) - 1: self.resize(self.N * 2) self.N += 1 self.storage[self.N] = Entry(v, p) self.swim(self.N)
def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if self.N == self.size: raise RuntimeError('Priority Queue is Full!') self.N += 1 if self.N > _sums[self.level + 1]: self.level += 1 self.storage[self.N] = Entry(v, p) self.swim(self.N)
def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if self.N == self.size: raise RuntimeError('Priority Queue is Full!') to_add = Entry(v, p) idx = binary_array_search(self.storage, self.N - 1, p) if idx < 0: self.storage.insert(-idx - 1, to_add) else: self.storage.insert(idx, to_add) self.N += 1
def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if self.N == self.size: raise RuntimeError('Priority Queue is Full!') idx = binary_array_search(self.storage, self.N - 1, p) if idx < 0: # might be duplicate, might not... idx = -idx - 1 # move each element up to make room for i in range(self.N, idx, -1): self.storage[i] = self.storage[i - 1] self.storage[idx] = Entry(v, p) self.N += 1
def heap_enqueue_animation(): """Show changes to storage with each swim().""" from ch04.entry import Entry heap = initial_heap() heap.N += 1 heap.storage[heap.N] = Entry(12, 12) fig_num = 8 print('Fig. 4-{:<2d} : '.format(fig_num),' -- |' + '|'.join([' {:>3} '.format(e.priority) for e in heap.storage[1:heap.N+1]])) print() child = heap.N while child > 1 and heap.less(child//2, child): heap.swap(child, child//2) child = child // 2 fig_num += 1 print('Fig. 4-{:<2d} : '.format(fig_num),' -- |' + '|'.join([' {:>3} '.format(e.priority) for e in heap.storage[1:heap.N+1]]))
def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if len(self.storage) == self.size: raise RuntimeError('Priority Queue is Full!') self.storage.append(Entry(v, p))
def enqueue(self, v, p): """Enqueue (v, p) entry into priority queue.""" if self.N == self.size: raise RuntimeError('Priority Queue is full!') self.storage[self.N] = Entry(v, p) self.N += 1
def test_entry(self): from ch04.entry import Entry e = Entry('99', 101) self.assertEqual('[99 p=101]', str(e))