【Python】二叉树前序遍历、中序遍历、后序遍历的递归与非递归版本实现+测试用例

算法详解参考:https://blog.csdn.net/Monster_ii/article/details/82115772

 

测试用例二叉树:

【Python】二叉树前序遍历、中序遍历、后序遍历的递归与非递归版本实现+测试用例_第1张图片

前序遍历结果:5 3 2 4 7 6 8

中序遍历结果:2 3 4 5 6 7 8

后序遍历结果:2 4 3 6 8 7 5

层序遍历结果:5 3 7 2 4 6 8

 

代码

1、前序遍历(递归与非递归)

2、中序遍历(递归与非递归)

3、后序遍历(递归与非递归)

4、层序遍历

 

1、前序遍历(递归与非递归)

class TreeNode:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None

# 递归
qianxu = []
def PreOrder(root):
    if not root:
        return None
    qianxu.append(root.val)
    PreOrder(root.left)
    PreOrder(root.right)

# 非递归
def Pre_Order(root):
    data = []
    stack = []
    cur = root
    while cur or stack:
        while cur: # 若当前节点不为空
            data.append(cur.val) # 得到当前节点值
            stack.append(cur) # 压入栈中
            cur = cur.left # 去往左子树
        top = stack.pop() # 来到这里,说明当前节点为空,也就是当前节点的父节点无左子树,取出栈顶元素也就是当前节点的父节点
        cur = top.right # 往右子树的方向,若右子树为空则看栈是不是空,若栈不为空,再取出栈顶节点,访问其右子树
    return data

if __name__ == '__main__':
    a1 = TreeNode(5)
    a2 = TreeNode(3)
    a3 = TreeNode(7)
    a4 = TreeNode(2)
    a5 = TreeNode(4)
    a6 = TreeNode(6)
    a7 = TreeNode(8)
    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5
    a3.left = a6
    a3.right = a7
    PreOrder(a1)
    print(qianxu)
    print(Pre_Order(a1))

 

2、中序遍历(递归与非递归)

class TreeNode:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None

# 递归
zhongxu = []
def InOrder(root):
    if not root:
        return None
    InOrder(root.left)
    zhongxu.append(root.val)
    InOrder(root.right)

# 非递归
def In_Order(root):
    data = []
    stack = []
    cur = root
    while cur or stack:
        while cur:
            stack.append(cur)
            cur = cur.left
        top = stack.pop()
        data.append(top.val)
        cur = top.right
    return data

if __name__ == '__main__':
    a1 = TreeNode(5)
    a2 = TreeNode(3)
    a3 = TreeNode(7)
    a4 = TreeNode(2)
    a5 = TreeNode(4)
    a6 = TreeNode(6)
    a7 = TreeNode(8)
    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5
    a3.left = a6
    a3.right = a7
    InOrder(a1)
    print(zhongxu)
    print(In_Order(a1))

 

3、后序遍历(递归与非递归)

class TreeNode:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None

# 递归
houxu = []
def PastOrder(root):
    if not root:
        return None
    PastOrder(root.left)
    PastOrder(root.right)
    houxu.append(root.val)

# 非递归
def Past_Order(root):
    data = []
    stack = []
    stack.append(root)
    while stack:
        node = stack.pop()
        if not node:
            continue
        stack.append(node.left)
        stack.append(node.right)
        data.append(node.val)
    return data[::-1]


if __name__ == '__main__':
    a1 = TreeNode(5)
    a2 = TreeNode(3)
    a3 = TreeNode(7)
    a4 = TreeNode(2)
    a5 = TreeNode(4)
    a6 = TreeNode(6)
    a7 = TreeNode(8)
    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5
    a3.left = a6
    a3.right = a7
    PastOrder(a1)
    print(houxu)
    print(Past_Order(a1))

 

4、层序遍历

class TreeNode:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None

def Level_Order(root):
    if not root:
        return
    data = []
    queue = []
    queue.append(root)
    while queue: # 队列不为空,说明还没遍历完
        front = queue.pop(0) # 队列先进先出
        if front.left:
            queue.append(front.left) # 进左子节点
        if front.right:
            queue.append(front.right) # 进有右子节点
        data.append(front.val)
    return data

if __name__ == '__main__':
    a1 = TreeNode(5)
    a2 = TreeNode(3)
    a3 = TreeNode(7)
    a4 = TreeNode(2)
    a5 = TreeNode(4)
    a6 = TreeNode(6)
    a7 = TreeNode(8)
    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5
    a3.left = a6
    a3.right = a7
    print(Level_Order(a1))

 

你可能感兴趣的:(数据结构)