Ejemplo n.º 1
0
    def findInsertLocation(self):
        if self.isEmpty():
            return self.root
        q = myQueue(LinkedList.Node(self.root))

        while q.is_empty() == False:
            n: BinarySearchTree.Node = q.dequeue().data
            if n.hasLeft() and n.hasRight():
                q.enqueue(LinkedList.Node(n.left))
                q.enqueue(LinkedList.Node(n.right))
            else:
                return n
    def bfs(self, startVertex, main_surface):
        q = myQueue()
        q.enqueue(LinkedList.Node(startVertex))
        startVertex.visited = True

        while not q.is_empty():
            v = q.dequeue().data
            neighbours = gg.neighbours(v)
            for n in neighbours:
                if not n.visited:
                    n.visited = True
                    q.enqueue(LinkedList.Node(n))
            showGrid(main_surface, self)
def checkValidity(inp: str):
    s = Stack()
    for i in inp:
        if i in openings:
            s.push(LinkedList.Node(i))
        elif i in closings:
            if s.is_empty():
                return False
            if getOpposite(i) != s.pop().data:
                return False
    if s.is_empty():
        return True
    else:
        return False
            frame_rate = 500 / (t1 - t0)
            t0 = t1

        main_surface.fill((255, 255, 255))
        if toggleFrameRateDisplay:
            the_text = my_font.render(
                "Frame = {0}, rate = {1:.2f} fps".format(
                    frame_count, frame_rate), True, (0, 0, 0))
            main_surface.blit(the_text, (10, 30))
        if start:
            startVertex = gg.vertices.head
            for i in range(SQUARE_SIZE * GRID_VERTEX_SIZE + GRID_VERTEX_SIZE):
                startVertex = startVertex.next

            q = myQueue()
            q.enqueue(LinkedList.Node(startVertex))
            startVertex.visited = True

            while not q.is_empty():
                v = q.dequeue().data
                neighbours = gg.neighbours(v)
                for n in neighbours:
                    if not n.visited:
                        n.visited = True
                        q.enqueue(LinkedList.Node(n))
                        main_surface.fill((255, 255, 255))
                showGrid(main_surface, gg)
                # pygame.time.delay(1)
                pygame.display.update()
                # fpsClock.tick(FPS)
        elif i in closings:
            if s.is_empty():
                return False
            if getOpposite(i) != s.pop().data:
                return False
    if s.is_empty():
        return True
    else:
        return False


if __name__ == "__main__":

    a = '[2+((3*4)+{2-4})][]'
    print(checkValidity(a))
    exit()
    s = Stack(None, None)
    s.display()
    s.push(LinkedList.Node(12, None))
    s.push(LinkedList.Node(7, None))
    s.push(LinkedList.Node(79, None))
    s.display()
    print('\nPopped: {}'.format(s.pop()))
    s.display()
    print('\n')
    print('\nPopped: {}'.format(s.pop()))
    s.display()
    print('\n')
    print('\nPopped: {}'.format(s.pop()))
    s.display()
    print('\n')
Ejemplo n.º 6
0

class myQueue(LinkedList):
    def __init__(self,
                 head: LinkedList.Node = None,
                 tail: LinkedList.Node = None):
        super().__init__(head, tail)

    def enqueue(self, node: LinkedList.Node):
        self.insert_last(node)

    def dequeue(self):
        return self.remove_first()

    def peek_front(self):
        return self.peek_first()

    def peek_back(self):
        return self.peek_last()


if __name__ == "__main__":
    queue = myQueue()
    queue.enqueue(LinkedList.Node('F**k'))
    queue.enqueue(LinkedList.Node('You'))
    queue.enqueue(LinkedList.Node('Sharyar and Majid'))

    queue.display()
    queue.dequeue()
    queue.display()