|
| 1 | +class VEBTree: |
| 2 | + def __init__(self, u): |
| 3 | + self.u = u # Universe size (must be a power of two) |
| 4 | + self.min_val = None |
| 5 | + self.max_val = None |
| 6 | + |
| 7 | + if u < 2: |
| 8 | + raise ValueError("Universe size must be at least 2") |
| 9 | + if (u & (u - 1)) != 0: |
| 10 | + raise ValueError("Universe size must be a power of two") |
| 11 | + |
| 12 | + self.summary = None |
| 13 | + self.clusters = None |
| 14 | + |
| 15 | + if u > 2: |
| 16 | + exponent = u.bit_length() - 1 |
| 17 | + self.upper_sqrt = 1 << ((exponent + 1) // 2) |
| 18 | + self.lower_sqrt = 1 << (exponent // 2) |
| 19 | + self.summary = VEBTree(self.upper_sqrt) |
| 20 | + self.clusters = [VEBTree(self.lower_sqrt) for _ in range(self.upper_sqrt)] |
| 21 | + |
| 22 | + def member(self, x): |
| 23 | + if x < 0 or x >= self.u: |
| 24 | + return False |
| 25 | + if x == self.min_val or x == self.max_val: |
| 26 | + return True |
| 27 | + if self.u == 2: |
| 28 | + return False |
| 29 | + cluster_idx = x // self.lower_sqrt |
| 30 | + idx_in_cluster = x % self.lower_sqrt |
| 31 | + return self.clusters[cluster_idx].member(idx_in_cluster) |
| 32 | + |
| 33 | + def insert(self, x): |
| 34 | + if x < 0 or x >= self.u: |
| 35 | + raise ValueError("Value out of range") |
| 36 | + if self.min_val is None: |
| 37 | + self.min_val = self.max_val = x |
| 38 | + return |
| 39 | + if x < self.min_val: |
| 40 | + x, self.min_val = self.min_val, x |
| 41 | + if self.u > 2: |
| 42 | + cluster_idx = x // self.lower_sqrt |
| 43 | + idx_in_cluster = x % self.lower_sqrt |
| 44 | + if self.clusters[cluster_idx].min_val is None: |
| 45 | + self.summary.insert(cluster_idx) |
| 46 | + self.clusters[cluster_idx].insert(idx_in_cluster) |
| 47 | + if x > self.max_val: |
| 48 | + self.max_val = x |
| 49 | + |
| 50 | + def successor(self, x): |
| 51 | + if x < 0 or x >= self.u: |
| 52 | + return None |
| 53 | + if self.u == 2: |
| 54 | + if x == 0 and self.max_val == 1: |
| 55 | + return 1 |
| 56 | + else: |
| 57 | + return None |
| 58 | + if self.min_val is not None and x < self.min_val: |
| 59 | + return self.min_val |
| 60 | + cluster_idx = x // self.lower_sqrt |
| 61 | + idx_in_cluster = x % self.lower_sqrt |
| 62 | + max_in_cluster = self.clusters[cluster_idx].max_val |
| 63 | + if max_in_cluster is not None and idx_in_cluster < max_in_cluster: |
| 64 | + offset = self.clusters[cluster_idx].successor(idx_in_cluster) |
| 65 | + if offset is not None: |
| 66 | + return cluster_idx * self.lower_sqrt + offset |
| 67 | + else: |
| 68 | + next_cluster = self.summary.successor(cluster_idx) |
| 69 | + if next_cluster is not None: |
| 70 | + offset = self.clusters[next_cluster].min_val |
| 71 | + return next_cluster * self.lower_sqrt + offset |
| 72 | + else: |
| 73 | + if self.max_val is not None and x < self.max_val: |
| 74 | + return self.max_val |
| 75 | + return None |
| 76 | + |
| 77 | + def delete(self, x): |
| 78 | + if x < 0 or x >= self.u: |
| 79 | + raise ValueError("Value out of range") |
| 80 | + if self.min_val == self.max_val: |
| 81 | + if x == self.min_val: |
| 82 | + self.min_val = self.max_val = None |
| 83 | + return |
| 84 | + if self.u == 2: |
| 85 | + if x == self.min_val: |
| 86 | + self.min_val = self.max_val |
| 87 | + else: |
| 88 | + self.max_val = self.min_val |
| 89 | + return |
| 90 | + if x == self.min_val: |
| 91 | + first_cluster = self.summary.min_val |
| 92 | + idx = self.clusters[first_cluster].min_val |
| 93 | + self.min_val = first_cluster * self.lower_sqrt + idx |
| 94 | + self.clusters[first_cluster].delete(idx) |
| 95 | + if self.clusters[first_cluster].min_val is None: |
| 96 | + self.summary.delete(first_cluster) |
| 97 | + else: |
| 98 | + cluster_idx = x // self.lower_sqrt |
| 99 | + idx = x % self.lower_sqrt |
| 100 | + self.clusters[cluster_idx].delete(idx) |
| 101 | + if self.clusters[cluster_idx].min_val is None: |
| 102 | + self.summary.delete(cluster_idx) |
| 103 | + # Update max_val |
| 104 | + if self.summary.max_val is None: |
| 105 | + self.max_val = self.min_val |
| 106 | + else: |
| 107 | + last_cluster = self.summary.max_val |
| 108 | + self.max_val = last_cluster * self.lower_sqrt + self.clusters[last_cluster].max_val |
| 109 | + |
| 110 | + def get_min(self): |
| 111 | + return self.min_val |
| 112 | + |
| 113 | + def get_max(self): |
| 114 | + return self.max_val |
| 115 | + |
| 116 | +# Example usage |
| 117 | +if __name__ == "__main__": |
| 118 | + veb = VEBTree(16) |
| 119 | + veb.insert(2) |
| 120 | + veb.insert(3) |
| 121 | + veb.insert(7) |
| 122 | + veb.insert(14) |
| 123 | + |
| 124 | + print("Member 3:", veb.member(3)) # True |
| 125 | + print("Member 4:", veb.member(4)) # False |
| 126 | + |
| 127 | + print("Successor of 3:", veb.successor(3)) # 7 |
| 128 | + print("Min:", veb.get_min()) # 2 |
| 129 | + print("Max:", veb.get_max()) # 14 |
| 130 | + |
| 131 | + veb.delete(3) |
| 132 | + print("Member 3 after delete:", veb.member(3)) # False |
| 133 | + print("Successor of 2:", veb.successor(2)) # 7 |
0 commit comments