コード例 #1
0
class TestQueues(unittest.TestCase):
    def setUp(self):
        self.Q = Queue(N=5)

    def test_setup(self):
        "Test for Empty Queue"
        self.assertEqual(self.Q.size(), 0)
        self.assertTrue(self.Q.isEmpty())
        self.assertFalse(self.Q.isFull())
        with self.assertRaises(EmptyQueueException) as cm:
            self.Q.dequeue()
        expected_msg = "Queue is empty"
        self.assertEquals(cm.exception.message, expected_msg)
        with self.assertRaises(EmptyQueueException) as cm:
            self.Q.front()
        expected_msg = "Queue is empty"
        self.assertEquals(cm.exception.message, expected_msg)

    def test_enqueue_dequeue(self):
        self.Q.enqueue('A')
        self.Q.enqueue('B')
        self.assertEqual(self.Q.size(), 2)
        self.assertFalse(self.Q.isEmpty())
        self.assertFalse(self.Q.isFull())
        self.Q.enqueue('C')
        self.Q.enqueue('D')
        self.Q.enqueue('E')
        self.assertFalse(self.Q.isEmpty())
        self.assertTrue(self.Q.isFull())
        self.assertEqual(self.Q.front(), 'A')
        with self.assertRaises(FullQueueException) as cm:
            self.Q.enqueue('F')
        expected_msg = "Queue is full"
        self.assertEquals(cm.exception.message, expected_msg)
        self.assertEqual(self.Q.size(), 5)
        self.Q.dequeue()
        self.assertEqual(self.Q.size(), 4)
        self.assertFalse(self.Q.isEmpty())
        self.assertFalse(self.Q.isFull())
        self.Q.enqueue('G')
        self.assertEqual(self.Q.front(), 'B')
        self.assertEqual(self.Q.size(), 5)
        self.Q.dequeue()
        self.Q.dequeue()
        self.Q.dequeue()
        self.Q.dequeue()
        self.assertEqual(self.Q.size(), 1)
        self.assertEqual(self.Q.front(), 'G')
        self.assertFalse(self.Q.isEmpty())
        self.assertFalse(self.Q.isFull())
        self.Q.dequeue()
        self.assertEqual(self.Q.size(), 0)
        with self.assertRaises(EmptyQueueException) as cm:
            self.Q.dequeue()
        expected_msg = "Queue is empty"
        self.assertEquals(cm.exception.message, expected_msg)
        self.assertTrue(self.Q.isEmpty())
        self.assertFalse(self.Q.isFull())
コード例 #2
0
ファイル: trees.py プロジェクト: kaisersakhi/python-algoritms
    def levelOrder(self):
        if self.size > 0:
            queue = Queue()
            queue.enqueue(self.root)

            while not queue.isEmpty():
                print(queue.front().data)
                queue.dequeue()
            pass
        pass
コード例 #3
0
ファイル: trees.py プロジェクト: kaisersakhi/python-algoritms
class BinaryTree:
    root = None
    # size : keeps track of no. of nodes and nothing else
    size = None
    queue = None

    def __init__(self):
        self.size = 0
        self.queue = Queue()

    def add(self, data):
        node = Node(data)
        # self.queue.enqueue(node)
        if self.root is None:
            self.root = node
            self.size += 1
            self.queue.enqueue(self.root)
        elif not self.queue.isEmpty():
            front = self.queue.front()
            if front.lchild is None:
                front.lchild = node
            else:
                front.rchild = node
                self.queue.dequeue()
            self.queue.enqueue(node)
            self.size += 1

    def preOrder(self):
        def r(node):
            if node is None:
                return
            print(node.data)
            r(node.lchild)
            r(node.rchild)
            pass

        r(self.root)

    def levelOrder(self):
        if self.size > 0:
            queue = Queue()
            queue.enqueue(self.root)

            while not queue.isEmpty():
                print(queue.front().data)
                queue.dequeue()
            pass
        pass