Пример #1
0
def test_queue():
    q = Queue([48, 3])
    q.enqueue(10)
    q.enqueue(15)
    q.enqueue(20)
    q.enqueue(25)
    print(q.indexof(25))
    print(q)
    q.clear()
    # q.pop()
    print(q._head, q._tail)
    print(q)
    q.enqueue(25)
    q.dequeue()
    print(q)
Пример #2
0
    def has_backup_path(self):
        for node in self.nodes:
            self.nodes[node].dist = float("inf")
            self.nodes[node].pre = None
            self.nodes[node].pre_door = None

        self.source.dist = 0
        que = Queue()

        que.enqueue(self.source)

        while que.size():
            first = que.dequeue()

            if first == self.exit_node:
                if self.max_exit_dist < first.dist:
                    self.max_exit_dist = first.dist
                return True

            for key in first.connections:
                neighbour = self.nodes[key]
                vertex = first.room + self.delimiter + key

                if self.vertices[vertex].capacity - self.vertices[vertex].flow:
                    if first.dist + 1 < neighbour.dist:
                        neighbour.dist = first.dist + 1
                        neighbour.pre = first
                        neighbour.pre_vertex = self.vertices[vertex]
                        que.enqueue(neighbour)

        return False
Пример #3
0
 def bft(self, starting_point):
     q = Queue()
     visited = []
     q.enqueue(starting_point)
     while q.len() is not 0:
         n = q.dequeue()
         if n not in visited:
             visited.append(n)
             # print(visited)
             for i in self.vertices[f"{n}"]:
                 q.enqueue(f"{i}")
     return visited
Пример #4
0
    def bfs_walk(self):
        que = Queue()
        que.enqueue(next(iter(self.nodes.values())))
        while que.size() > 0:
            first = que.dequeue()
            if first.mark:
                continue
            else:
                print(first.name)
                first.mark = True

            for neighbour in first.connections:
                que.enqueue(self.nodes[neighbour])
Пример #5
0
    def print_level_order(self):
        q = Queue()
        tmp = OrderedDict()
        for index in range(self.get_height(), -2, -1):
            tmp[index] = []

        q.enqueue(self.node)
        level = 0

        while q.size() > 0:
            actual = q.dequeue()

            if actual:
                if level != actual.level:
                    level = actual.level

                tmp[level].append(str(actual.key))

                if actual.left:
                    q.enqueue(actual.left.node)

                if actual.right:
                    q.enqueue(actual.right.node)

                parent = actual

            else:
                if parent:
                    if parent.left.node or parent.right.node:
                        tmp[level - 1].append("_")
                    if not parent.left.node and not parent.right.node:
                        tmp[level - 1].append("_")

                    parent = actual
                else:
                    tmp[level - 1].append("_")

        result = self.format_avl(tmp)
        print(result)
Пример #6
0
# print(lst)
# print(lst.nodes)

# stack = Stack()
# stack.push(4)
# stack.push(5)
# stack.push(6)
# print(stack.pop())
# stack.push(7)
# print(stack)
# print(stack.count)

print('Stack:')
stack = Stack([1, 2, 3])
print(stack)
print(stack.peek())
stack.pop()
print(stack)
print(stack.peek())

print('Queue:')
queue = Queue([1, 2, 3])
print(queue)
print(queue.peek())
queue.dequeue()
print(queue)
print(queue.peek())