def huffman(str):
    freqdict = frequency(str)
    pq = PriorityQueue()

    for char in freqdict:  # NOT for char in STRING!!
        t = BinaryTree(char)
        key = freqdict[char]
        pq.add((key, t))

    while pq.getSize() > 1:
        u = pq.delMin()
        v = pq.delMin()
        t = BinaryTree(u[0] + v[0])
        t.insertLeft(u[1])  # inserts left subtree T1
        t.insertRight(v[1])  # inserts right subtree T2
        pq.add((u[0] + v[0],
                t))  # inserts T into PQ with key f1 + f2 (from u and v tuples)

    result = pq.delMin()  # tuple of (freq, T)
    return result[1]  # returns the final tree, T
示例#2
0
def dijskstra(source):
    global dist
    global adjList

    pq = PriorityQueue()
    dist[source] = 0
    pq.insertNode((source, 0))

    while pq.getSize() > 1:
        currPair = pq.getMin()
        currNodeIndex = currPair[0]
        currWeight = currPair[1]
        if( currWeight > dist[currNodeIndex]):
            continue
        
        for neighbor in adjList[currNodeIndex]:
            nextNodeIndex = neighbor[0]
            nextWeight = neighbor[1]
            if dist[currNodeIndex] + nextWeight < dist[nextNodeIndex]:
                dist[nextNodeIndex] = dist[currNodeIndex] + nextWeight
                pq.insertNode(neighbor)
from PriorityQueue import PriorityQueue

patient1 = [2, "John"]
patient2 = [1, "Jim"]
patient3 = [5, "Tim"]
patient4 = [7, "Cindy"]

priorityQueue = PriorityQueue()
priorityQueue.enqueue(patient1)
priorityQueue.enqueue(patient2)
priorityQueue.enqueue(patient3)
priorityQueue.enqueue(patient4)
    
while priorityQueue.getSize() > 0:
    print(str(priorityQueue.dequeue()), end = " ")
示例#4
0
from PriorityQueue import PriorityQueue

patient1 = [2, "John"]
patient2 = [1, "Jim"]
patient3 = [5, "Tim"]
patient4 = [7, "Cindy"]

priorityQueue = PriorityQueue()
priorityQueue.enqueue(patient1)
priorityQueue.enqueue(patient2)
priorityQueue.enqueue(patient3)
priorityQueue.enqueue(patient4)

while priorityQueue.getSize() > 0:
    print(str(priorityQueue.dequeue()), end=" ")