def level_order_traversal(node):
    if node == None:
        return
    queue1 = Queue()
    queue1.insert(node)
    while not queue1.is_empty():
        temp = queue1.remove()
        print temp,
        if temp.left:
            queue1.insert(temp.left)
        if temp.right:
            queue1.insert(temp.right)
Beispiel #2
0
def size_of_tree_queue(node):
    if node == None:
        return
    queue1 = Queue()
    queue1.insert(root)
    size = 0
    while not queue1.is_empty():
        temp = queue1.remove()
        size += 1
        if temp.left:
            queue1.insert(temp.left)
        if temp.right:
            queue1.insert(temp.right)
    return size
def is_binary_tree_complete(node):
    if node == None:
        #probably tree is empty
        return True
    queue = Queue()
    queue.insert(node)
    while not queue.is_empty():
        temp = queue.remove()
        # if one of the children is None
        if ((temp.left== None and temp.right) or
              (temp.right== None and temp.left)):
            return False
        elif temp.left and temp.right:
            queue.insert(temp.left)
            queue.insert(temp.right)
    return True
    def binary_tree_count_leaves_old(self, root):
        if not root:
            return 0

        queue = Queue()
        queue.insert(root)
        leaf_count = 0
        
        while queue.size()>0:
            node = queue.remove()
            if self.is_leaf(node):
                leaf_count +=1
            if node.left:
                queue.insert(node.left)
            if node.right:
                queue.insert(node.right)
        return leaf_count
def binary_tree_invert(root):
    if not root:
        return 
    queue = Queue()
    queue.insert(root)

    while not queue.is_empty():
        temp = queue.remove()
        if temp.left:
            queue.insert(temp.left)
        if temp.right:
            queue.insert(temp.right)

        transfer = temp.left
        temp.left = temp.right
        temp.right = transfer

    return root
Beispiel #6
0
    def _shortest_path(self, start_node:"Hashable", queue:Queue,
                             verbose:bool=False) -> "Graph":
        father = {}
        distance = {}

        if verbose:
            node_list = [n for n in self.list_nodes()]
            print("{:^5}|{:^5}".format("u","d") +
                   (" " * 5 * (len(node_list)-1)) +
                   "|{:^5}".format("p") + 
                   (" " * 5 * (len(node_list)-1)) + 
                   "|{:^5}".format("Q"))
            print("{:^5}".format("") +
                 ("|"+ ("{:^5}"*len(node_list)).format(*node_list)) *2 + 
                  "|")
            print("-"*5 + "|" +
                  ("-"*5*len(node_list) + "|")*2) 

        for node in self.list_nodes():
            distance[node] = float("inf")
            father[node] = None

        distance[start_node] = 0
        father[start_node] = None

        queue.put(start_node, distance[start_node])

        while not queue.empty():
            node = queue.get()
            for neighbour in self.forward_star(node):
                connection_weight = self.get_weight(node, neighbour)
                if not connection_weight:
                    connection_weight = 0
                if distance[node] + connection_weight < distance[neighbour]:
                    distance[neighbour] = distance[node] + connection_weight
                    father[neighbour] = node
                    if not queue.is_in(neighbour):
                        queue.put(neighbour, distance[neighbour])
            if verbose:
                d_list = [distance[n] for n in node_list]
                f_list = [father[n] for n in node_list]
                p_list = d_list + f_list
                row_format = "{:^5}|" + (("{:^5}"*len(node_list)) + "|")*2
                print(row_format.format(node, *p_list),queue)


        result = Graph()
        result.add_node(start_node,value=0)
        for node in self.list_nodes():
            if father[node] != None:
                result.add_connection(father[node], node)
                result.set_node_value(node, distance[node])

        return result
def height_of_tree(head):
    if head == None:
        return 0
    queue = Queue()
    queue.insert(head)
    height = 0

    while(1):
        children_count = queue.size()
        if children_count == 0:
            return height
        else:
            height += 1

        while(children_count>0):
            node = queue.remove()
            if node.left:
                queue.insert(node.left)
            if node.right:
                queue.insert(node.right)
            children_count -= 1
def binary_tree_connect_nodes_at_same_level_to_linked_list(node):
    if node == None:
        return
    queue1 = Queue()
    queue1.insert(node)
    linked_list = None
    node_count = 1
    temp_list = []
    while not queue1.is_empty():
        node_count = queue1.size()
        temp = queue1.remove()
        linked_list = insert_in_linked_list(linked_list, temp)
        node_count -= 1
        if temp.left:
            temp_list.append(temp.left)
        if temp.right:
            temp_list.append(temp.right)
        if node_count == 0:
            print_linked_list(linked_list)
            for e in temp_list:
                queue1.insert(e)
            temp_list = []
            linked_list = None
 def __init__(self, *args):
     Queue.__init__(self, *args)