二叉树遍历的 python 实现 (先序,中序,后序,层次,深度优先)

二叉树遍历的实现

本文是看完一篇介绍二叉树遍历的博客后所写的 python 代码,如果要看具体原理,请移步原博客。

python 代码

import queue

class TreeNode(object):
    def __init__(self, x=-1, left=None, right=None):
        self.val = x
        self.left = left
        self.right = right

def preorder_traversal_recursive(tree):
    if tree is None:
        return
    ans.append(tree.val)
    preorder_traversal_recursive(tree.left)
    preorder_traversal_recursive(tree.right)

def preorder_traversal_noRecursive(tree):
    cur_tree = tree
    tree_stack = []
    while (cur_tree is not None) or (tree_stack):
        if cur_tree is not None:
            ans.append(cur_tree.val)
            tree_stack.append(cur_tree)
            cur_tree = cur_tree.left
        else:
            cur_tree = tree_stack.pop()
            cur_tree = cur_tree.right

def inorder_traversal_recursive(tree):
    if tree is None:
        return
    inorder_traversal_recursive(tree.left)
    ans.append(tree.val)
    inorder_traversal_recursive(tree.right)

def inorder_traversal_noRecursive(tree):
    cur_tree = tree
    tree_stack = []
    while (cur_tree is not None) or (tree_stack):
        if cur_tree is not None:
            tree_stack.append(cur_tree)
            cur_tree = cur_tree.left
        else:
            cur_tree = tree_stack.pop()
            ans.append(cur_tree.val)
            cur_tree = cur_tree.right

def postorder_traversal_recursive(tree):
    if tree is None:
        return
    postorder_traversal_recursive(tree.left)
    postorder_traversal_recursive(tree.right)
    ans.append(tree.val)

def postorder_traversal_noRecursive(tree):
    cur_tree = tree
    tree_stack = []
    pre = TreeNode()
    while (cur_tree is not None) or (tree_stack):
        if cur_tree is not None:
            tree_stack.append(cur_tree)
            cur_tree = cur_tree.left
        else:
            cur_tree = tree_stack.pop()
            if cur_tree.right is None or cur_tree.right==pre:
                ans.append(cur_tree.val)
                pre = cur_tree
                cur_tree = None
            else:
                tree_stack.append(cur_tree)
                cur_tree = cur_tree.right
                
def level_traversal(tree):
    cur_queue = queue.Queue()
    cur_queue.put(tree)
    while not cur_queue.empty():
        tmp = cur_queue.get()
        if tmp.left is not None:
            cur_queue.put(tmp.left)
        if tmp.right is not None:
            cur_queue.put(tmp.right)
        ans.append(tmp.val)

def depthorder_traversal(tree):
    tree_stack = [tree]
    while tree_stack:
        tree_tmp = tree_stack.pop()
        ans.append(tree_tmp.val)
        if tree_tmp.right is not None:
            tree_stack.append(tree_tmp.right)
        if tree_tmp.left is not None:
            tree_stack.append(tree_tmp.left)
            

ans = []

traversal_candidates = {
     'preorder_recursive': preorder_traversal_recursive,
                        'preorder_noRecursive': preorder_traversal_noRecursive,
                        'inorder_recursive': inorder_traversal_recursive,
                        'inorder_noRecursive': inorder_traversal_noRecursive,
                        'postorder_recursive': postorder_traversal_recursive,
                        'postorder_noRecursive': postorder_traversal_noRecursive,
                        'level': level_traversal,
                        'depthorder': depthorder_traversal}

def main():
    binary_tree = TreeNode(1, left=TreeNode(2, left=TreeNode(4)
                            , right=TreeNode(5, left=TreeNode(7),
                            right=TreeNode(8))), right=TreeNode
                           (3, right=TreeNode(6)))
    

    traversal_candidates['depthorder'](binary_tree)
    print(ans)

if __name__ == '__main__':
    main()

你可能感兴趣的:(Python,编程,python,算法,二叉树)