In this article, the example for you to share the python implementation of binary tree traversal specific code for your reference, the details are as follows
Code:
# -*- coding: gb2312 -*- class Queue(object): def __init__(self): = [] def enqueue(self, item): (item) def dequeue(self): # if != []: if len()>0: return (0) else: return None def length(self): return len() def isempty(self): return len()==0 class Stack(object): def __init__(self): = [] def push(self, item): (item) def pop(self): if !=[]: item = (-1) else: item = None return item def length(self): return len() def isempty(self): return == [] def top(self): return [-1] class TreeNode(object): def __init__(self, data, left=None, right=None): = data = left = right = False def setData(self, data): = data def setLeft(self, left): = left def setRight(self, right): = right def visit(self): print , = True def deVisit(self): = False class BinaryTree(object): def __init__(self, root): = root # Pre-order traversal (recursive) def freshVisit(self, node): if node is not None: () if : () if : () # Pre-order traversal (recursive) def preOrder(self, node): if node is not None: () if : () if : () # Middle-order traversal (recursion) def inOrder(self, node): if : () if node is not None: () if : () # Posterior traversal (recursive) def postOrder(self, node): if : () if : () if node is not None: () # Recursive traversal def orderTraveral(self, type): if type == 0: () elif type == 1: () elif type == 2: () # Pre-order traversal (non-recursive) # With a stack and a queue # First the root node goes on the stack, then it loops off the stack # # The outgoing stack element is not empty, then access the # Outgoing elements are put on the stack if they have a left child node, or on the queue if they have a right child node # Outgoing stack element is empty, then access queue # End the loop if the queue is also empty, otherwise the queue elements are out of the queue # access to the outgoing element, the outgoing element has a left child node into the stack, the outgoing element has a right child node into the queue # Loop until the final exit def preOrderByNotRecursion(self): s = Stack() q = Queue() () while not () or not (): if not (): item = () () if : () if : () elif not (): item = () () if : () if : () # Pre-order traversal (non-recursive) # With a stack # First the root node goes on the stack, then it loops off the stack # # If the top element of the stack is not empty, then access it, and set the accessed flag. # If the top element of the stack has a left child node then it goes on the stack # If the top element of the stack has been accessed, exit the stack # Outgoing elements go onto the stack if they have a right child node # Loop until the stack is empty def preOrderByNotRecursion2(self): s = Stack() () while not (): item = () if : () if : () else: () if : () # Middle-order traversal (non-recursive) # With a stack # First put the root node on the stack and loop off the stack # If the outgoing element has a left child node and the left child node has not been accessed then it goes on the stack # Instead, out of the stack and access; in if the out element has a right child node # Repeat until the stack is empty def inOrderByNotRecursion(self): s = Stack() () while not (): item = () while( and not ): () item = else: item = () () if : () # Posterior traversal (non-recursive) # With a stack # First put the root node on the stack and loop off the stack # If the outgoing element has a left child node and the left child node has not been accessed then it goes on the stack # Conversely, if the top element of the stack if it has a right child node and the right child node has not been visited, it goes on the stack # Otherwise, get off the stack and visit # Repeat until the stack is empty def postOrderByNotRecursion(self): s = Stack() () while not (): item = () while( and not ): () item = else: if and not : () else: item = () () # Hierarchical traversal (non-recursive) # With a queue # First put the root node in the queue # Remove an element from the queue, access # If there is a left child node it goes into the queue, if there is a right child node it goes into the queue # Repeat until the queue is empty def layerOrder(self): q = Queue() () while not (): item = () () if : () if : () # A # B C # D E F G #H if __name__ == '__main__': nE = TreeNode('E'); nF = TreeNode('F'); nG = TreeNode('G'); nH = TreeNode('H'); nD = TreeNode('D', nH); nB = TreeNode('B', nD, nE); nC = TreeNode('C', nF, nG); nA = TreeNode('A', nB, nC); bTree = BinaryTree(nA); # Pre-order recursive traversal print '----------preorder traversal(recursive (calculation))-----------' (0) print '\n----------medium-order traversal(recursive (calculation))-----------' (1) print '\n----------backward traversal(recursive (calculation))-----------' (2) print '\n\n----------preorder traversal(非recursive (calculation))-----------' print '----------Method 1-----------' () () print '\n---------- Method II -----------' () bTree.preOrderByNotRecursion2() print '\n\n----------medium-order traversal(非recursive (calculation))-----------' () () print '\n\n----------backward traversal(非recursive (calculation))-----------' () () print '\n\n----------hierarchical traversal(非recursive (calculation))-----------' () ()
Results:
This is the entire content of this article.