热线电话:13121318867

登录
2019-07-11 阅读量: 1717
二叉树的遍历

class BiTreeNode(object):

def __init__(self,data):

self.data = data

self.lchild = None

self.rchild = Nonea = BiTreeNode('A')

b= BiTreeNode('B')

c = BiTreeNode('C')

d = BiTreeNode('D')

e = BiTreeNode('E')

f = BiTreeNode('F')

g = BiTreeNode('G')

e.lchild = a

e.rchild = g

a.rchild = c

c.lchild = b

c.rchild = d

g.rchild = f

root = e# 前序遍历 (先找做子树,后找右子树)def pre_order(root):

if root:

print(root.data,end='') # EACBDGF

pre_order(root.lchild)

pre_order(root.rchild)

pre_order(root)

print('')# 中序遍历def in_order(root):

if root:

in_order(root.lchild)

print(root.data,end='') # ABCDEGF

in_order(root.rchild)

in_order(root) # ABCDEGFprint('')# 后序遍历def post_order(root):

if root:

post_order(root.lchild)

post_order(root.rchild)

print(root.data,end='')

post_order(root) #BDCAFGE

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据