python实现二叉树遍历_Python实现二叉树的遍历

Python实现二叉树的遍历

class BinaryTree(object):

def __init__(self, value=None, left=None, right=None):

self.value = value

self.left = left

self.right = right

def rebuild(self, preOrder, inOrder):

"""

根据前序列表和中序列表,重建二叉树

:param preOrder: 前序列表

:param inOrder: 中序列表

:return: 二叉树

"""

if preOrder == None or inOrder == None or len(preOrder) <=0 or len(inOrder) <=0 \

or len(preOrder) != len(inOrder):

return None

cur = BinaryTree(preOrder[0])

index = inOrder.index(preOrder[0])

cur.left = self.rebuild(preOrder[1: index+1], inOrder[:index])

cur.right = self.rebuild(preOrder[index+1:], inOrder[index+1:])

return cur

def preOrder(self, tree):

"""

前序遍历

:param tree:

:return:

"""

if tree == None:

return None

print(tree.value, end=' ')

self.preOrder(tree.left)

self.preOrder(tree.right)

def preOrderLoop(self, tree):

"""

前序遍历的循环实现

:param tree:

:return:

"""

if tree == None:

return None

lst = []

node = tree

while node != None or len(lst) > 0:

if node != None:

lst.append(node)

print(node.value, end=' ')

node = node.left

else:

item = lst[len(lst)-1]

lst = lst[:-1]

node = item.right

def inOrder(self, tree):

"""

中序遍历

:param tree:

:return:

"""

if tree == None:

return None

self.inOrder(tree.left)

print(tree.value, end=' ')

self.inOrder(tree.right)

def inOrderLoop(self, tree):

"""

中序遍历循环实现

:param tree:

:return:

"""

if tree == None:

return

lst = []

node = tree

while node != None or len(lst) > 0:

if node != None:

lst.append(node)

node = node.left

else:

item = lst[len(lst)-1]

lst = lst[:-1]

print(item.value, end=' ')

node = item.right

def postOrder(self, tree):

"""

后序遍历

:param tree:

:return:

"""

if tree == None:

return None

self.postOrder(tree.left)

self.postOrder(tree.right)

print(tree.value, end=' ')

def postOrderLoop(self, tree):

"""

后续遍历的循环实现

:param tree:

:return:

"""

if tree == None:

return None

visited = set()

lst = []

node = tree

while node != None or len(lst) > 0:

if node != None:

lst.append(node)

node = node.left

else:

item = lst[len(lst)-1]

if item.right != None and item.right not in visited:

node = item.right

else:

print(item.value, end=' ')

visited.add(item)

lst = lst[:-1]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值