Python二叉树的三种深度优先遍历
一、广度优先遍历和深度优先遍历
二、实现一棵二叉树
# coding=utf-8class Node(object):def __init__(self, data):self.data = dataself.parent = Noneself.left_child = Noneself.right_child = Noneclass TreeQueue(object):def __init__(self):self.__members = list()def is_empty(self):return not len(self.__members)def enter(self, data):self.__members.insert(0, data)def outer(self):if self.is_empty():returnreturn self.__members.pop()class PerfectBinaryTree(object):def __init__(self):self.__root = Nonedef is_empty(self):return not self.__rootdef append(self, data):node = Node(data)if self.is_empty():self.__root = nodereturnqueue = TreeQueue()queue.enter(self.__root)while not queue.is_empty():cur = queue.outer()if cur.left_child is None:cur.left_child = nodenode.parent = curreturnqueue.enter(cur.left_child)if cur.right_child is None:cur.right_child = nodenode.parent = curreturnqueue.enter(cur.right_child)def show(self):if self.is_empty():print('空二叉树')returnqueue = TreeQueue()queue.enter(self.__root)while not queue.is_empty():cur = queue.outer()print(cur.data, end=' ')if cur.left_child is not None:queue.enter(cur.left_child)if cur.right_child is not None:queue.enter(cur.right_child)print()def get_root(self):return self.__root
三、先序遍历
# 先根遍历(先序遍历)def preorder_traversal(root):if root is None:returnprint(root.data, end=' ')preorder_traversal(root.left_child)preorder_traversal(root.right_child)
四、中序遍历
# 中根遍历(中序遍历)def inorder_traversal(root):if root is None:returninorder_traversal(root.left_child)print(root.data, end=' ')inorder_traversal(root.right_child)
五、后序遍历
# 后根遍历(后序遍历)def postorder_traversal(root):if root is None:returnpostorder_traversal(root.left_child)postorder_traversal(root.right_child)print(root.data, end=' ')
if __name__ == '__main__':tree = PerfectBinaryTree()for alpha in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']:tree.append(alpha)print('层次遍历:', end='')tree.show()print('先序遍历:', end='')preorder_traversal(tree.get_root())print()print('中序遍历:', end='')inorder_traversal(tree.get_root())print()print('后序遍历:', end='')postorder_traversal(tree.get_root())print()
层次遍历:A B C D E F G H I J先序遍历:A B D H I E J C F G中序遍历:H D I B J E A F C G后序遍历:H I D J E B F G C A
