Create a binary tree recursive and non-recursive traversal

Binary tree is traversed normal operation, this is one of the basic skills of a binary tree

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

class BinaryTree(object):
    def __init__(self, pre, tin):
        self.pre = pre
        self.tin = Tin
        self.levelTrave = [] # hierarchy traversal results 
        self.preTraveRecur = [] # preamble recursive traversal results 
        self.preTraveNoRecur = [] # preamble non-recursive traversal results 
        self.inTraveRecur = [] # in order recursive traversal results 
        self.inTraveNoRecur = [] # in order non-recursive traversal results 
        self.postTraveRecur = [] # postorder recursive traversal results 
        self.postTraveNoRecur = [] # post-order non-recursive traversal results

    # Before use preorder traversal results and reconstructed binary sequence 
    DEF reConstructBinaryTree (Self, pre, tin): # pre result of a preorder traversal, tin preorder result 
        IF len (pre) == 0:
             return None
         IF len ( pre) ==. 1 :
             return the TreeNode (pre [0])
         the else :
            res = TreeNode(pre[0])
            res.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0]) + 1], tin[:tin.index(pre[0])])
            res.right = self.reConstructBinaryTree(pre[tin.index(pre[0]) + 1:], tin[tin.index(pre[0]) + 1:])
        return res

    # Traverse the level 
    DEF levelTraversal (Self, PROOT):
        aux = []
         IF PROOT = None:! # non-empty root node is added to the list aux 
            aux = [PROOT]

        the while len (aux): # determine whether there are elements aux list, if there are elements continue to visit, or to traverse the end of 
            the p-aux.pop = (0) # Analog queue each time to take the first element of the list 
            self.levelTrave.append (p. Val)
             IF p.left = None:! # if the current element has left the first node to join aux end of the list 
                aux.append (p.left)
             IF p.right = None:! # If the current first element has to be the right node Join aux end of the list 
                aux.append (p.right)

    # Pre-order recursive traversal 
    DEF preorderTraversalRecursion (Self, PROOT):
         IF PROOT == None:
             return 
        self.preTraveRecur.append (pRoot.val) # ! ! ! ! ! ! ! 
        IF pRoot.left =! None:
            self.preorderTraversalRecursion(pRoot.left)
        if pRoot.right != None:
            self.preorderTraversalRecursion(pRoot.right)

    # Preamble non-recursive traversal 
    DEF preorderTraversalNoRecursion (Self, PROOT):
         IF PROOT == None:
             return 
        AUX = []
        the Node = PROOT
         the while the Node or the AUX:
             the while the Node:
                 # starting from the root, has been looking for its left subtree 
                self.preTraveNoRecur.append (node.val)
                aux.append (node)
                node = node.left
                 # the while end node indicates that the current node is empty, that before a tree node has no left child of 
            node = aux.pop ()
             # start looking at it right subtree 
            node = node.right

    # In order recursive traversal 
    DEF inorderTraversalRecursion (Self, PROOT):
         IF PROOT == None:
             return 
        IF ! PRoot.left = None:
            self.inorderTraversalRecursion(pRoot.left)
        self.inTraveRecur.append(pRoot.val) # !!!!!!!!
        if pRoot.right != None:
            self.inorderTraversalRecursion(pRoot.right)

    # The non-recursive traversal sequence 
    DEF inorderTraversalNoRecursion (Self, PROOT):
         IF the root == None:
             return 
        AUX = []
        the Node = root
         the while the Node or the AUX:
             the while the Node:
                 #   starting from the root, has been found in the left subtree 
                aux.append (node)
                node = node.left
             # the while end node indicates that the current node is empty, that before a tree node has no left child of 
            node = aux.pop ()
            self.inTraveNoRecur.append(node.val)
            node = node.right

    # Postorder traversal recursion 
    DEF postorderTraversalRecursion (Self, PROOT):
         IF PROOT == None:
             return 
        IF ! PRoot.left = None:
            self.postorderTraversalRecursion(pRoot.left)
        if pRoot.right != None:
            self.postorderTraversalRecursion(pRoot.right)
        self.postTraveRecur.append(pRoot.val) # !!!!!!

    # The non-recursive traversal sequence 
    DEF postorderTraversalNoRecursion (Self, PROOT):
         IF the root == None:
             return 
        AUX = []
        the Node = root
         the while the Node or the AUX:
             the while the Node:
                 # starting from the root, has been looking for its right subtree 
                self.postTraveNoRecur.insert (0, node.val)
                aux.append (node)
                node = node.right
             # the while end node indicates that the current node is empty, that before a node does not have the right child 
            node = aux.pop ()
             # start viewing its left sub-tree 
            node = node.left

IF  the __name__ == ' __main__ ' :
     # using the preamble and the reconstructed binary preorder 
    pre = [. 1, 2,. 4,. 7,. 3,. 5,. 6,. 8 ]
    message = [4, 7, 2, 1, 5, 3, 8, 6 ]
    BT = BinaryTree (for, tin)
    root = BT.reConstructBinaryTree (BT.pre, BT.tin)

    # Traverse the level 
    BT.levelTraversal (root)
     Print ( " level through the results: " , BT.levelTrave)

    # Pre-order recursive traversal 
    BT.preorderTraversalRecursion (root)
     Print ( " pre-order recursive traversal: " , BT.preTraveRecur)

    # Preamble non-recursive traversal 
    BT.preorderTraversalNoRecursion (root)
     Print ( " preamble non-recursive traversal: " , BT.preTraveNoRecur)

    # In order recursive traversal 
    BT.inorderTraversalRecursion (root)
     Print ( " in order recursive traversal: " , BT.inTraveRecur)

    # The non-recursive traversal order 
    BT.inorderTraversalNoRecursion (the root)
     Print ( " the non-recursive traversal order " , BT.inTraveNoRecur)

    # Order recursive traversal after 
    BT.postorderTraversalRecursion (root)
     Print ( " post-order recursive traversal " , BT.postTraveRecur)

    # Sequence after non-recursive traversal 
    BT.postorderTraversalNoRecursion (the root)
     Print ( " postorder traversal non-recursive " , BT.postTraveNoRecur)

 

Guess you like

Origin www.cnblogs.com/shnuxiaoan/p/12241694.html