def test_enqueue(self): queue = Queue() tests = [1, '0', Queue, lambda x: x, {}, [], None] for test in tests: queue.enqueue(test) assert len(tests) == len(queue)
def test_size(self): queue = Queue() assert len(queue) == 0 queue.enqueue(1) assert len(queue) == 1 queue.dequeue() assert len(queue) == 0
def test_is_empty(self): queue = Queue() assert queue.is_empty() is True queue.enqueue(1) assert queue.is_empty() is False queue.dequeue() assert queue.is_empty() is True
def test_queue_as_string(self): queue = Queue() assert str(queue) == '' queue.enqueue(3) queue.enqueue(1) queue.enqueue(2) assert str(queue) == '3 -> 1 -> 2'
def traverse_level_order(self): queue = Queue() queue.enqueue(self) arr = [] while (not queue.is_empty()): current = queue.dequeue() arr.append(current.data) for child in [current.left, current.right]: if (child): queue.enqueue(child) return arr
def test_dequeue(self): queue = Queue() with self.assertRaises(QueueDequeueException): queue.dequeue() one = 1 two = 2 queue.enqueue(one) queue.enqueue(two) assert len(queue) == 2 assert queue.dequeue() == one assert queue.dequeue() == two assert len(queue) == 0
def find_ladders(beginWord, endWord, wordList): """ :type beginWord: str :type endWord: str :type wordList: List[str] :rtype: List[List[str]] """ # ensure we add the beginWord to the graph graph_words = [beginWord] + wordList graph = Graph.from_same_length_word_list(graph_words) # to trace our path back when we find the target parent_of = defaultdict(lambda: []) depth = defaultdict(lambda: math.inf) depth[beginWord] = 0 queue = Queue() queue.enqueue(beginWord) success = False while (not queue.is_empty()): current = queue.dequeue() if (current == endWord): success = True for child in graph.get_neighbors(current): not_encountered_yet = depth[child] == math.inf already_encountered_at_same_depth = depth[ child] == depth[current] + 1 if (not_encountered_yet): depth[child] = depth[current] + 1 parent_of[child].append(current) queue.enqueue(child) elif (already_encountered_at_same_depth): parent_of[child].append(current) if success: all_paths = DoublyLinkedList() current_path = DoublyLinkedList() dfs(parent_of, endWord, all_paths, current_path) return all_paths.to_array_from_head() else: return []
def construct_from_list(self, list): """ Creates a binary tree from a level-ordered list of values. Empty children for existing nodes should be specified with None. e.g. [4,7,3,None,None,1] yields a tree of shape: 4 / \ 7 3 / 1 """ queue = Queue() root = self.__node_for_construction(list[0]) current = root for data in list[1:]: new = self.__node_for_construction(data) if new is not None: queue.enqueue(new) if (current.left == self.__unassigned): current.left = new elif (current.right == self.__unassigned): current.right = new else: # current node is full current = queue.dequeue() current.left = new # clean up (ensure unassigned values are represented as None) queue = Queue() queue.enqueue(root) while (not queue.is_empty()): current = queue.dequeue() if (current.left == self.__unassigned): current.left = None elif (current.left): queue.enqueue(current.left) if (current.right == self.__unassigned): current.right = None elif (current.right): queue.enqueue(current.right) return root