Пример #1
0
    def go_deep(self, curr_node, level, parent=None):
        ''' Deepness '''
        for left, right in curr_node.items():
            tmp_node = Node(left, level + 1, parent, self.DEBUG)
            if (parent is not None):
                parent.addChild(tmp_node)
            else:
                self.root_node = tmp_node

            if (type(right) == dict):
                self.go_deep(right, level + 1, tmp_node)
            else:
                parent.addChild(Node(right, level + 2, tmp_node, self.DEBUG))
    def initialise_heap(self):
        """Initialises a heap of Nodes of length self.max_size * 4 + 1"""
        heap = np.array([
            Node(
                self.default_key_to_use,
                tuple([None
                       for _ in range(self.dimension_of_value_attribute)]))
            for _ in range(self.max_size * 4 + 1)
        ])

        # We don't use the 0th element in a heap so we want it to have infinite value so it is never swapped with a lower node
        heap[0] = Node(float("inf"), (None, None, None, None, None))
        return heap
Пример #3
0
 def add(self, val):
     """链表头部添加元素
     :param val: 要保存的具体数据
     """
     node = Node(val)
     node.next = self.__head
     self.__head = node
 def initialise_deque(self):
     """Initialises a queue of Nodes of length self.max_size"""
     deque = np.array([
         Node(
             0,
             tuple([None
                    for _ in range(self.dimension_of_value_attribute)]))
         for _ in range(self.max_size)
     ])
     return deque
Пример #5
0
 def depth_first_search(self, head, visited):
     if not head:
         return None, visited
     if head in visited:
         return visited[head], visited
     new_node = Node(head.val)
     visited[head] = new_node
     new_node.next, visited = self.depth_first_search(head.next, visited)
     new_node.random, visited = self.depth_first_search(
         head.random, visited)
     return new_node, visited
Пример #6
0
 def __init__(self, node=None, *args, **kwargs):
     self._head = 1
     if node is None:
         self.__head = node
     else:
         self.__head = Node(node)
         for arg in args:
             self.append(arg)
     if kwargs.values() is not None:
         for kwarg in kwargs:
             self.append(kwargs[kwarg])
Пример #7
0
 def append(self, val):
     """链表尾部添加元素"""
     node = Node(val)
     # 如果链表为空,需要特殊处理
     if self.is_empty():
         self.__head = node
     else:
         cur = self.__head
         while cur.next is not None:
             cur = cur.next
         # 退出循环的时候,cur指向的尾结点
         cur.next = node
Пример #8
0
def tree_for_adv_search():
    a1 = Node(infinity, create_leafs([3, 12, 8]))
    a2 = Node(infinity, create_leafs([2, 4, 6]))
    a3 = Node(infinity, create_leafs([14, 5, 2]))
    parent = Node(infinity, [a1, a2, a3])
    print('number of nodes: {}'.format(parent.total()))
    return parent
def ComplicatedLinkList(value_2d_lst):
    """

    :param value_2d_lst:
    :return:
    """
    if not value_2d_lst: return None
    node_lst = [Node(value) for value, _ in value_2d_lst]

    for idx, [_, random] in enumerate(value_2d_lst):
        node_lst[idx].next = node_lst[
            idx + 1] if idx is not len(node_lst) - 1 else None
        node_lst[idx].random = node_lst[random] if random != 'null' else None

    return node_lst[0]
Пример #10
0
 def insert(self, pos, val):
     """指定位置添加元素"""
     # 在头部添加元素
     if pos <= 0:
         self.add(val)
     # 在尾部添加元素
     elif pos >= self.length():
         self.append(val)
     else:
         cur = self.__head
         count = 0
         while count < (pos - 1):
             count += 1
             cur = cur.next
         # 退出循环的时候,cur指向pos的前一个位置
         node = Node(val)
         node.next = cur.next
         cur.next = node
Пример #11
0
def austria_graph():
    def create_child(node):
        return {'node': node, 'color': None}

    wa = create_child(Node("WA"))
    nt = create_child(Node("NT"))
    sa = create_child(Node("SA"))
    q = create_child(Node('Q'))
    nsw = create_child(Node('NSW'))
    v = create_child(Node('V'))

    wa['node'].add_children([
        nt,
        sa
    ])

    sa['node'].add_children([
        wa,
        nt,
        q,
        nsw,
        v
    ])

    nt['node'].add_children([
        wa,
        sa,
        q
    ])

    q['node'].add_children([
        nt,
        sa,
        nsw
    ])
    nsw['node'].add_children([
        q,
        sa,
        v
    ])

    v['node'].add_children([
        nsw,
        sa
    ])

    return wa
