Exemple #1
0
    def _delete(self, x, key):
        if x is None:
            return None

        compared = cmp(key, x.key)
        if compared < 0:
            x.left = self._delete(x.left, key)
        elif compared > 0:
            x.right = self._delete(x.right, key)
        else:
            if x.left is None:
                return x.right
            elif x.right is None:
                return x.left
            else:
                m = self.min(x.right)
                m.right = self.del_min(x.right)
                m.left = x.left

                x = m

        if self.is_red(x.right) and not self.is_red(x.left):
            x = self.rotate_left(x)
        if self.is_red(x.left) and self.is_red(x.left.left):
            x = self.rotate_right(x)
        if self.is_red(x.left) and self.is_red(x.right):
            x = self.flip_colors(x)

        x.count = 1 + _count(x.left) + _count(x.right)
        return x
Exemple #2
0
 def get(self, key):
     i = self.hash(key)
     x = self.id[i]
     while x is not None:
         if util.cmp(x.key, key) == 0:
             return x.value
         x = x.next_node
     return None
Exemple #3
0
 def contains(self, key):
     i = self.hash(key)
     x = self.id[i]
     while x is not None:
         if util.cmp(x.key, key) == 0:
             return True
         x = x.next_node
     return False
Exemple #4
0
 def get(self, key):
     i = self.hash(key)
     for j in range(self.M):
         k = (i + j) % self.M
         x = self.id[k]
         if x is None:
             return None
         if util.cmp(key, x.key) == 0:
             return x.value
Exemple #5
0
 def contains(self, key):
     i = self.hash(key)
     for j in range(self.M):
         k = (i + j) % self.M
         x = self.id[k]
         if x is None:
             return False
         if util.cmp(key, x.key) == 0:
             return True
Exemple #6
0
 def _get(self, x, key):
     if x is None:
         return None
     compared = cmp(key, x.key)
     if compared < 0:
         return self._get(x.left, key)
     elif compared > 0:
         return self._get(x.right, key)
     else:
         return x
Exemple #7
0
 def delete(self, key):
     i = self.hash(key)
     for j in range(self.M):
         k = (i + j) % self.M
         x = self.id[k]
         if x is None:
             return None
         if util.cmp(key, x.key) == 0:
             self.id[k] = None
             self.N -= 1
             if self.N == self.M // 4:
                 self.resize(self.M // 2)
             return x.value
Exemple #8
0
 def add(self, key):
     i = self.hash(key)
     for j in range(self.M):
         k = (i + j) % self.M
         x = self.id[k]
         if x is None:
             self.id[k] = Node(key)
             self.N += 1
             if self.N == self.M // 2:
                 self.resize(self.M * 2)
             break
         if util.cmp(x.key, key) == 0:
             break
Exemple #9
0
 def _get(self, x, key, d):
     if x is None:
         return None
     c = char_at(key, d)
     compared = util.cmp(c, x.key)
     if compared < 0:
         return self._get(x.left, key, d)
     elif compared > 0:
         return self._get(x.right, key, d)
     elif len(key) - 1 == d:
         return x
     else:
         return self._get(x.mid, key, d + 1)
Exemple #10
0
    def add(self, key):
        if key is None:
            raise ValueError('key cannot be None')

        i = self.hash(key)
        x = self.id[i]
        while x is not None:
            if util.cmp(x.key, key) == 0:
                return
            x = x.next_node
        old_first = self.id[i]
        self.id[i] = Node(key)
        self.id[i].next_node = old_first
        self.N += 1
Exemple #11
0
    def _put(self, x, key, value):
        if x is None:
            return Node(key, value)

        compared = cmp(key, x.key)

        if compared < 0:
            x.left = self._put(x.left, key, value)
        elif compared > 0:
            x.right = self._put(x.right, key, value)
        else:
            x.value = value

        x.count = 1 + _count(x.left) + _count(x.right)

        return x
Exemple #12
0
 def delete(self, key):
     i = self.hash(key)
     x = self.id[i]
     prev_node = None
     while x is not None:
         if util.cmp(x.key, key) == 0:
             next_node = x.next_node
             self.N -= 1
             if prev_node is not None:
                 prev_node.next_node = next_node
             if self.id[i] == x:
                 self.id[i] = None
             return True
         prev_node = x
         x = x.next_node
     return False
Exemple #13
0
    def _put(self, x, key, value, d):
        c = char_at(key, d)
        if x is None:
            x = TSTNode(c)
        compared = util.cmp(c, x.key)
        if compared < 0:
            x.left = self._put(x.left, key, value, d)
        elif compared > 0:
            x.right = self._put(x.right, key, value, d)
        elif len(key) - 1 == d:
            if x.value is None:
                self.N += 1
            x.value = value
        else:
            x.mid = self._put(x.mid, key, value, d + 1)

        return x
Exemple #14
0
    def _put2(self, x, key, value):
        if x is None:
            return Node(key, value, 1)
        compared = cmp(key, x.key)
        if compared < 0:
            x.left = self._put2(x.left, key, value)
        elif compared > 0:
            x.right = self._put2(x.right, key, value)
        else:
            x.value = value

        if self.is_red(x.right) and not self.is_red(x.left):
            x = self.rotate_left(x)
        if self.is_red(x.left) and self.is_red(x.left.left):
            x = self.rotate_right(x)
        if self.is_red(x.left) and self.is_red(x.right):
            x = self.flip_colors(x)

        x.count = 1 + _count(x.left) + _count(x.right)
        return x
Exemple #15
0
    def _delete(self, x, key):
        if x is None:
            return None

        compared = cmp(key, x.key)
        if compared < 0:
            x.left = self._delete(x.left, key)
        elif compared > 0:
            x.right = self._delete(x.right, key)
        else:
            if x.left is None:
                return x.right
            elif x.right is None:
                return x.left
            else:
                m = self.min(x.right)
                m.right = self.del_min(x.right)
                m.left = x.left

                x = m

        x.count = 1 + _count(x.left) + _count(x.right)
        return x
Exemple #16
0
 def __cmp__(self, other):
     return util.cmp(self.weight, other.weight)