class Node(object):
def __init__(self, data=-1, lchild=None, rchild=None):
self.data = data
self.lchild = lchild
self.rchild = rchild
class Tree(object):
def __init__(self):
self.root = Node()
self.treeQueue = []
def add(self, data): # 层序添加成完全二叉树
node = Node(data)
if self.root.data == -1: # 树为空
self.root = node
self.treeQueue.append(self.root)
else: # 树不为空,那么队列有根节点
treeNode = self.treeQueue[0] # 取队列第一个数为父结点
if treeNode.lchild == None: # 先加左子树
treeNode.lchild = node
self.treeQueue.append(node)
elif treeNode.rchild == None: # 后加右子树
treeNode.rchild = node
self.treeQueue.append(node)
self.treeQueue.pop(0) # 父节点的左右子树都赋值了
def pre_order_recursion(self, root): # 递归实现
if not root:
return
print(str(root.data) + " ", end='')
self.pre_order_recursion(root.lchild)
self.pre_order_recursion(root.rchild)
def pre_order_stack(self, root): # 堆栈:非递归实现
if not root:
return
stack = [] # pop() 函数用于移除列表中的一个元素(默认最后一个元素)
node = root # 初试化父节点
while node or stack: # 一直有下一个节点,或者 stack里面有元素
while node: # 从根结点开始一直寻找左子树
print(str(node.data) + " ", end='')
stack.append(node) # 根
node = node.lchild
node = stack.pop() # 当没有下一个左子树了,就返回最后一个父节点,看他的右子树
node = node.rchild
def in_order_stack(self, root):
if not root:
return
stack = []
node = root
while node or stack:
while node:
stack.append(node)
node = node.lchild
# 当没有左子树了,就可以打印父节点了
node = stack.pop()
print(str(node.data) + " ", end='') # 中序和前序区别就在这一行
node = node.rchild
def post_order_stack(self, root):
# 后序遍历难一些。利用先序:根->左-> 右, 变为 根->右->左(左右有序)
# 再输出 左->右->根
if not root:
return
stack = []
post_stack = []
node = root
while stack or node:
while node:
stack.append(node)
post_stack.append(node)
node = node.rchild
node = stack.pop()
node = node.lchild
while post_stack:
print(str(post_stack.pop().data) + " ", end='')
# 时间复杂度: O(n): 每个节点只遍历一次
# 空间复杂度: O(2N): 两个stack
def level_order_queue(self, root): # 队列实现:父节点出列,他的左右子树入列
if not root:
return
queue = []
node = root
queue.append(node)
while queue:
node = queue.pop(0)
print(str(node.data) + " ", end='')
if node.lchild:
queue.append(node.lchild)
if node.rchild:
queue.append(node.rchild)
if __name__ == '__main__':
"""主函数"""
datas = [i for i in range(10)] #生成十个数,放到树中
tree = Tree()
for data in datas:
tree.add(data) #逐个添加树的节点
print('递归实现前序遍历:')
tree.pre_order_recursion(tree.root)
print("\n")
tree.pre_order_stack(tree.root)
print('\n非递归实现中序遍历:')
tree.in_order_stack(tree.root)
print('\n非递归实现后序遍历:')
tree.post_order_stack(tree.root)
print('\n队列实现层次遍历:')
tree.level_order_queue(tree.root)