def dft_print(self, node): # If there is a node if node: # Save the current node in current current = node # Create a stack my_stack = Stack() # While (current is not None) or (my_stack.len is not 0) while current or my_stack.len() != 0: # if current is not empty if current: # Add current to the stack ( push ) my_stack.push(current) # print the value of current print(current.value) # Make current.left the new current node # So we can keep going left current = current.left # Else: If current is empty else: # pop from the stack the last node added # Save that node in popped_node popped_node = my_stack.pop() # Save popped_node.right as current # So we can check if popped_node.right is # empty in the next while loop current = popped_node.right
def dft_print(self, node): to_print = Stack() to_print.push(node) while to_print.len() > 0: popped_node = to_print.pop() print(popped_node.value) popped_node.right and to_print.push(popped_node.right) popped_node.left and to_print.push(popped_node.left)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: current = stack.pop() print(current.value) current.left and stack.push(current.left) current.right and stack.push(current.right)
def dft_print(self, node): stack = Stack() temp_node = node while temp_node: print(temp_node.value) if temp_node.left: stack.push(temp_node.left) if temp_node.right: stack.push(temp_node.right) temp_node = stack.pop()
def dft_print(self, node): theStack = Stack() currentNode = node while currentNode: print(currentNode.value) if currentNode.right: theStack.push(currentNode.right) if currentNode.left: theStack.push(currentNode.left) currentNode = theStack.pop()
def dft_print(self, node): storage = Stack() current = node while current: print(current.value) if current.right: storage.push(current.right) if current.left: storage.push(current.left) current = storage.pop()
def dft_print(self, node): stack = Stack() curr = node while curr: print(curr.value) if curr.left: stack.push(curr.left) if curr.right: stack.push(curr.right) curr = stack.pop()
def dft_print(self, node): stack = Stack() current = node while True: if current is not None: stack.push(current) current = current.left elif stack: current = stack.pop() print(current) current = current.right else: break
def dft_print(self, node): my_S = Stack() my_S.push(node) counter = 0 while my_S.storage.head is not None or counter == 0: pushes = [] if my_S.storage.head.value.left is not None: pushes.append(my_S.storage.head.value.left) if my_S.storage.head.value.right is not None: pushes.append(my_S.storage.head.value.right) print(my_S.pop().value) for p in pushes: my_S.push(p) counter += 1
def post_order_dft(self, node): order_stack = Stack() order_stack.push(node) print_stack = Stack() while order_stack.len() > 0: current_node = order_stack.pop() print_stack.push(current_node) if current_node.left: order_stack.push(current_node.left) if current_node.right: order_stack.push(current_node.right) while print_stack.len() > 0: print(print_stack.pop().value) # test_bst = BinarySearchTree(1) # test_bst.insert(8) # test_bst.insert(5) # test_bst.insert(7) # test_bst.insert(6) # test_bst.insert(3) # test_bst.insert(4) # test_bst.insert(2) # test_bst.in_order_print(test_bst) # test_bst.bft_print(test_bst)
def in_order_print(self, node): #Recursive stack = Stack() stack.push(node) while stack.len() > 0: current_node = stack.pop() if current_node.left is None and current_node.right is None: print(current_node.value) else: if current_node.left: current_node.in_order_print(current_node.left) print(current_node.value) if current_node.right: current_node.in_order_print(current_node.right)
def dft_print(self, node): s = Stack() while node is not None: print(node.value) # Stick all of the node's children in the end of the stack. if node.left: s.push(node.left) if node.right: s.push(node.right) if s.len() > 0: # Get the last node in the stack and continue the loop with it. node = s.pop() else: break return
def dft_print(self, node): # node is the tracker my_stack = Stack() while my_stack.len() > 0 or node: # if our tracker exists push it to stack and reassign it to the left if node: print(node) my_stack.push(node) node = node.left # if our tracker is null pop off from stack and use it for the tracker to visit the right else: node = my_stack.pop() node = node.right
def dft_print(self, node): node_stack = Stack() node_stack.push(node) while node: print(node.value) if node.left: node = node.left if node.right: node_stack.push(node) else: popped_node = node_stack.pop() if popped_node: node = popped_node.right else: node = None
def for_each(self, cb): # the root node is self node = self my_stack = Stack() while my_stack.len() > 0 or node: # top = my_stack.pop() if node: my_stack.push(node) node = node.left else: node = my_stack.pop() cb(node.value) node = node.right pass
def dft_print(self, node): # I missed the iterative part, here it is recursively :( # if node == None: # return # print(node.value) # self.dft_print(node.right) # self.dft_print(node.left) #*********************************************************# storage = Stack() while True: if node == None and storage.len() == 0: break elif node == None: node = storage.pop().left else: print(node.value) storage.push(node) node = node.right
def dft_print(self, node): # create stack stack = Stack() # add root to stack current_node = node # while stack is not empty while current_node: # DO THE THING!!! (print) print(current_node.value) # add children of node to stack if current_node.left: stack.push(current_node.left) if current_node.right: stack.push(current_node.right) # node = pop top of stack current_node = stack.pop()
def dft_print(self, node): # Create an empty stack s = Stack() # Add the starting node to the stack current_node = s.push(node) # Iterate over the stack while s.len() > 0: # set the current_node to the first item in the stack current_node = s.pop() # the print the current value print(current_node.value) # if the current node has a left child if current_node.left: # call push on the current left s.push(current_node.left) # if the current node has a right child if current_node.right: # call push on the current right s.push(current_node.right)
def get_max(self): node = self max_ = 0 if node: max_ = node.value my_stack = Stack() while my_stack.len() > 0 or node: # top = my_stack.pop() if node: my_stack.push(node) node = node.left else: node = my_stack.pop() # print(node.value) if (node.value > max_): max_ = node.value node = node.right return max_
def for_each(self, cb): # if not self.value: # return None # cb(self.value) # if self.left: # self.left.for_each(cb) # if self.right: # self.right.for_each(cb) # cb(self.value): # if self.left: # self.left.for_each(cb) # if self.right: # seelf.right.for_each(cb) stack = Stack() stack.push(self) while stack.len() > 0: current_node = stack.pop() if current_node.right: stack.push(current_node.right) if current_node.left: stack.push(current_node.left) cb(current_node.value)
def dft_print(self, node): """ Iterative approach notes from lecture Make a stack add root to stack while stack.length > 0 immediately pop root and save to a temp var (temp = stack.pop()) Do the thing if temp.left add to stack if temp.right add to stack """ # instantiating stack level_stack = Stack() # pushing initial node to stack level_stack.push(node) # while something is in the stack while level_stack.len() > 0: # doing this after when enter loop, otherwise it will be 0 and it won't start the loop current_node = level_stack.pop() print(current_node.value) # if there is a node to the right, push that onto the stack if current_node.right: level_stack.push(current_node.right) # if there is a node to the left, push that onto the stack if current_node.left: level_stack.push(current_node.left)
def dft_print(self, node=None): my_stack = Stack() if node == None: current_node = self else: current_node = node my_stack.push(current_node) while my_stack.len() > 0: current_node = my_stack.pop() print(current_node.value) if current_node.right: # exists: my_stack.push(current_node.right) if current_node.left: my_stack.push(current_node.left) # put self on stack # current = pop from stack # print current # put current.right on stack # put current.left on stack. # repeat # # use a stack pass
def dft_print(self, node): #PLAN #create/initialize stack #add/push root to stack #while stack is not empty #pop head/top item out of stack & into temp variable [node = pop head of stack] #if temp exists # if there is temp var on the right #put that into the stack on the right # if there is a left temp var #put that into the stack on the left # DO the THING!... print temp value # else break stack = Stack() stack.push(node) while stack.len() > 0: temp: BinarySearchTree = stack.pop() if temp: if temp.right: stack.push(temp.right) if temp.left: stack.push(temp.left) print(temp.value) else: break
def dft_print(self, node): s = Stack() s.push(node) while(len(s)!=0): popped = s.pop() print(popped.value) if popped.left: s.push(popped.left) if popped.right: s.push(popped.right)
def dft_print(self, node): s = Stack() s.push(node) while s.len() != 0: node = s.pop() print(node.value) if node.left: s.push(node.left) if node.right: s.push(node.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: temp = stack.pop() print(temp) if temp.left: stack.push(temp.left) if temp.right: stack.push(temp.right)
def dft_print(self, node): stack = Stack() stack.push(self) while stack.len() > 0: node = stack.pop() print(node.value) if node.left: stack.push(node.left) if node.right: stack.push(node.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.size > 0: curr_node = stack.pop() print(curr_node.value) if curr_node.left: stack.push(curr_node.left) if curr_node.right: stack.push(curr_node.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len(): current_node = stack.pop() print(current_node.value) # d if current_node.left: stack.push(current_node.left) # l if current_node.right: stack.push(current_node.right) # r
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: current_pop = stack.pop() print(current_pop.value) if current_pop.left: stack.push(current_pop.left) if current_pop.right: stack.push(current_pop.right)