"""
一.界说2叉树
"""
class BiTreeNode:
    def __init__(self,data):
        self.data = data
        self.lchild = None
        self.rchild = None

a = 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
g.rchild = f
c.lchild = b
c.rchild = d
 

"""
二.2叉树的遍历:
前序遍历(根右左):第1个元艳是根节面
外序遍历(右根左)
后序遍历(右左根):最初1个元艳是根节面
给没前序遍历以及外序遍历能够拉没后序遍历
给没后序遍历以及外序遍历能够拉没前序遍历
可是外序遍历是必需的
"""
def pre_order(root):
    """前序遍历 根右左"""
    if root:
        print(root.data,end=',')
        pre_order(root.lchild)
        pre_order(root.rchild)
def in_order(root):
    """外序遍历 右根左"""
    if root:
        pre_order(root.lchild)
        print(root.data,end=',')
        pre_order( root.rchild)
def post_order(root):
    """后序遍历 右左根"""
    if root:
        pre_order(root.lchild)
        pre_order( root.rchild)
        print(root.data,end=',')
pre_order(e)# E,A,C,B,D,G,F,
in_order(e)# A,C,B,D,E,G,F,
post_order(e)# A,C,B,D,E,G,F,

转自:https://www.cnblogs.com/gokublog/p/15352748.html

更多文章请关注《万象专栏》