def breadthfirst(self): if not self.is_empty(): fringe = ArrayQueue() fringe.enqueue(self.root()) while not fringe.is_empty(): p = fringe.dequeue() yield p for child in self.children(p): fringe.enqueue(child)
def _traverse_level_order_iterative(self, start_node, visit): """Traverse this binary tree with iterative level-order traversal (BFS). Start at the given node and visit each node with the given function. TODO: Running time: ??? Why and under what conditions? TODO: Memory usage: ??? Why and under what conditions?""" # TODO: Create queue to store nodes not yet traversed in level-order queue = ArrayQueue() # TODO: Enqueue given starting node queue.enqueue(start_node) #Loop while queue has nodes while queue.length() > 0: node = queue.dequeue() #dequeue the first node visit(node.data) #visit the dequeue node #enqueue the children of the node if node.left != None: queue.enqueue(node.left) if node.right != None: queue.enqueue(node.right)
def hot_potato_spicy(name_list, num): """ Simulates the Hot potato game "spiced up" with lives and randomness :param name_list: a list containing the name of the participants and their number of lives :param num: the counting constant (e.g., the length of each round of the game) :return: the winner """ def oneRound(): for k in range(num): dequed = Q.dequeue() Q.enqueue(dequed) if random()>0.5: return oneRound() else: roundLoser = Q.first() if lives[roundLoser] > 1: lives[roundLoser] -= 1 else: gameLoser = Q.dequeue() print(f'{gameLoser} has been eliminated from the game!') Q = ArrayQueue() lives = {player[0]: player[1] for player in name_list} for i in name_list: Q.enqueue(i[0]) while Q.__len__() != 1: oneRound() winner = Q.first() return winner
def hot_potato(name_list, num): """ Hot potato simulator. While simulating, the name of the players eliminated should also be printed (Note: you can print more information to see the game unfolding, for instance the list of players to whom the hot potato is passed at each step...) :param name_list: a list containing the name of the players, e.g. ["John", "James", "Alice"] :param num: the counting constant (i.e., the length of each round of the game) :return: the winner (that is, the last player standing after everybody else is eliminated) """ Q = ArrayQueue() for i in name_list: Q.enqueue(i) while Q.__len__() != 1: for k in range(num): dequed = Q.dequeue() Q.enqueue(dequed) removed = Q.dequeue() print(f'{removed} has been removed from the game!') winner = Q.first() return winner
def BFS(G, a): status={} for node in G.nodes(): status[node]='U' nodes=ArrayQueue() nodes.enqueue(a) status[a]='V' while nodes.is_empty()==False: pnode=nodes.dequeue() for node in G.neighbors(pnode): if status[node]=='U': nodes.enqueue(node) status[node]='V' status[pnode]='P' print(pnode) return
def items_level_order(self): """Return a list of all items in this binary search tree found using level-order traversal""" # Create a queue to store nodes not yet traversed in level-order queue = ArrayQueue() # Create an items list items = list() # Enqueue the root node if this tree is not empty if self.is_empty() is False: queue.enqueue(self.root) # Loop until the queue is empty while queue.is_empty() is False: # Dequeue the node at the front of the queue node = queue.dequeue() # Add this node's data to the items list items.append(node.data) # Enqueue this node's left child if it exists if node.left is not None: queue.enqueue(node.left) # Enqueue this node's right child if it exists if node.right is not None: queue.enqueue(node.right) # Return the items list return items
def __init__(self, length): self._queue_first = ArrayQueue(length) self._queue_second = ArrayQueue(length) self._active_queue = self._queue_first self._passive_queue = self._queue_second
def setUp(self): self.queue = ArrayQueue()
class TestArrayQueue(unittest.TestCase): def setUp(self): self.queue = ArrayQueue() def test_enqueue(self): self.queue.enqueue(1) self.queue.enqueue(2) self.queue.enqueue(3) self.assertEqual(self.queue.size, 3) def test_dequeue(self): self.queue.enqueue(1) self.queue.enqueue(2) self.assertEqual(self.queue.dequeue(), 1) def test_peek(self): self.queue.enqueue(1) self.assertEqual(self.queue.peek(), 1) def test_peek_when_stack_is_empty(self): with self.assertRaises(Exception): self.queue.peek() def test_is_empty_when_stack_is_empty(self): self.assertEqual(self.queue.is_empty(), True) def test_is_empty_when_stack_is_not_empty(self): self.queue.enqueue(1) self.assertEqual(self.queue.is_empty(), False) def test_show_all(self): self.queue.show_all()
def __init__(self, *args, **kwargs): super(QueueTest, self).__init__(*args, **kwargs) self.ll_queue = LinkedListQueue() self.arr_queue = ArrayQueue()
class QueueTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(QueueTest, self).__init__(*args, **kwargs) self.ll_queue = LinkedListQueue() self.arr_queue = ArrayQueue() def test_ll_queue_enqueue(self): self.ll_queue.enqueue(10) self.ll_queue.enqueue(20) self.ll_queue.enqueue(30) self.ll_queue.enqueue(40) self.assertEqual([i for i in self.ll_queue], [10, 20, 30, 40]) self.ll_queue.enqueue(50) self.assertEqual([i for i in self.ll_queue], [10, 20, 30, 40, 50]) def test_ll_queue_dequeue(self): self.ll_queue.enqueue(10) self.ll_queue.enqueue(20) self.ll_queue.enqueue(30) self.ll_queue.enqueue(40) self.assertEqual(self.ll_queue.dequeue(), 10) self.assertEqual([i for i in self.ll_queue], [20, 30, 40]) self.assertEqual(self.ll_queue.dequeue(), 20) self.assertEqual(self.ll_queue.dequeue(), 30) self.assertEqual(self.ll_queue.dequeue(), 40) self.assertRaises(IndexError, self.ll_queue.dequeue) self.ll_queue.enqueue(10) self.ll_queue.enqueue(20) self.assertEqual(self.ll_queue.dequeue(), 10) def test_arr_queue_enqueue(self): self.arr_queue.enqueue(10) self.arr_queue.enqueue(20) self.arr_queue.enqueue(30) self.arr_queue.enqueue(40) self.assertEqual([i for i in self.arr_queue], [10, 20, 30, 40]) self.arr_queue.enqueue(50) self.assertEqual([i for i in self.arr_queue], [10, 20, 30, 40, 50]) self.arr_queue.enqueue(60) self.arr_queue.enqueue(70) self.assertEqual([i for i in self.arr_queue], [10, 20, 30, 40, 50, 60, 70]) self.arr_queue.enqueue(80) self.arr_queue.enqueue(90) self.arr_queue.enqueue(100) self.arr_queue.enqueue(110) self.arr_queue.enqueue(120) self.arr_queue.enqueue(130) self.assertEqual( [i for i in self.arr_queue], [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130]) def test_arr_queue_dequeue(self): self.arr_queue.enqueue(10) self.arr_queue.enqueue(20) self.arr_queue.enqueue(30) self.arr_queue.enqueue(40) self.assertEqual(self.arr_queue.dequeue(), 10) self.assertEqual([i for i in self.arr_queue], [20, 30, 40]) self.assertEqual(self.arr_queue.dequeue(), 20) self.assertEqual(self.arr_queue.dequeue(), 30) self.assertEqual(self.arr_queue.dequeue(), 40) self.assertRaises(IndexError, self.arr_queue.dequeue) self.arr_queue.enqueue(10) self.arr_queue.enqueue(20) self.assertEqual(self.arr_queue.dequeue(), 10) self.arr_queue.enqueue(30) self.arr_queue.enqueue(40) self.assertEqual([i for i in self.arr_queue], [20, 30, 40])
def _traverse_level_order_iterative(self, start_node, visit): """Traverse this binary tree with iterative level-order traversal (BFS). Start at the given node and visit each node with the given function. TODO: Running time: on each node we perform 3 operations, assumming each operations on each node take O(1) it takes 3 * O(n) for the tree to be traversed TODO: Memory usage: O(2^h) would be the worst running time if we have two children for every parent, but that might not be the case. Considering that a parent might have one child O(n) would be a better estimate.""" # TODO: Create queue to store nodes not yet traversed in level-order q = ArrayQueue() # TODO: Enqueue given starting node q.enqueue(start_node) # TODO: Loop until queue is empty while q.length() != 0: # TODO: Dequeue node at front of queue q.front() node = q.dequeue() # TODO: Visit this node's data with given function visit(node.data) # TODO: Enqueue this node's left child, if it exists if node.left: q.enqueue(node.left) # TODO: Enqueue this node's right child, if it exists if node.right: q.enqueue(node.right)
from queue import ArrayQueue q = ArrayQueue() data_entry = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] for data in data_entry: q.enqueue(data) # test to see if the length of the data array is double print(len(q.data)) data_recptr = [] for n in range(9): data_recptr.append(q.dequeue()) print(data_recptr) # the size of the queue shrinks to less than 1/4, # accordingly, the size of the array shrinks to half print(len(q.data))
def _traverse_level_order_iterative(self, start_node, visit): """Traverse this binary tree with iterative level-order traversal (BFS). Start at the given node and visit each node with the given function. TODO: Running time: ??? Why and under what conditions? TODO: Memory usage: ??? Why and under what conditions?""" # TODO: Create queue to store nodes not yet traversed in level-order queue = ArrayQueue() # TODO: Enqueue given starting node queue.enqueue(start_node) # TODO: Loop until queue is empty while queue.length() != 0: # TODO: Dequeue node at front of queue node = queue.front() queue.dequeue() # TODO: Visit this node's data with given function visit(node.data) # TODO: Enqueue this node's left child, if it exists if node.left is not None: queue.enqueue(node.left) # TODO: Enqueue this node's right child, if it exists if node.right is not None: queue.enqueue(node.right)