Exemplo n.º 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)
Exemplo n.º 2
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)
Exemplo n.º 3
0
    def mid_order(self, f):
        """
        B树中序遍历
        :param f:
        :return:
        """
        result = []
        stack = Stack()
        cur_node = self.__root
        if cur_node.is_leaf:
            return map(f, cur_node.keys)

        while True:
            if cur_node.is_leaf:
                # 到叶节点了,开始把叶节点的所有关键字都遍历掉
                result.extend(map(f, cur_node.keys))
                # 开始从栈中取元素,遍历下一个节点叶节点
                if stack.empty():
                    return result
                cur_node, i = stack.pop()
                result.append(f(cur_node.keys[i]))
                if i < len(cur_node) - 1:
                    stack.push((cur_node, i + 1))
                cur_node = cur_node.childs[i + 1]
            else:
                stack.push((cur_node, 0))
                cur_node = cur_node.childs[0]
        return result
Exemplo n.º 4
0
 def midorder(self, f):
     result = []
     stack = Stack(self.__root)
     cur_node = self.__root.left
     # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历
     # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点
     while not stack.empty() or cur_node is not None:
         # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环
         if cur_node is not None:
             stack.push(cur_node)
             cur_node = cur_node.left
         else:
             cur_node = stack.pop()
             result.append(f(cur_node.value))
             cur_node = cur_node.right
     return result
Exemplo n.º 5
0
 def midorder(self, f):
     """
     中序遍历
     :param f:访问一个节点的时候要对节点进行处理的函数
     :return:
     """
     result = []
     stack = Stack(self.__root)
     cur_node = self.__root.left
     # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历
     # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点
     while not stack.empty() or cur_node is not self.Nil:
         # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环
         if cur_node is not self.Nil:
             stack.push(cur_node)
             cur_node = cur_node.left
         else:
             cur_node = stack.pop()
             result.append(f(cur_node))
             cur_node = cur_node.right
     return result
Exemplo n.º 6
0
 def preorder(self, f):
     result = []
     stack = Stack(self.__root)
     while True:
         cur_node = stack.pop()
         # 栈中没有元素的时候就表示所有的元素都已经遍历完了
         if cur_node is None:
             break
         result.append(f(cur_node.value))
         if cur_node.left is not None:
             stack.push(cur_node.left)
         if cur_node.right is not None:
             stack.push(cur_node.right)
     return result
Exemplo n.º 7
0
 def preorder(self, f):
     result = []
     stack = Stack(self.__root)
     while True:
         cur_node = stack.pop()
         # 栈中没有元素的时候就表示所有的元素都已经遍历完了
         if cur_node is None:
             break
         result.append(f(cur_node.value))
         if cur_node.left is not None:
             stack.push(cur_node.left)
         if cur_node.right is not None:
             stack.push(cur_node.right)
     return result
Exemplo n.º 8
0
    def dfs(self, gray_func, black_func):
        """
        图的深度遍历
        :param gray_func:
        :param black_func:
        :return:
        """
        gray_list = []
        black_list = []

        # 初始化
        for key in self.__dict.iterkeys():
            key.start_time = None
            key.end_time = None
            key.set_white()

        # 开始遍历
        counter = 0
        for key in self.__dict.iterkeys():
            if key.is_white():
                dfs_stack = Stack()
                key.set_gray()
                key.start_time = counter
                counter += 1
                dfs_stack.push(key)
                while not dfs_stack.empty():
                    cur_node = dfs_stack.pop()
                    gray_list.append(gray_func(key))
                    for end_node in self.__dict[cur_node]:
                        if end_node.is_white():
                            end_node.set_gray()
                            end_node.start_time = counter
                            counter += 1
                            dfs_stack.push(end_node)
                    cur_node.set_black()
                    black_list.append(black_func(cur_node))
                    cur_node.end_time = counter
                    counter += 1
        return gray_list, black_list
Exemplo n.º 9
0
 def midorder(self, f):
     result = []
     stack = Stack(self.__root)
     cur_node = self.__root.left
     # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历
     # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点
     while not stack.empty() or cur_node is not None:
         # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环
         if cur_node is not None:
             stack.push(cur_node)
             cur_node = cur_node.left
         else:
             cur_node = stack.pop()
             result.append(f(cur_node.value))
             cur_node = cur_node.right
     return result
Exemplo n.º 10
0
    def dfs(self, gray_func, black_func):
        """
        图的深度遍历
        :param gray_func:
        :param black_func:
        :return:
        """
        gray_list = []
        black_list = []

        # 初始化
        for key in self.__dict.iterkeys():
            key.start_time = None
            key.end_time = None
            key.set_white()

        # 开始遍历
        counter = 0
        for key in self.__dict.iterkeys():
            if key.is_white():
                dfs_stack = Stack()
                key.set_gray()
                key.start_time = counter
                counter += 1
                dfs_stack.push(key)
                while not dfs_stack.empty():
                    cur_node = dfs_stack.pop()
                    gray_list.append(gray_func(key))
                    for end_node in self.__dict[cur_node]:
                        if end_node.is_white():
                            end_node.set_gray()
                            end_node.start_time = counter
                            counter += 1
                            dfs_stack.push(end_node)
                    cur_node.set_black()
                    black_list.append(black_func(cur_node))
                    cur_node.end_time = counter
                    counter += 1
        return gray_list, black_list
Exemplo n.º 11
0
 def midorder(self, f):
     """
     中序遍历
     :param f:访问一个节点的时候要对节点进行处理的函数
     :return:
     """
     result = []
     stack = Stack(self.__root)
     cur_node = self.__root.left
     # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历
     # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点
     while not stack.empty() or cur_node is not self.Nil:
         # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环
         if cur_node is not self.Nil:
             stack.push(cur_node)
             cur_node = cur_node.left
         else:
             cur_node = stack.pop()
             result.append(f(cur_node))
             cur_node = cur_node.right
     return result