Python Binary Tree definition and common traversal algorithm analysis, python Binary Tree
This example describes the definition of a Python binary tree and common Traversal Algorithms. We will share this with you for your reference. The details are as follows:
Speaking of binary tree traversal, the university talks about recursive algorithms. Most people first think of them as recursive algorithms. But as a programmer with ideals and pursuits. We should also learn non-recursive algorithms to implement binary tree traversal. Secondary stacks are required for non-recursive algorithms of Binary Trees. These algorithms are clever and wide open.
The following direct topics:
Define a binary tree, See the official imagination of its shape,
class BinNode( ): def __init__( self, val ): self.lchild = None self.rchild = None self.value = valbinNode1 = BinNode( 1 )binNode2 = BinNode( 2 )binNode3 = BinNode( 3 )binNode4 = BinNode( 4 )binNode5 = BinNode( 5 )binNode6 = BinNode( 6 )binNode1.lchild = binNode2binNode1.rchild = binNode3binNode2.lchild = binNode4binNode2.rchild = binNode5binNode3.lchild = binNode6
Sequential traversal:
'''First traverse the binary tree ''' def bin_tree_pre_order_traverse (root, visit_func): s = Stack () s. push (root) while not s. is_empty (): node = s. pop () visit_func (node) if node. rchild: s. push (node. rchild) if node. lchild: s. push (node. lchild)
In-order traversal:
'''Forward traversal Binary Tree ''' def bin_tree_in_order_traverse (root, visit_func): s = Stack () node = root while node or not s. is_empty (): if node: s. push (node) node = node. lchild else: node = s. pop () visit_func (node) node = node. rchild
Post-order traversal:
In the post-order traversal, make sure that both the left and right children have been accessed to access the root node, and the left child must be accessed before the right child, this brings difficulties to process control. The following two ideas are described.
Train of Thought 1, Double stack method, this method is easier to understand, the disadvantage is that two stacks are required.
'''Post-order traversal of Binary Tree ''' def bin_tree_post_order_traverse (root, visit_func): s1 = Stack () s2 = Stack () s1.push (root) while not s1.is _ empty (): node = s1.pop () s2.push (node) if node. lchild: s1.push (node. lchild) if node. rchild: s1.push (node. rchild) while not s2.is _ empty (): visit_func (s2.pop ())
Train of Thought 2Ensure that the root node can be accessed only after the Left and Right nodes are accessed. Therefore, for any node P, it is first written into the stack. If P does not have a left or right child, you can directly access it. Or P has a left or right child, but both the left and right children have been accessed, you can also directly access this node. If the two conditions are not described above, the right and left children of P will be added to the stack in sequence. This ensures that the left child will be accessed in front of the right child each time the top element of the stack is obtained, both the left and right children are accessed in front of the root node.
def bin_tree_post_order_traverse2( root, visit_func ): curr = root prev = None s = Stack() s.push( curr ) while not s.is_empty(): curr = s.peek() if ( not curr.lchild and not curr.rchild ) or ( prev and ( prev == curr.lchild or prev == curr.rchild ) ): visit_func( curr ) s.pop() prev = curr else: if curr.rchild: s.push( curr.rchild ) if curr.lchild: s.push( curr.lchild )
Sequence traversal:
def bin_tree_level_traverse( root, visit_func ): queue = Queue() queue.enqueue( root ) while not queue.is_empty(): node = queue.dequeue().value visit_func( node ) if node.lchild: queue.enqueue( node.lchild ) if node.rchild: queue.enqueue( node.rchild )