Python 实现二叉树四种遍历算法

遍历二叉树也就是按照某种次序,顺着制定的搜索路径访问二叉树中的各个结点,该过程中每个结点被且仅被访问一次。

根据二叉树的结构特征,可以有两种搜索路径,广度优先遍历和深度优先遍历。

形如:

《Python 实现二叉树四种遍历算法》

首先构造二叉树结点类

'''
二叉树结点
'''  
class TreeNode:
    def __init__(self,val=None,left=None,right=None):
        self.val = val
        self.left = left
        self.right = right
    def settag(self,tag=None):
        self.tag = tag

def visit(treenode):
    print(str(treenode.val),end=' ')

广度优先遍历,也是层次遍历,自上而下,自左向右,使用队列数据结果实现:

import queue
'''
广度优先遍历
'''  
def levelOrder(root):
    deque = queue.Queue()
    if(root is not None):
        deque.put(root)
    while(not deque.empty()):
        treenode = deque.get()
        visit(treenode)
        if(treenode.left is not None):
            deque.put(treenode.left)
        if(treenode.right is not None):
            deque.put(treenode.right)

深度优先遍历——递归遍历

'''
递归深度优先遍历——前序遍历
''' 
def RecursionPreOrder(root):
    if(root is not None):
        visit(root)
        RecursionPreOrder(root.left)
        RecursionPreOrder(root.right)
'''
递归深度优先遍历——中序遍历
''' 
def RecursionInOrder(root):
    if(root is not None):
        RecursionInOrder(root.left)
        visit(root)
        RecursionInOrder(root.right)
'''
递归深度优先遍历——后序遍历
''' 
def RecursionPostOrder(root):
    if(root is not None):
        RecursionPostOrder(root.left)
        RecursionPostOrder(root.right)
        visit(root)

深度优先遍历——非递归遍历

import queue
'''
非递归深度优先遍历——前序遍历
使用栈数据结构保存结点信息
''' 
def PreOrderWithoutRecursion(root):
    stacknode = queue.LifoQueue()
    while(root is not None or not stacknode.empty()):
        if(root is not None):
            visit(root)
            stacknode.put(root.right)
            root = root.left
        else:
            root = stacknode.get()
'''
非递归深度优先遍历——中序遍历
使用栈数据结构保存结点信息
''' 
def InOrderWithoutRecursion(root):
    stacknode = queue.LifoQueue()
    while(root is not None or not stacknode.empty()):
        if(root is not None):
            stacknode.put(root)
            root = root.left
        else:
            root = stacknode.get()
            visit(root)
            root = root.right
tags = {'left':'Left','right':'Right'}
'''
非递归深度优先遍历——后序遍历
使用栈数据结构保存结点信息
对父节点已访问过的左右子树作标记
''' 
def PostOrderWithoutRecursion(root):
    stacknode = queue.LifoQueue()
    while(root is not None or not stacknode.empty()):
        while(root is not None):
            root.tag = tags['left']
            stacknode.put(root)
            root = root.left
        root = stacknode.get()
        if(root.tag == tags['left']):
            root.tag = tags['right']
            stacknode.put(root)
            root = root.right
        else:
            visit(root)
            root = None
'''
非递归深度优先遍历——后序遍历
使用栈数据结构保存结点信息
保存前一个被访问过的结点
''' 
def PostOrderWithoutRecursion_1(root):
    stacknode = queue.LifoQueue()
    pre = root
    while(root is not None):
        while(root.left is not None):
            stacknode.put(root)
            root = root.left
        while(root is not None and (root.right is None or root.right == pre)):#当前结点没有右孩子或者右孩子刚被访问过,则访问改结点
            visit(root)
            pre = root
            if(stacknode.empty()): 
                return
            root = stacknode.get()
        stacknode.put(root)
        root = root.right

测试代码

if __name__=='__main__':
    a = TreeNode(1)
    b = TreeNode(2)
    c = TreeNode(3)
    d = TreeNode(4)
    e = TreeNode(5)
    f = TreeNode(6)
    g = TreeNode(7)
    a.left = b
    a.right = c
    b.right = d
    c.left = e
    c.right = f
    f.left = g
    print("levelOrder:",end=' ') 
    levelOrder(a) 
    print("\nRecursionPreOrder:",end=' ')
    RecursionPreOrder(a)
    print("\nRecursionInOrder:",end=' ')
    RecursionInOrder(a)
    print("\nRecursionPostOrder:",end=' ')
    RecursionPostOrder(a)
    print("\nPreOrderWithoutRecursion:",end=' ')
    PreOrderWithoutRecursion(a)
    print("\nInOrderWithoutRecursion:",end=' ')
    InOrderWithoutRecursion(a)
    print("\nPostOrderWithoutRecursion:",end=' ')
    PostOrderWithoutRecursion(a)
    print("\nPostOrderWithoutRecursion_1:",end=' ')
    PostOrderWithoutRecursion_1(a)

代码运行结果:

levelOrder: 1 2 3 4 5 6 7 
RecursionPreOrder: 1 2 4 3 5 6 7 
RecursionInOrder: 2 4 1 5 3 7 6 
RecursionPostOrder: 4 2 5 7 6 3 1 
PreOrderWithoutRecursion: 1 2 4 3 5 6 7 
InOrderWithoutRecursion: 2 4 1 5 3 7 6 
PostOrderWithoutRecursion: 4 2 5 7 6 3 1 
PostOrderWithoutRecursion_1: 4 2 5 7 6 3 1 

 

    原文作者:yht201293018
    原文地址: https://blog.csdn.net/yht201293018/article/details/81198827
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