this example for everyone to share a specific traversal code to implement Python two fork tree, for reference, the specific contents are as follows

 coding: UTF-8 # -*- -*- class Queue (object): def __init__ (self): self.q def [] = enqueue (self, item): self.q.append (item) def dequeue (self): self.q = if #! []: if len (self.q) > 0: return self.q.pop (0) else: return None def length (self): return len (self.q) def isempty (self): return len (self.q ==0) class Stack (object): def __init__ (self): self.s = push (self, item def []): self .s.append (item) def pop (self): if self.s! =[]: item = self.s.pop (-1) else: item None return item def = length (self): return len (self.s) def isempty (self): return self.s = [] def top (self): return self.s[-1] class TreeNode (object): def __init__ (self, data, left=None, right=None): self.data = data self.left = left self.right = right self.visited = False def setData (self, data): self.data = Data def setLeft (self, left): self.left = left def setRight (self, right): self.right = right def visit (self): print self.data self.visited = True def deVisit (self): self.visited = False class BinaryTree (obj ECT): def __init__ (self, root): self.root = root # preorder traversal (recursive) def freshVisit (self, node): if node is not None: (node.deVisit) if node.left: self.freshVisit (node.left) if node.right: self.freshVisit (node.right) # preorder traversal (recursive) def preOrder (self, node). If node is not None: (node.visit) if node.left: self.preOrder (node.left) if node.right: self.preOrder (node.right) # in order traversal (recursive) def inOrder (self, node): if node.left: self.inOrder (node.left) if node is not None: (node.visit) if node.right: self.inOrder (node.right) # traversal (recursive) def postOrder (self, node) If node.left: self.postOrder (node.left) if node.right: self.postOrder (node.right) if node is not None: node.visit (DEF orderTraveral) # recursive traversal (self, type): if type = 0: self.preOrder (self.root) elif type 1: self.inOrder (self.root) = elif type = = 2: self.postOrder (self.root) # preorder traversal (non recursive) # used a stack and a queue # first is the root node of the stack, the stack out of the stack # recycling element is not empty, then access # stack element has left child node into the stack, if there is a right child node into the queue # stack element is empty, then access the queue queue for # empty end loop, otherwise the queue element out of the team # access team elements, a team of elements A left child node into the stack, the team has the right child node elements into the queue # cycle until the last exit from def preOrderByNotRecursion (self): S = Stack (q) = Queue (q.enqueue) (self.root) while not s.isempty (or) not (q.isempty): if not (q.isempty): item = q.dequeue (item.visit (if)) item.left: q.enqueue (item.left) if item.right: s.push (item.right) elif not (s.isempty): item = s.pop (item.visit) (if) item.left: q.enqueue (item.left) if item.right: s.push (item.right) # preorder traversal (non recursive) # uses a stack # first is the root node stack, recycling # stack top stack element Access is not empty, then, have access # signs such as juxtaposition of the top of the stack has left child node stack # if the top element has access to the stack out of the stack is # elements if the right child node into the stack until the stack is no # cycle to exit the def preOrderByNotRecursion2 element (self): S = (Stack) s.push (self.root) while not (s.isempty): item = s.top (if) item.visited: (s.pop) if item.right: s.push (item.right) else: (item.visit) if item.left: s.push (item.left) # in order traversal (non recursive) # uses a stack # first root node into the stack, the stack if a # cycle the stack elements have left child node and left child node has not visited the stack is # instead, then Stack and stack access; if the element has a right child node stack # repeat the cycle until the stack is empty def inOrderByNotRecursion (self): S = Stack (s.push) (self.root) while not (s.isempty): item = s.top (item.left) while (and not item.left.visited): s.push (item.left) item = item.left else: = s.pop (item) item.visit (if) item.right: s.push (item.right) # postorder (non recursive) # uses a stack # first root node into the stack, the stack out of the stack # cycle if elements are left child node and left child node has not visited the stack is # on the other hand, if the stack the top element if the right child node and right child nodes have not visited, then stack # otherwise The stack, and access to # repeat the cycle until the stack is empty def postOrderByNotRecursion (self): S = Stack (s.push) (self.root) while not (s.isempty): item = s.top (item.left) while (and not item.left.visited): s.push (item.left) item = item.left else: if item.right and not item.right.visited: s.push (item.right) else: = s.pop (item) (item.visit) # level traversal (non recursive) # used a queue # first root node into the queue # remove an element from the queue, if the children left # access nodes into the team, such as the right child node is enqueued # repeat the operation until the queue into the empty def layerOrder (self): q = Queue (q.enqueue (s) Elf.root not q.isempty (while)): item (item.visit) = q.dequeue (if) item.left: q.enqueue (item.left) if item.right: q.enqueue (item.right) A B C # # # D E F G #H if __name__ = ='__main__': nE = TreeNode ('E'); nF = TreeNode; nG = TreeNode ('F') ('G'); nH = TreeNode; nD = ('H') TreeNode ('D', nH); nB = TreeNode ('B', nD, nE); nC = TreeNode ('C', nF, nG); nA = TreeNode ('A', nB, nC); bTree = BinaryTree (nA); # preorder recursive traversal print'---------- preorder traversal (recursive) -----------' bTree.orderTraveral (0) print'n---------- traversal (recursive) -----------'bTree.orderTraveral (1) print'n---------- traversal (recursive) -----------' BTree.orderTraveral (2) print'nn---------- preorder traversal (non recursive) -----------'print'---------- method -----------' bTree.freshVisit (bTree.root) bTree.preOrderByNotRecursion (print)'n---------- two -----------'bTree.freshVisit (bTree.root) bTree.preOrderByNotRecursion2 ('nn----------) print traversal (non recursive) -----------' bTree.freshVisit (bTree.root) bTree.inOrderByNotRecursion (print)'nn---------- after the traversal (non recursive) -----------'bTree.freshVisit (bTree.root) bTree.postOrderByNotRecursion (print)'nn---------- level traversal (non recursive) -----------' bTree.freshVisit (bTree.root) bTree.layerOrder (

)

above is the whole content of this article, I hope it will help you learn, and I hope you will support the script home.


This concludes the body part