Exemplo n.º 1
0
 def levelorder(self):
     items = []
     queue = Queue()
     node = self._root  # starting from the root
     queue.insert(node)  # insert it
     while not queue.is_empty():
         node = queue.remove()  # pick that node
         items.append(node._data)  # add to the list
         # if the left side is not None, there exist a left side, add the queue (going left to right_
         if node._left is not None:
             queue.insert(node._left)
         # you also want to add the right side
         if node._right is not None:
             queue.insert(node._right)
     return items
Exemplo n.º 2
0
    def __str__(self):
        """
        -------------------------------------------------------
        Description:
            Returns a string representation of bst
            Uses Breadth First Search (BFS)
        Assert: none
        Use: str(bst) or print(bst)
        -------------------------------------------------------
        Parameters:
            None
        Returns:
            output: string representation of bst (str)
        -------------------------------------------------------
        """
        q = Queue(self.MAX_SIZE)
        output = ''

        q.insert(self._root)
        level = 1

        while not q.is_empty():
            node = q.remove()

            if node is None:
                continue

            node_level = self.level(node._data)

            if node_level == level:
                output += '{} '.format(str(node._data))

            elif node_level > level:
                output = '{}\n'.format(output[:-1])
                output += '{} '.format(str(node._data))
                level += 1

            if node._left is not None:
                q.insert(node._left)

            if node._right is not None:
                q.insert(node._right)

        return output[:-1]
Exemplo n.º 3
0
    def _reflect_vertical_aux(self, node):
        queue = Queue()
        tree = BST()

        if node is None:
            return tree

        if node is not None:
            queue.insert(node)
        while not queue.is_empty():
            temp = node._left
            node._left = node._right
            node._right = temp
            node = queue.remove()
            tree.insert(node._data)
            if node._left is not None:
                queue.insert(node._left)
            if node._right is not None:
                queue.insert(node._right)

        return tree
Exemplo n.º 4
0
 def __str__(self):
     """
     -------------------------------------------------------
     Description:
         Returns a string representation of bst
         Uses Breadth First Search (BFS)
     Assert: none
     Use: str(bst) or print(bst)
     -------------------------------------------------------
     Parameters:
         None
     Returns:
         output: string representation of bst (str)
     -------------------------------------------------------
     """
     # You can all the way down print the depth (DFS depth first search) --> Stacks
     # Horizontal search, reads horizontally to prints (BFS breath first search) --> Queues
     q = Queue(self.MAX_SIZE)
     output = ''
     q.insert(self._root)
     level = 1
     # come back to this in WEEK #11
     while not q.is_empty():
         node = q.remove()
         if node is None:
             continue
         node_level = self.level(node._data)
         if node_level == level:
             output += str(node._data) + ' '
         elif node_level > level:
             output = output[:-1] + '\n'
             output += str(node._data) + ' '
             level += 1
         if node._left is not None:
             q.insert(node._left)
         if node._right is not None:
             q.insert(node._right)
     return output[:-1]
Exemplo n.º 5
0
def reverse_stack(stack):
    """
    -------------------------------------------------------
    Description:
        Reverse a stack using a queue
    Use: reverse_stack(stack)
    -------------------------------------------------------
    Parameters:
        stack - a stack of items (Stack)
    Returns:
        No returns
    -------------------------------------------------------
    """
    queue = Queue()
    
    while not stack.is_empty():
        item = deepcopy(stack.pop())
        queue.insert(item)
    
    while not queue.is_empty():
        stack.push(queue.remove())

    return
Exemplo n.º 6
0
print(lis.pop_front())
print("container of size: " + str(lis.get_size()) + ":")
print(lis)
print(lis.pop_front())
print(lis.pop_front())
print("container of size: " + str(lis.get_size()) + ":")
print(lis)

print("\nTESTING QUEUE WITH ARRAYS\n")

queue = Queue("array")
queue.add(2)
queue.add(4)
queue.add(7)
print("the data structure is of size: " + str(queue.get_size()))
print(queue.remove())
print(queue.remove())
print(queue.remove())
print(queue.remove())
print("the data structure is of size: " + str(queue.get_size()))

print("\nTESTING STACK WITH ARRAYS\n")

stack = Stack("array")
stack.push(2)
stack.push(4)
stack.push(7)
print("the data structure is of size: " + str(stack.get_size()))
print(stack.pop())
print(stack.pop())
print(stack.pop())
Exemplo n.º 7
0
result = q.insert(-1)
assert (result == True)
assert (not q.is_empty())
assert (q.__str__() == "-1 7 5")

result = q.insert(20)
assert (result == False)
assert (not q.is_empty())
assert (q.__str__() == "-1 7 5")

result = q.insert(33)
assert (result == False)
assert (not q.is_empty())
assert (q.__str__() == "-1 7 5")

x = q.remove()
assert (not q.is_empty())
assert (q.__str__() == "-1 7")
assert (x == 5)

x = q.remove()
assert (not q.is_empty())
assert (q.__str__() == "-1")
assert (x == 7)

q.insert(11)
assert (not q.is_empty())
assert (q.__str__() == "11 -1")

x = q.remove()
assert (not q.is_empty())