Пример #12
0
def letterTree():
    a1 = Node('B', create_leafs(['C', 'D', 'E'], 3), 2)
    a2 = Node('F', create_leafs(['G', 'H', 'I'], 3), 2)
    a3 = Node('J', create_leafs(['K', 'L', 'M'], 3), 2)
    parent = Node('A', [a1, a2, a3], 1)
    return parent
Пример #13
0
def letterGraph():
    a = Node('A')
    b = Node('B')
    c = Node('C')
    d = Node('D')
    c = Node('C')
    e = Node('E')
    f = Node('F')
    g = Node('G')

    a.add_children([b, c])
    b.add_children([a, d, e])
    c.add_children([a, d])
    d.add_children([b, c, e, g, f])
    e.add_children([b, d, g])
    f.add_children([d, g])
    g.add_children([e, d, f])

    return a
Пример #14
0
def mapGraphWithCost():
    def create_node_with_cost(node, distance):
        return {'node': node, 'distance': distance, 'cost': infinity}

    arad = Node('Arad')
    sibiu = Node('Sibiu')
    timisoara = Node('Timisoara')
    zerind = Node('Zerind')
    fagaras = Node('Fagaras')
    oradea = Node('Oradea')
    rimniicu_vilcea = Node('Rimniicu Vilcea')
    lugoj = Node('Lugoj')
    bucharest = Node('Bucharest')
    pitesti = Node('Pitesti')
    craiova = Node('Craiova')
    mehadia = Node('Mehadia')
    urzieni = Node('Urzieni')
    drobeta = Node('Drobeta')
    hirsova = Node('Hirsova')
    vaslui = Node('Vaslui')
    iasi = Node('Iasi')

    arad.add_children([
        create_node_with_cost(sibiu, 140),
        create_node_with_cost(timisoara, 118),
        create_node_with_cost(zerind, 75)
    ])
    sibiu.add_children([
        create_node_with_cost(arad, 140),
        create_node_with_cost(fagaras, 99),
        create_node_with_cost(oradea, 151),
        create_node_with_cost(rimniicu_vilcea, 80)
    ])
    timisoara.add_children(
        [create_node_with_cost(arad, 118),
         create_node_with_cost(lugoj, 111)])
    zerind.add_children(
        [create_node_with_cost(arad, 75),
         create_node_with_cost(oradea, 71)])
    fagaras.add_children([
        create_node_with_cost(sibiu, 99),
        create_node_with_cost(bucharest, 211)
    ])
    oradea.add_children(
        [create_node_with_cost(zerind, 71),
         create_node_with_cost(sibiu, 151)])
    rimniicu_vilcea.add_children([
        create_node_with_cost(sibiu, 80),
        create_node_with_cost(pitesti, 97),
        create_node_with_cost(craiova, 146)
    ])
    lugoj.add_children([
        create_node_with_cost(timisoara, 111),
        create_node_with_cost(mehadia, 70)
    ])
    bucharest.add_children([
        create_node_with_cost(fagaras, 211),
        create_node_with_cost(pitesti, 101),
        create_node_with_cost(Node('Giurgiu', bucharest), 90),  # Leaf Node
        create_node_with_cost(urzieni, 85)
    ])
    pitesti.add_children([
        create_node_with_cost(rimniicu_vilcea, 97),
        create_node_with_cost(craiova, 138),
        create_node_with_cost(bucharest, 101)
    ])
    craiova.add_children([
        create_node_with_cost(pitesti, 138),
        create_node_with_cost(rimniicu_vilcea, 146),
        create_node_with_cost(drobeta, 120)
    ])
    mehadia.add_children(
        [create_node_with_cost(lugoj, 70),
         create_node_with_cost(drobeta, 75)])
    urzieni.add_children([
        create_node_with_cost(hirsova, 98),
        create_node_with_cost(vaslui, 142),
        create_node_with_cost(bucharest, 85)
    ])
    drobeta.add_children([
        create_node_with_cost(mehadia, 75),
        create_node_with_cost(craiova, 120)
    ])
    hirsova.add_children([
        create_node_with_cost(Node('Eforie', [hirsova]), 86),
        create_node_with_cost(urzieni, 98)
    ])
    vaslui.add_children(
        [create_node_with_cost(urzieni, 142),
         create_node_with_cost(iasi, 92)])
    iasi.add_children([
        create_node_with_cost(vaslui, 92),
        create_node_with_cost(Node('Neamt', [iasi]), 87)
    ])

    return arad