Exemplo n.º 1
0
 def add(self, priority, value):
     if self.count >= self.capacity:
         raise Exceptions.IllegalArgumentError("Heap is full!")
     new_entry = HeapEntry(priority, value)
     self.heap.append(new_entry)
     self.count += 1
     self.trickle_up(self.heap, self.count - 1)
Exemplo n.º 2
0
 def remove(self):
     if self.count < 1:
         raise Exceptions.IllegalArgumentError("Heap is empty!")
     temp = self.heap[0]
     self.heap[0] = self.heap[self.count - 1]
     self.count -= 1
     self.trickle_down(self.heap, 0, self.count)
     return temp.get_value()
Exemplo n.º 3
0
 def remove_last(self):
     if self.is_empty() is True:
         raise Exceptions.ListError("Linked List is empty!")
     elif self._count == 1:
         node_val = self._tail.get_value()
         self._head = None
         self._tail = None
         self._count -= 1
     else:
         node_val = self._tail.get_value()
         self._tail = self._tail.get_prev()
         self._tail.set_next(None)
         self._count -= 1
     return node_val
 def insert_rec(self, current, key, value):
     index = 0
     num_keys = current.get_key_size()
     tie_breaker = False
     if current.key_present(key):
         raise Exceptions.IllegalArgumentError(f'Keys must be unique: {key} already present')
     if current.get_child_size() == 0:  # Base case reached
         if current.get_key_size() < self.max_key_size - 1:
             print(f'Node has space, inserted key: {key} and value: {value}')
             current.insert_node(key, value)
             self.size += 1
         elif current.get_key_size() == self.max_key_size - 1:
             print("\033[1mNode to insert is full, split!\033[0m")
             self.split(current)
             self.insert(key, value)
     else:
         while not tie_breaker:
             compare = current.get_node(index).get_key()
             if index == 0 and key <= compare:
                 print(f'Key {key} is lesser than {compare} at index[{index}]')
                 current = current.get_children(index)
                 self.insert_rec(current, key, value)
                 tie_breaker = True
             elif index == num_keys - 1 and key > compare:
                 print(f'Key {key} is larger than {compare} at index[{index}]')
                 current = current.get_children(index)
                 self.insert_rec(current, key, value)
                 tie_breaker = True
             elif index > 0:
                 previous = current.get_node(index - 1).get_key()
                 following = current.get_node(index).get_key()
                 if previous < key <= following:
                     print(f'Key {key} is in between than {previous} and {following} and inserted in index[{index}]')
                     current = current.get_children(index)
                     self.insert_rec(current, key, value)
                     tie_breaker = True
             index += 1
Exemplo n.º 5
0
 def peek_last(self):
     if self.is_empty() is True:
         raise Exceptions.ListError("Linked List is empty!")
     else:
         node_val = self._tail.get_value()
     return node_val