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: O(N) Why and under what conditions? TODO: Memory usage: O(N) Why and under what conditions?""" # Traverse pre-order without using recursion (stretch challenge) # [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15] queue = DeQueue() queue.enqueue_front(node)
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. Running time: O(n) Why and under what conditions? Memory usage: O(n) Why and under what conditions?""" # Create queue to store nodes not yet traversed in level-order """Remove and return the item at the back of this queue,""" queue = DeQueue() queue.enqueue_front(start_node) while queue.is_empty() == False: node = queue.dequeue_front() visit(node.data) if node.left != None: queue.enqueue_back(node.left) if node.right != None: queue.enqueue_back(node.right)
def LevelWiseBtree(r): q = Queue() EnQueue(q, r) while not IsEmptyQueue(q): t = DeQueue(q) print(t.data) if t.left is not None: EnQueue(q, t.left) if t.right is not None: EnQueue(q, t.right)
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. Running time: o(n) Memory usage: Based on size of level, so n/2? """ # Create queue to store nodes not yet traversed in level-order queue = DeQueue() # Enqueue given starting node queue.enqueue_back(start_node) # Loop until queue is empty while queue.length() > 0: # Dequeue node at front of queue node = queue.dequeue_front() # Visit this node's data with given function visit(node.data) # Enqueue this node's left child, if it exists if node.left: queue.enqueue_back(node.left) # Enqueue this node's right child, if it exists if node.right: queue.enqueue_back(node.right)
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. Running time: O(n forever man) Memory usage: Depends on a diagonal; no clue how to calculate this""" # Traverse pre-order without using recursion (stretch challenge) # [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15] stack = DeQueue() stack.enqueue_front(node) # I'm going to need a blackboard for this # while stack.length() > 0: # while node.left is not None: # stack.enqueue_front(node) # node = node.left # visit.append(node.data) # else: # node = stack.dequeue_front() # if node.right: # node = node.right # visit.append(node.data) # # Above was close, but kept printing the root's right at the end! while stack.length() > 0: # Keep popping the first thing that shows up! node = stack.dequeue_front() visit(node.data) # We need to make sure we add the right first before left # This is to make sure the leftmost is stacked/printed last if node.right: stack.enqueue_front(node.right) if node.left: stack.enqueue_front(node.left) # Because of this order, this prints what's IMMEDIATELY left # then go deeper left before going right return
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 have to go through all of the nodes anyway Memory usage: O(height?) We do queue all of the left node""" stack = DeQueue() stack.enqueue_front(node) # I'm going to need a blackboard for this while stack.length() > 0: # Go as left as possible! while node.left is not None: stack.enqueue_front(node.left) node = node.left # If there's no more left, let's pop something + append else: node = stack.dequeue_front() visit(node.data) # Check right and eventually see if it has lefts # If it doesn't have a left, we skip the above while loop if node.right: stack.enqueue_front(node.right) node = node.right return
def BalancedBTreeCreate(tree): flag=0 q=Queue() t=Node(tree[0]) root=t for data in tree[1:]: if root.left is None: root.left=Node(data) flag+=1 EnQueue(q,root.left) else: if root.right is None: root.right=Node(data) flag+=1 EnQueue(q,root.right) if flag is 2: flag=0 root=DeQueue(q) return t
def test_enqueue(self): q = DeQueue() q.enqueue_back('B') assert q.front() == 'B' assert q.length() == 1 q.enqueue_back('C') assert q.front() == 'B' assert q.length() == 2 q.enqueue_front('A') assert q.front() == 'A' assert q.length() == 3 assert q.is_empty() is False
def test_length(self): q = DeQueue() assert q.length() == 0 q.enqueue_back('A') assert q.length() == 1 q.enqueue_front('B') assert q.length() == 2 q.dequeue_front() assert q.length() == 1 q.dequeue_back() assert q.length() == 0
def test_init_with_list(self): q = DeQueue(['A', 'B', 'C']) assert q.front() == 'A' assert q.length() == 3 assert q.is_empty() is False
def test_init(self): q = DeQueue() assert q.front() is None assert q.length() == 0 assert q.is_empty() is True
def test_dequeue(self): q = DeQueue(['A', 'B', 'C']) assert q.dequeue_front() == 'A' assert q.length() == 2 assert q.dequeue_back() == 'C' assert q.length() == 1 assert q.dequeue_back() == 'B' assert q.length() == 0 assert q.is_empty() is True with self.assertRaises(ValueError): q.dequeue_front()
def test_front_back(self): q = DeQueue() assert q.front() is None q.enqueue_back('A') assert q.front() == 'A' q.enqueue_back('B') assert q.front() == 'A' q.dequeue_front() assert q.front() == 'B' q.dequeue_back() q.enqueue_front('C') assert q.front() == 'C' q.enqueue_front('B') assert q.front() == 'B' q.enqueue_front('A') assert q.front() == 'A' assert q.back() == 'C' q.dequeue_front() assert q.front() == 'B' assert q.back() == 'C' q.dequeue_front() q.dequeue_front() assert q.front() is None assert q.back() is None
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: Half of the tree. It goes down one half before another """ # Traverse post-order without using recursion (stretch challenge) stack = DeQueue() # while (stack.length() > 0) or (node): # if node: # stack.enqueue_front(node) # node = node.left # else: # check = stack.front() # if (check.right is not None) and (parent is not check.right): # node = check.right # else: # visit.append(check.data) # parent = stack.dequeue_front() # # # # # # # # # # # # # # # # # # # # # # # while (stack.length() > 0): # print(stack.list) # while node.right: # if node.right: # stack.enqueue_front(node.right) # node = node.right # else: # if stack.front().left: # node = stack.front().left # else: # # # # # # # # # # # # # # # # # # while(stack.length() > 0): # print(visit) # while node.left: # stack.enqueue_front(node.right) # stack.enqueue_front(node.left) # node = node.left # else: # node = stack.dequeue_front() # if node not in visit: # visit.append(node) # check = stack.front() # if (check.right is not None): # # check.right not in visit: # if check.left: # stack.enqueue_front(stack.front().left) # stack.enqueue_front(stack.front().right) # # # # # # # # # # # # # # Okay, last one # This wasn't meant to work # While it's not null # My usual "whiles" don't work # It was this, or make sure visit's len is same as the tree while True: # If the node is valid, grab right and left # Thanks Alan while node: if node.right: stack.enqueue_front(node.right) stack.enqueue_front(node) node = node.left # If it's null, start dequeues else: node = stack.dequeue_front() # I don't even care at this point, just move the stack around # Originally, it would be left, parent, right; # needed left, right, node if node.right and node.right == stack.front(): stack.dequeue_front() stack.enqueue_front(node) node = node.right else: # Put that in the visit. It's ALWAYS going to do this # as a last case scenario visit(node.data) node = None if stack.length() == 0: break # print(stack.list, visit, "\n\n") return visit
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: Half of the tree. It goes down one half before another """ # Traverse post-order without using recursion (stretch challenge) stack = DeQueue() # It was this, or make sure visit's len is same as the tree while True: # If the node is valid, grab right and left while node: if node.right: stack.enqueue_front(node.right) stack.enqueue_front(node) node = node.left # If it's null, start dequeues else: node = stack.dequeue_front() # I don't even care at this point, just move the stack around # Originally, it would be left, parent, right; # needed left, right, node if node.right and node.right == stack.front(): stack.dequeue_front() stack.enqueue_front(node) node = node.right else: # Put that in the visit. It's ALWAYS going to do this # as a last case scenario visit(node.data) node = None if stack.length() == 0: break # print(stack.list, visit, "\n\n") return visit