def ceiling(self, key): if key is None: raise IllegalArgumentException("called ceiling() with None key") keys = self.keys() ceiling = None for k in keys: if (ceiling is None and k >= key) or (ceiling is not None and k >= key and k < ceiling): ceiling = k if ceiling is None: raise NoSuchElementException("all keys are less than " + str(key)) return ceiling
def change_key(self, i, key): """ Change the key associated with index i to the specified value. :param i: the index of the key to change :param key: change the key associated with index i to this key :raises IllegalArgumentException: unless 0 <= i < max_n :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") if not self.contains(i): raise NoSuchElementException("index is not in the priority queue") self.keys[i] = key self._swim(self.qp[i]) self._sink(self.qp[i])
def del_min(self): """ Removes a minimum key and returns its associated index. :return: an index associated with a minimum key :raises NoSuchElementException: if this priority queue is empty :rtype: int """ if self.n == 0: raise NoSuchElementException("Priority queue underflow") _min = self.pq[1] self._exch(1, self.n) self.n -= 1 self._sink(1) self.qp[_min] = -1 self.keys[_min] = None self.pq[self.n + 1] = -1 return _min
def delete(self, i): """ Remove the key associated with index i :param i: the index of the key to remove :raises IllegalArgumentException: unless 0 <= i < max_n :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not in range") if not self.contains(i): raise NoSuchElementException("index is not in the priority queue") index = self.qp[i] self._exch(index, self.n) self.n -= 1 self._sink(index) self.keys[i] = None self.qp[i] = -1
def increase_key(self, i, key): """ Increase the key associated with index i to the specified value. :param i: the index of the key to increase :param key: increase the key associated with index i to this key :raises IllegalArgumentException: unless 0 <= i < max_n :raises IllegalArgumentException: if key <= key_of(i) :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") if not self.contains(i): raise NoSuchElementException("index is not in the priority queue") if self.keys[i] >= key: raise IllegalArgumentException("calling increase_key() with given argument would not strictly increase the key") self.keys[i] = key self._sink(self.qp[i])
def del_max(self) -> Key: """ Removes and returns a largest key on this priority queue. :return: a largest key on this priority queue :raises NoSuchElementException: if this priority queue is empty """ if self.is_empty(): raise NoSuchElementException("Priority queue underflow") _max = self._pq[1] assert _max is not None self._exch(1, self._n) self._n -= 1 self._sink(1) self._pq[self._n + 1] = None if self._n > 0 and self._n == (len(self._pq) - 1) // 4: self._resize(len(self._pq) // 2) return _max
def min(self): if self.is_empty(): raise NoSuchElementException("called min() with empty set") return min(self._set)
def max(self): if self.is_empty(): raise NoSuchElementException("called max() with empty symbol table") return max(self._st)