def __init__(self, sectId, pos): self.ID = sectId # Our layer ID self.position = pos # Our relative position in the ordering of columns self.c = Assoc() # Assoc of columns self._width = 0 # Width of complete section self.visible = True # Is this section currently visible?
def buildOBT(data, start, end): if start > end: return None mid = (end + start) // 2 left = DictOptBinTree.buildOBT(data, start, mid - 1) right = DictOptBinTree.buildOBT(data, mid + 1, end) return BinTNode(Assoc(*data[mid]), left, right)
class Section: def __init__(self, sectId, pos): self.ID = sectId # Our layer ID self.position = pos # Our relative position in the ordering of columns self.c = Assoc() # Assoc of columns self._width = 0 # Width of complete section self.visible = True # Is this section currently visible? def _get_width(self): if(self.exposed): return self._width else: return len(self.ID) + 3 width = property(_get_width) def append(self, name, w): self.c[name] = w self._width += w + 1 def __repr__(self): rv = "\nID:" + self.ID rv += " exposed:" + str(self.exposed) rv += " width:" + str(self.width) rv += " visible:" + str(self.visible) rv += " RO:" + str(self.RO) + "\n" for k,v in self.c.iteritems(): rv += " col:" + k + " w:" + str(v) + " " return rv def dump(self): return self.__repr__()
def insert(self, key, value): bt = self._root if bt is None: self._root = BinTNode(Assoc(key, value)) return while True: entry = bt.data if key < entry.key: if bt.left is None: bt.left = BinTNode(Assoc(key, value)) return bt = bt.left elif key > entry.key: if bt.right is None: bt.right = BinTNode(Assoc(key, value)) return bt = bt.right else: bt.data.value = value return
def insert(self, key, value): a = p = self._root if a is None: self._root = AVLNode(Assoc(key, value)) return pa = q = None # 维持 pa, q 为 a, p 的父结点 while p: # 确定插入位置及最小非平衡子树 if key == p.data.key: # key存在,修改关联值 p.data.value = value return if p.bf != 0: pa, a = q, p # 已知最小非平衡子树 q = p if key < p.data.key: p = p.left else: p = p.right # q 是插入点的父结点,parent,a 记录最小非平衡子树 node = AVLNode(Assoc(key, value)) if key < q.data.key: q.left = node # 作为左子结点 else: q.right = node # 或右子结点 # 新结点已插入,a 是最小不平衡子树 if key < a.data.key: # 新结点在 a 的左子树 p = b = a.left d = 1 else: # 新结点在 a 的右子树 p = b = a.right d = -1 # d记录新结点在a哪棵子树 # 修改 b 到新结点路上各结点的BF值,b 为 a 的子结点 while p != node: # node 一定存在,不用判断 p 空 if key < p.data.key: # p 的左子树增高 p.bf = 1 p = p.left else: # p的右子树增高 p.bf = -1 p = p.right if a.bf == 0: # a原BF为0,不会失衡 a.bf = d return if a.bf == -d: # 新结点在较低子树里 a.bf = 0 return # 新结点在较高子树,失衡,必须调整 if d == 1: # 新结点在 a 的左子树 if b.bf == 1: b = DictAVL.LL(a, b) # LL 调整 else: b = DictAVL.LR(a, b) # LR 调整 else: # 新结点在 a 的右子树 if b.bf == -1: b = DictAVL.RR(a, b) # RR 调整 else: b = DictAVL.RL(a, b) # RL 调整 if pa is None: self._root = b # 原 a 为树根 else: if pa.left == a: pa.left = b else: pa.right = b
if x not in self._elems: self._elems.append(x) def includes(self, e): return e in self._elems def str_hash(s): h1 = 0 for c in s: h1 = h1 * 29 + ord(c) return h1 if __name__ == '__main__': lst1 = [Assoc(randint(1, 30), i) for i in range(16)] lst1.sort() print(list(map(str, lst1))) for i in range(1, 30, 3): ind = bisearch(lst1, i) print("Search", i, "in the list and get:", ind) print("12345:",str_hash("12345")) print("asdfg:",str_hash("asdfg")) pass
def insert(self, key, value): self._elems.append(Assoc(key, value))
def insert(self, key, value): a = p = self._root if a is None: self._root = AVLNode(Assoc(key, value)) return pa = q = None # 维持pa, q为a, p的父节点 while p: # 确定插入位置和最小非平衡子树 if key == p.data.key: p.data.value = value return if p.bf != 0: pa, a = q, p q = p if key < p.data.key: p = p.left else: p = p.right node = AVLNode(Assoc(key, value)) # q是插入点的父节点 if key < q.data.key: q.left = node else: q.right = node # 新结点已插入 if key < a.data.key: # 新结点在a的左子树 p = b = a.left d = 1 # d记录新结点在a的哪颗子树 else: p = b = a.right # 新结点在a的右子树 d = 1 # 修改b到新结点路径上各节点的BF值,b为a的子节点 while p != node: if key < p.data.key: # p的左子树增高 p.bf = 1 p = p.left else: # p的右子树增高 p.bf = -1 p = p.right if a.bf == 0: a.bf = d return if a.bf == -d: # 新结点在较低子树里 a.bf = 0 return # 新结点在较高子树,失衡 if d == 1: # 新结点在a的左子树 if b.bf == 1: b = DictAVL.LL(a, b) # LL调整 else: b = DictAVL.LR(a, b) # LR调整 else: # 新结点在a的右子树 if b.bf == -1: b = DictAVL.RR(a, b) # RR调整 else: b = DictAVL.RL(a, b) # RL调整 if pa is None: # 原a为树根,修改_root self._root = b else: # a为非树根 if pa.left == a: pa.left = b else: pa.right = b
def insert(self, key, value): a = p = self._root if self._root is None: self._root = AVLNode(Assoc(key, value)) return pa = q = None #q,p是用来搜索的 a是非平衡子树的树根 pa是非平衡子树树根的父结点 while p is not None: if key == p.data.key: #要插入的assoc对象的key已经存在 替换value p.data.value = value return if p.bf != 0: pa, a = q, p #记录非平衡子树 q = p #递归 q是p的父结点 if key < p.data.key: p = p.left else: p = p.right # 得到插入点的父结点q 最小非平衡子树a 最小非平衡子树的父结点pa node = AVLNode(Assoc(key, value)) #将结点插入 if key < q.data.key: q.left = node #作为左子结点 else: q.right = node #作为右子结点 if key < a.data.key: p = b = a.left #新结点在a的左子树 d = 1 #d是a左右子树的高度差 else: p = b = a.right #新结点在a的右子树 d = -1 #插入之后 要修改bf值 while p != node: if key < p.data.key: #新插入结点在a的左子树 左子树增高 p.bf = 1 p = p.left else: #新插入的结点在a的右子树 右子树增高 p.bf = -1 p = p.right if a.bf == 0: #a的原BF为0 不会失衡 a.bf = d return if a.bf == -d: #新结点插入在了a的较低的子树 a.bf = 0 return if d == 1: #新结点在a的左子树 if b.bf == 1: b = DictAVL.LL(a, b) #新结点在a的左子树的左子树 else: b = DictAVL.LR(a, b) #新结点在a的左子树的右子树 else: if b.bf == -1: b = DictAVL.RR(a, b) #新结点在a的右子树的右子树 else: b = DictAVL.RL(a, b) #新结点在a的右子树的左子树 #修改pa与其子树的联系 if pa is None: #a为树根 self._root = b else: if pa.left == a: pa.left = b else: pa.right = b