コード例 #1
0
def main():
    print "\ncheck stack"
    stack = Stack(1, 2, 34, 5)
    for x in range(0, 5):
        stack.push(x)
    print stack
    for x in range(0, 15):
        print "".join(["size=", str(len(stack)), " cur_node=", str(stack.pop())])

    print "\ncheck queue"
    queue = Queue(1, 2, 34, 5)
    for x in range(0, 5):
        queue.enter(x)
    print stack
    for x in range(0, 15):
        print "".join(["size=", str(len(queue)), " cur_node=", str(queue.exit())])

    print "\ncheck BSTree"
    tree = BSTree(1, 2, 34, 5)
    print tree
    print tree.find(10)
    print tree.find(5)
    print tree.max()
    print tree.min()
    print tree.successor(34)
    print tree.successor(5)
    print tree.predecessor(1)
    print tree.predecessor(2)
コード例 #2
0
    def flatorder(self, f):
        result = []
        queue = Queue(self.__root)
        while not queue.empty():
            cur_node = queue.exit()
            result.append(f(cur_node.value))
            if cur_node.left is not None:
                queue.enter(cur_node.left)

            if cur_node.right is not None:
                queue.enter(cur_node.right)
        return result
コード例 #3
0
    def print_tree(self):
        """
        打印树的结构
        :return:
        """
        queue = Queue(self.__root)
        next_level = 1
        now_node_count = 0
        while not queue.empty():
            cur_node = queue.exit()
            print str(cur_node) + "\t",
            now_node_count += 1
            if now_node_count == next_level:
                print
                now_node_count = 0
                next_level *= 2
            if cur_node.left is not None:
                queue.enter(cur_node.left)

            if cur_node.right is not None:
                queue.enter(cur_node.right)
コード例 #4
0
ファイル: interval_tree.py プロジェクト: MarkAlive/algorithm
    def print_tree(self):
        """
        打印树的结构
        :return:
        """
        queue = Queue(self.__root)
        next_level = 1
        now_node_count = 0
        while not queue.empty():
            cur_node = queue.exit()
            print str(cur_node) + "\t",
            now_node_count += 1
            if now_node_count == next_level:
                print
                now_node_count = 0
                next_level *= 2
            if cur_node.left is not None:
                queue.enter(cur_node.left)

            if cur_node.right is not None:
                queue.enter(cur_node.right)
コード例 #5
0
ファイル: weighted_graph.py プロジェクト: MarkAlive/algorithm
 def lws(self, from_node, to_node):
     """
     在有向无环带权图里面查找最长路径
     令list(s,t)是从s到t的最长带权路径。
     那么可以使用递归式表示这个问题
     list(s,t) = max(list(s,t-1))+list(t-1,t)(唯一)
     用动态规划自下而上解决,因为自上而下解决首先遇到的问题就是
     查找指向一个节点的节点在邻接表中比较困难
     :param from_node:
     :param to_node:
     :return:
     """
     __lws = {}
     # 为了计算方便,这里把开始节点到开始节点插入字典中,
     zero_edge = Edge(from_node, from_node)
     __lws[zero_edge] = 0
     graph_stack = Queue()
     graph_stack.enter(from_node)
     while not graph_stack.empty():
         cur_node = graph_stack.exit()
         cur_edge_list = self.__adj_list.get(cur_node)
         if cur_edge_list is None:
             print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
             print ",".join(map(lambda edge: str(edge), __lws))
             return __lws[Edge(from_node, to_node)]
         for edge_end_node in cur_edge_list:
             graph_stack.enter(edge_end_node.key)
             last_weighted_length = __lws[Edge(from_node, cur_node)]
             cur_edge = Edge(from_node, edge_end_node.key)
             cur_weight_length = last_weighted_length + edge_end_node.weight
             # 如果不存在这个边,那么就插入
             if cur_edge not in __lws:
                 __lws[cur_edge] = cur_weight_length
             # 如果存在,那么就把最大值插入
             elif cur_weight_length > __lws[cur_edge]:
                 __lws[cur_edge] = cur_weight_length
     print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
     print ",".join(map(lambda edge: str(edge), __lws))
     return __lws[Edge(from_node, to_node)]
コード例 #6
0
 def lws(self, from_node, to_node):
     """
     在有向无环带权图里面查找最长路径
     令list(s,t)是从s到t的最长带权路径。
     那么可以使用递归式表示这个问题
     list(s,t) = max(list(s,t-1))+list(t-1,t)(唯一)
     用动态规划自下而上解决,因为自上而下解决首先遇到的问题就是
     查找指向一个节点的节点在邻接表中比较困难
     :param from_node:
     :param to_node:
     :return:
     """
     __lws = {}
     # 为了计算方便,这里把开始节点到开始节点插入字典中,
     zero_edge = Edge(from_node, from_node)
     __lws[zero_edge] = 0
     graph_stack = Queue()
     graph_stack.enter(from_node)
     while not graph_stack.empty():
         cur_node = graph_stack.exit()
         cur_edge_list = self.__adj_list.get(cur_node)
         if cur_edge_list is None:
             print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
             print ",".join(map(lambda edge: str(edge), __lws))
             return __lws[Edge(from_node, to_node)]
         for edge_end_node in cur_edge_list:
             graph_stack.enter(edge_end_node.key)
             last_weighted_length = __lws[Edge(from_node, cur_node)]
             cur_edge = Edge(from_node, edge_end_node.key)
             cur_weight_length = last_weighted_length + edge_end_node.weight
             # 如果不存在这个边,那么就插入
             if cur_edge not in __lws:
                 __lws[cur_edge] = cur_weight_length
             # 如果存在,那么就把最大值插入
             elif cur_weight_length > __lws[cur_edge]:
                 __lws[cur_edge] = cur_weight_length
     print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
     print ",".join(map(lambda edge: str(edge), __lws))
     return __lws[Edge(from_node, to_node)]