Exemple #1
0
    def __contains__(self, key):
        current_node = self.root

        if key.value is not None:
            while True:
                i = bisect_right(current_node.keys, key) - 1

                if i >= 0 and current_node.keys[i] == key:
                    return True
                elif current_node.is_leaf:
                    return False
                else:
                    current_node = current_node.children[i + 1]

        else:
            while True:
                i = bisect_right([i.key for i in current_node.keys],
                                 key.key) - 1

                if i >= 0 and current_node.keys[i].key == key.key:
                    return True
                elif current_node.is_leaf:
                    return False
                else:
                    current_node = current_node.children[i + 1]
Exemple #2
0
 def test_bisect_right(self):
     for length in range(10):
         array = sorted([random.randint(1, 10) for _ in range(length)])
         target = random.randint(0, 15)
         index = bisect_right(array, target)
         self.assertTrue(all(val <= target for val in array[:index]))
         self.assertTrue(all(val > target for val in array[index:]))
Exemple #3
0
    def __contains__(self, key):
        current_node = self.root
        while True:
            i = bisect_right(current_node.keys, key) - 1

            if i >= 0 and current_node.keys[i] == key:
                return True
            elif current_node.is_leaf:
                return False
            else:
                current_node = current_node.children[i + 1]
Exemple #4
0
def insert_with_binary_search(array, right_index):
    """
    Use binary search to find a position in already sorted array[0...right_index-1] to insert array[right_index] into,
    making array[0...right_index] a sorted array.
    :param array:
    :param right_index: right_index > 0
    :return:
    """
    x = array[right_index]
    index = bisect_right(array, x, 0, right_index)
    array[index + 1:right_index + 1] = array[index:right_index]
    array[index] = x
Exemple #5
0
    def _insert_to_nonfull_node(self, node, key):
        i = bisect_right(node.keys, key)

        if node.is_leaf:
            node.keys.insert(i, key)
        else:
            if len(node.children[i].keys
                   ) >= self.max_number_of_keys:  # overflow
                self._split_child(node, i)
                if node.keys[
                        i] < key:  # decide which child is going to have a new key
                    i += 1

            self._insert_to_nonfull_node(node.children[i], key)
Exemple #6
0
    def set_by_key(self, key, new_value):
        current_node = self.root

        while True:
            i = bisect_right([i.key for i in current_node.keys], key) - 1

            if i >= 0 and current_node.keys[i].key == key:
                current_node.keys[i].value = new_value
                return True
            elif current_node.is_leaf:
                return False
            else:
                current_node = current_node.children[i + 1]

        return False
Exemple #7
0
    def get_by_key(self, key):
        current_node = self.root
        item_to_get = KeyValue(key)

        while True:
            i = bisect_right([i.key for i in current_node.keys], key) - 1

            if i >= 0 and item_to_get == current_node.keys[i]:
                return current_node.keys[i].value
            elif current_node.is_leaf:
                return None
            else:
                current_node = current_node.children[i + 1]

        return None
Exemple #8
0
    def _remove_key(self, node, key) -> bool:
        try:
            key_index = node.keys.index(key)
            if node.is_leaf:
                node.keys.remove(key)
                return True
            else:
                self._remove_from_nonleaf_node(node, key_index)

            return True

        except ValueError:  # key not found in node
            if node.is_leaf:
                # print("Key not found.")
                return False  # key not found
            else:
                i = bisect_right(node.keys, key)

                action_performed = self._repair_tree(node, i)
                if action_performed:
                    return self._remove_key(node, key)
                else:
                    return self._remove_key(node.children[i], key)