Beispiel #1
0
    def traversal(self, visitor: Visitor):
        """
        遍历
        def test(key,value):
            print(key, value)
            return True if key == 10 else False
        :param visitor: lambda函数, 类似于上面形式的
        :return:
        """
        if self.__size == 0:
            return
        assert visitor is not None, "visit不能为空,请传入一个lambda函数"

        queue = Queue()
        for i in range(len(self.__table)):
            root = self.__table[i]
            if root is None:
                continue
            queue.en_queue(root)
            while not queue.is_empty():
                node = queue.de_queue()
                if visitor.visit(node.key, node.value):
                    return
                if node.left:
                    queue.en_queue(node.left)
                if node.right:
                    queue.en_queue(node.right)
Beispiel #2
0
    def __ensure_capacity(self):
        """
        扩容
        :param capacity:
        :return:
        """
        old_capacity = len(self.__table)
        if self.__size / old_capacity < 0.75:
            return

        # 开始扩容 扩大到原来的两倍
        new_capacity = old_capacity << 1
        print('---扩容了--', self.__size, old_capacity, new_capacity)

        old_table = self.__table
        self.__table = [None] * new_capacity
        # new_table = [None] * new_capacity
        queue = Queue()
        for index in range(old_capacity):
            root = old_table[index]
            if root is None:
                continue
            queue.en_queue(root)

            while not queue.is_empty():
                node = queue.de_queue()

                if node.left:
                    queue.en_queue(node.left)

                if node.right:
                    queue.en_queue(node.right)

                self.__move_node(node)
    def level_order_tranversal(self, visitor=None) -> List[T]:
        """
		层序遍历 visit为lambda函数,可以控制遍历随时停止
		def visit(e):
			print(e)
			return True if e == 10 else False
		:param visitor: lambda函数, 类似于上面形式的
		:return:
		"""
        if self._root is None:
            return
        container = []
        queue = Queue()
        queue.en_queue(self._root)
        # 如果队列不为空 进入循环
        while not queue.is_empty():
            node = queue.de_queue()
            container.append(node.element)
            if visitor is not None and visitor.visit(node.element):
                # 控制遍历随时停止
                return container

            if node.left is not None:
                queue.en_queue(node.left)
            if node.right is not None:
                queue.en_queue(node.right)

        return container
Beispiel #4
0
    def is_complete(self) -> bool:
        """
		是否是完全二叉树
		:return:
		"""
        if self._root is None:
            return False

        queue = Queue()
        queue.en_queue(self._root)
        is_leaf = False
        while not queue.is_empty():
            node = queue.de_queue()
            if is_leaf and not node.is_leaf():
                return False

            if node.left is not None:
                # 左子树不为空 入队
                queue.en_queue(node.left)
            elif node.right is not None:
                # 能走到这里 说左子树为空,右子树不为空, 则不是完全二叉树
                return False

            if node.right is not None:
                # 如果右子树不为空,入队
                queue.en_queue(node.right)
            else:
                # 如果右子树为空,则后面的节点必须都是叶子节点
                is_leaf = True

        return True
    def contains_value(self, value) -> bool:
        """
		是否包含某个value
		:return:
		"""
        queue = Queue()
        queue.en_queue(self.__root)
        while not queue.is_empty():
            node = queue.de_queue()
            if node.value == value:
                return True
            if node.left:
                queue.en_queue(node.left)
            if node.right:
                queue.en_queue(node.right)

        return False
Beispiel #6
0
def test2():
    """
	测试队列
	:return:
	"""
    queue = Queue()
    queue.en_queue(3)
    queue.en_queue(6)
    queue.en_queue(9)
    queue.en_queue(12)
    # 3,6,9,12,
    print(queue)
    # 3
    print(queue.de_queue())
    # 6,9,12,
    print(queue)
    # 6
    print(queue.front())
Beispiel #7
0
    def level_order_tranversal(self) -> List[T]:
        """
		层序遍历
		:return:
		"""
        if self._root is None:
            return
        container = []
        queue = Queue()
        queue.en_queue(self._root)
        # 如果队列不为空 进入循环
        while not queue.is_empty():
            node = queue.de_queue()
            container.append(node.element)
            if node.left is not None:
                queue.en_queue(node.left)
            if node.right is not None:
                queue.en_queue(node.right)

        return container
Beispiel #8
0
    def contains_value(self, value) -> bool:
        """
        是否包含某个value
        :return:
        """
        queue = Queue()
        for i in range(len(self.__table)):
            root = self.__table[i]
            queue.en_queue(root)
            while not queue.is_empty():
                node = queue.de_queue()
                if node is None:
                    continue
                if node.value == value:
                    return True
                if node.left:
                    queue.en_queue(node.left)
                if node.right:
                    queue.en_queue(node.right)

        return False
Beispiel #9
0
    def height(self) -> int:
        """
		计算树的高度
		:return: 树的高度
		"""
        if self._root is None:
            return 0
        level_size = 1
        tree_height = 0
        queue = Queue()
        queue.en_queue(self._root)
        while not queue.is_empty():
            node = queue.de_queue()
            level_size -= 1
            if node.left is not None:
                queue.en_queue(node.left)

            if node.right is not None:
                queue.en_queue(node.right)

            if level_size == 0:
                level_size = queue.size()
                tree_height += 1
        return tree_height
Beispiel #10
0
	def __str__(self):
		string = ''
		if self.__root is None:
			return 0
		level_size = 1
		# tree_height = 0
		queue = Queue()
		queue.en_queue(self.__root)
		while not queue.is_empty():
			node = queue.de_queue()
			string += node.__str__()
			string += ' '
			level_size -= 1
			if node.left is not None:
				queue.en_queue(node.left)
			
			if node.right is not None:
				queue.en_queue(node.right)
			
			if level_size == 0:
				level_size = queue.size()
				# tree_height += 1
				string += '\n'
				
		return string
 def __str__(self):
     string = ''
     queue = Queue()
     queue.en_queue(self._root)
     level_size = 1
     while not queue.is_empty():
         node = queue.de_queue()
         string += str(node)
         level_size -= 1
         string += ','
         if node.left is not None:
             queue.en_queue(node.left)
         if node.right is not None:
             queue.en_queue(node.right)
         if level_size == 0:
             level_size = queue.size()
             string += '\n'
     return string