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)
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
return self.data < othernode.data class HuffmanPrioQ(PrioQueue): def number(self): return len(self._elems) def HuffmanTree(weights): trees = HuffmanPrioQ() for w in weights: trees.enqueue(HTNode(w)) while trees.number() > 1: t1 = trees.dequeue() t2 = trees.dequeue() x = t1.data + t2.data trees.enqueue(HTNode(x, t1, t2)) return trees.dequeue() if __name__ == '__main__': t = BinTNode(1, BinTNode(2), BinTNode(3)) print_BinTNodes(t) print("\n") h = HuffmanTree([2, 3, 7, 10, 4, 2, 5]) print_BinTNodes(h) pass
def __init__(self, data): BinTNode.__init__(self, data) self.bf = 0