Пример #1
0
 def setUp(self):
     self.theHeap = BinHeap()
     self.theHeap.insert(FooThing(5, 'a'))
     self.theHeap.insert(FooThing(9, 'd'))
     self.theHeap.insert(FooThing(1, 'x'))
     self.theHeap.insert(FooThing(2, 'y'))
     self.theHeap.insert(FooThing(3, 'z'))
Пример #2
0
 def testMixed(self):
     myHeap = BinHeap()
     myHeap.insert(9)
     myHeap.insert(1)
     myHeap.insert(5)
     assert myHeap.delMin() == 1
     myHeap.insert(2)
     myHeap.insert(7)
     assert myHeap.delMin() == 2
     assert myHeap.delMin() == 5
Пример #3
0
 def testDupes(self):
     myHeap = BinHeap()
     myHeap.insert(9)
     myHeap.insert(1)
     myHeap.insert(8)
     myHeap.insert(1)
     assert myHeap.currentSize == 4
     assert myHeap.delMin() == 1
     assert myHeap.delMin() == 1
     assert myHeap.delMin() == 8
Пример #4
0
 def testBuildHeap(self):
     myHeap = BinHeap()
     myHeap.buildHeap([9, 5, 6, 2, 3])
     f = myHeap.delMin()
     print("f = ", f)
     assert f == 2
     assert myHeap.delMin() == 3
     assert myHeap.delMin() == 5
     assert myHeap.delMin() == 6
     assert myHeap.delMin() == 9
Пример #5
0
class TestBinHeap(unittest.TestCase):
    def setUp(self):
        self.theHeap = BinHeap()
        self.theHeap.insert(FooThing(5, 'a'))
        self.theHeap.insert(FooThing(9, 'd'))
        self.theHeap.insert(FooThing(1, 'x'))
        self.theHeap.insert(FooThing(2, 'y'))
        self.theHeap.insert(FooThing(3, 'z'))

    def testInsert(self):
        assert self.theHeap.currentSize == 5

    def testDelmin(self):
        assert self.theHeap.delMin().val == 'x'
        assert self.theHeap.delMin().val == 'y'
        assert self.theHeap.delMin().val == 'z'
        assert self.theHeap.delMin().val == 'a'

    def testMixed(self):
        myHeap = BinHeap()
        myHeap.insert(9)
        myHeap.insert(1)
        myHeap.insert(5)
        assert myHeap.delMin() == 1
        myHeap.insert(2)
        myHeap.insert(7)
        assert myHeap.delMin() == 2
        assert myHeap.delMin() == 5

    def testDupes(self):
        myHeap = BinHeap()
        myHeap.insert(9)
        myHeap.insert(1)
        myHeap.insert(8)
        myHeap.insert(1)
        assert myHeap.currentSize == 4
        assert myHeap.delMin() == 1
        assert myHeap.delMin() == 1
        assert myHeap.delMin() == 8

    def testBuildHeap(self):
        myHeap = BinHeap()
        myHeap.buildHeap([9, 5, 6, 2, 3])
        f = myHeap.delMin()
        print("f = ", f)
        assert f == 2
        assert myHeap.delMin() == 3
        assert myHeap.delMin() == 5
        assert myHeap.delMin() == 6
        assert myHeap.delMin() == 9
Пример #6
0
#!/usr/bin/env python
# coding=utf-8
from pythonds.trees import BinHeap

bh = BinHeap()
bh.insert(5)
bh.insert(7)
bh.insert(3)
bh.insert(11)

print(bh.delMin())

print(bh.delMin())

print(bh.delMin())

print(bh.delMin())
# Use Binary Heap to implement a priority queue
# A Binary Heap will allow us to enqueue and dequeue items both with O(log n) performance which is very fast.
# -Max Heap: priority is given to the biggest value
# -Min Heap: priority is given to the smallest value

# Binary Heap Operations:
# -BinaryHeap() creates a new, empty, binary heap.
# -insert(item) adds a new item to the heap. O(log n)
# -findMin() returns the item with the minimum key value, leaving item in the heap.
# -delMin() returns the item with the minimum key value, removing the item from the heap. O(log n)
# -isEmpty() returns true if the heap is empty, false otherwise.
# -size() returns the number of items in the heap.
# -buildHeap(list) builds a new heap from a list of keys.
from pythonds.trees import BinHeap

bh = BinHeap()
bh.insert(5)
bh.insert(7)
bh.insert(3)
bh.insert(11)

print(bh.delMin())  # 3

print(bh.delMin())  # 5

print(bh.delMin())  # 7

print(bh.delMin())  # 11

# A complete binary tree is a tree in which each level has all of its nodes. The exception to this is the bottom level
# of the tree, which we fill in from left to right.