def list_depths(root, queue=None, levels=None, index=0):
    if queue is None:
        queue = dequeue([root])
    if levels is None:
        levels = dict()

    levels[index] = LinkedList()
def list_depths(root, queue=None, levels=None, index=0):
    if queue is None:
        queue = dequeue([root])
    if levels is None:
        levels = dict()

    levels[index] = LinkedList()
Esempio n. 3
0
def bfs(g, root):
    visited = set()
    queue = collections.dequeue([root])
    visited.add(root)
    while queue:
        s = queue.popleft()
        for i in g[s]:
            if i not in visited:
                visited.add(i)
                queue.append(i)
Esempio n. 4
0
def Kruskal(G):
    mst_pq = dequeue()
    min_pq = PriorityQueue()
    for e in G.edges():
        min_pq.put(e)
    uf = UF(V)  # a union find data structure
    while min_pq and mst_pq.size() < V - 1:
        e = min_pq.get()
        v = e.either()
        w = e.other(v)
        if uf.connected(v, w):
            continue
        uf.union(v, w)
        mst_pq.append(e)
 def bfs(begin, end, dict_word):
     q, visited = dequeue([(beigin,1)]), set()
     while q:
         word, steps = q.popleft()
         if word not in visited:
             if word==end:
                 return steps
             visited.add(word)
             for i in range(len(word)):
                 s = word[:i]+'*'+word[i+1:]
                 neigh_words = dict_word.get(s,[])
                 for neign in neigh_words:
                     if neigh not in visited:
                         q.append((neigh,steps+1))
     return 0
Esempio n. 6
0
    def iterative_breadth_first_for_each(self, fn):
        from collections import dequeue
        # BFT: FIFO traversal
        # we'll use a queue structure to facilitate ordering
        # pattern the same as before, except using a queue
        queue = dequeue()
        queue.append(self)

        while len(queue) > 0:
            current = queue.popleft()  # equivalent of dequeueing
            if current.left:
                queue.append(current.left)
            if current.right:
                queue.append(current.right)
            fn(current.value)
Esempio n. 7
0
def max_in_sliding_window(arr, w):
    out = []
    deq = dequeue()

    for i in range(len(arr)):
        # Pop elements outside of window
        while (len(deq) > 0) and deq[0] <= (i-w):
            deq.popleft()

        # DQ should remain in descending order. Remove any element less than
        # current from deque as those elements can never be max for this window.
        while (len(deq) > 0) and arr[deq[-1]] < arr[i]:
            deq.popright()

        # Push current element's index to deque
        deq.append(i)

        # Add max element at head of dequeue to result
        out.append(arr[deq.popleft])
    return out
Esempio n. 8
0
 def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
     traverse = []
     if root == None:
         return traverse
     row = [root]
     cnt = 0
     while len(row) > 0:
         if cnt % 2 == 0:
             rowVal = [node.val for node in row]
         else:
             rowVal = [node.val for node in row[::-1]]
         traverse.append(rowVal)
         row_next = dequeue()
         for node in row:
             if node.left:
                 row_next.append(node.left)
             if node.right:
                 row_next.append(node.right)
         row = row_next
         cnt += 1
     return traverse
Esempio n. 9
0
def solution2(text):
    """
    Single forward pass w/ queue solution O(n).

    >>> solution1("let's replace all whitespaces with %20")
    "let's%20replace%20all%20whitespaces%20with%20%20"
    """
    from collections import dequeue
    text = list(text)
    queue = dequeue()
    old_len, idx = len(text), 0
    for i in range(old_len):
        if text[i] == ' ':
            queue.appendleft('%')
            queue.appendleft('2')
            queue.appendleft('0')
        else:
            queue.appendleft(text[i])
        text[idx] = queue.pop()
        idx += 1
    while queue:
        text.append(queue.pop())
    return "".join(text)
def solution2(text):
    """
    Single forward pass w/ queue solution O(n).

    >>> solution1("let's replace all whitespaces with %20")
    "let's%20replace%20all%20whitespaces%20with%20%20"
    """
    from collections import dequeue
    text = list(text)
    queue = dequeue()
    old_len, idx = len(text), 0
    for i in range(old_len):
        if text[i] == ' ':
            queue.appendleft('%')
            queue.appendleft('2')
            queue.appendleft('0')
        else:
            queue.appendleft(text[i])
        text[idx] = queue.pop()
        idx += 1
    while queue:
        text.append(queue.pop())
    return "".join(text)
Esempio n. 11
0
# queue_example.py

from collections import dequeue

a = dequeue()

a.append('Monday')


from collections import dequeue

def list_depths(root, queue=None, levels=None, index=0):
    if queue is None:
        queue = dequeue([root])
    if levels is None:
        levels = dict()

    levels[index] = LinkedList()
queue2 = dequeue()
while len(queue1):
    node = queue.pop()
    levels[index].add(node)
    if node.left:
        queue2.append(node.left)
    if node.right:
        queue2.append(node.right)
return list_depths(root, queue, levels, index + 1)






from collections import dequeue


def list_depths(root, queue=None, levels=None, index=0):
    if queue is None:
        queue = dequeue([root])
    if levels is None:
        levels = dict()

    levels[index] = LinkedList()


queue2 = dequeue()
while len(queue1):
    node = queue.pop()
    levels[index].add(node)
    if node.left:
        queue2.append(node.left)
    if node.right:
        queue2.append(node.right)
return list_depths(root, queue, levels, index + 1)