Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
        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
Ejemplo n.º 4
0
 def __init__(self, data):
     BinTNode.__init__(self, data)
     self.bf = 0