Exemplo n.º 1
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
Exemplo n.º 2
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])
Exemplo n.º 3
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)