def test_peek(self): s = Stack() assert s.peek() is None s.push('A') assert s.peek() == 'A' s.push('B') assert s.peek() == 'B' s.pop() assert s.peek() == 'A' s.pop() assert s.peek() is None
def test_push(self): s = Stack() s.push('A') assert s.peek() == 'A' assert s.length() == 1 s.push('B') assert s.peek() == 'B' assert s.length() == 2 s.push('C') assert s.peek() == 'C' assert s.length() == 3 assert s.is_empty() is False
def _traverse_post_order_iterative(self, node, visit): """Traverse this binary tree with iterative post-order traversal (DFS). Start at the given node and visit each node with the given function. Running time: O(n) Memory usage: O(n) Credit to Nicolai and Ryan for solving what I began""" # Traverse post-order without using recursion (stretch challenge) traversed = set() # Create queue to store nodes not yet traversed stack = LinkedStack() # Enqueue root node as last stack.push(node) while not stack.is_empty(): node = stack.peek() if node.right and node.right not in traversed: stack.push(node.right) if node.left and node.left not in traversed: stack.push(node.left) if ( node.is_leaf() or node.left is None and node.right in traversed or node.left in traversed and node.right is None or node.left in traversed and node.right in traversed ): node = stack.pop() visit(node.data) traversed.add(node)
def reverse(nums): #declare your stack Stack = LinkedStack() #init the stack #declare a variable to get track of the reversed number reversed = [] #add each number in nums to the stack for num in nums: # Loop through the whole list O(n) if num == '-': reversed.append(num) #adds sign to list O(1) if num.isdigit(): Stack.push(num) #adds num to stack O(1) #traverse through the stack while Stack.length() > 0: # Loops through the whole stack #add the head of the stack then delete the head reversed.append(Stack.peek()) # adds number back to list O(1) Stack.pop() # deletes current stack to move to the next one O(1) if reversed[0] == '-': if reversed[1] == '0': reversed.pop(1) #removes a digit O(1) if reversed[0] == '0': reversed.pop(0) #removes a digit O(1) return "".join(reversed)
def _traverse_in_order_iterative(self, node, visit): """Traverse this binary tree with iterative in-order traversal (DFS). Start at the given node and visit each node with the given function. Running time: O(n) we are visiting each node Memory usage: O(n) creating a stack with number of nodes in the tree""" stack = LinkedStack() stack.push(node) # push the root node while not stack.is_empty(): if (stack.peek().left != None): # if node has left child stack.push(stack.peek().left) else: node = stack.pop() visit(node.data) if (not stack.is_empty()): node = stack.pop() visit(node.data) if (node.right is not None): stack.push(node.right)
def _traverse_post_order_iterative(self, node, visit): """Traverse this binary tree with iterative post-order traversal (DFS). Start at the given node and visit each node with the given function. TODO: Running time: O(n) because every node is visited TODO: Memory usage: O(log n) we are creating a stack that will hold at most the same number of nodes as height in tree """ # stack to keep track of nodes in order stack = LinkedStack() # done keeps track of if done traversing # controls while loop done = False # set current node to starting node current = node # traverse nodes while stack is not empty while not done: # while current node is something while current: # if right child is there push onto stack if current.right is not None: stack.push(current.right) # push current node onto stack (after right children) stack.push(current) # grab left child of root current = current.left # Current node is none pop node from top of stack current = stack.pop() # check if the node has a right child that hasn't # been checkout yet then push onto stack before current node if current.right is not None and stack.peek() == current.right: # pop child node from stack stack.pop() # push current node onto stack stack.push(current) current = current.right # now set current to right child else: # visit the node visit(current.data) # set current node to None current = None if stack.is_empty(): # stack is empty we are done traversing done = True
def _traverse_pre_order_iterative(self, node, visit): """Traverse this binary tree with iterative pre-order traversal (DFS). 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?""" stack = LinkedStack() stack.push(node) while stack.is_empty() is False: peeked = stack.peek() visit(peeked.data) # add to stack while node.left exist if node.left is not None: # add left, or smaller, node on top stack.push(node.left) # iterate left tree node = node.left # begin iterating right tree else: """once the left tree of node is None, we can begin popping and iterating the right side""" while True and stack.is_empty() is False: if node.right is None: node = stack.pop() else: # iterate the right node since the left node is now empty stack.push(node.right) node = node.right break
def _traverse_in_order_iterative(self): """Traverse this binary tree with iterative in-order traversal (DFS). 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: Traverse in-order without using recursion (stretch challenge) items = [] stack = LinkedStack() node = self.root stack.push(node) while stack.is_empty() == False: if node.is_leaf(): items.append(node.data) stack.pop() if stack.is_empty() == False: items.append(stack.peek().data) node = stack.pop().right stack.push(node) else: if node.left is not None: node = node.left stack.push(node) return items
def test_init(self): s = Stack() assert s.peek() is None assert s.length() == 0 assert s.is_empty() is True
def test_init_with_list(self): s = Stack(['A', 'B', 'C']) assert s.peek() == 'C' assert s.length() == 3 assert s.is_empty() is False