basic programming python: python realize the tree depth-first traversal and breadth-first traversal Comments

This paper describes examples of depth-first traversal and breadth-first traversal tree python achieve. Share to you for your reference, as follows:

Breadth-first (hierarchy traversal)

Starting root node of the tree, the tree from top to bottom from left to right through the entire
Here Insert Picture Description
difference between the number and the binary tree is the binary tree is only about two nodes

Breadth-first order: A - B - C - D - E - F - G - H - I

Code

def breadth_travel(self, root):
    """利用队列实现树的层次遍历"""
    if root == None:
      return
    queue = []
    queue.append(root)
    while queue:
      node = queue.pop(0)
      print node.elem,
      if node.lchild != None:
        queue.append(node.lchild)
      if node.rchild != None:
        queue.append(node.rchild)

Depth-first

There are three depth-first algorithm: preorder traversal, in order traversal, postorder
Here Insert Picture Description
preorder traversal of the earlier order, we first visit the root, then recursively preorder visit the left subtree, then recursively preorder access right subtree

Root -> left sub-tree -> right subtree

#实现 1
def preorder(self, root):
   """递归实现先序遍历"""
   if root == None:
     return
   print root.elem
   self.preorder(root.lchild)
   self.preorder(root.rchild)
#实现 2
def depth_tree(tree_node):
  if tree_node is not None:
    print (tree_node._data)
    if tree_node._left is noe None:
      return depth_tree(tree_node._left)
    if tree_node._right is not None:
      return depth_tree(tree_node._right)

In order traversal in preorder, we use a recursive preorder visit the left subtree, then visit the root node, and finally the use of recursive preorder access right subtree

Left subtree -> root -> right subtree

def inorder(self, root):
   """递归实现中序遍历"""
   if root == None:
     return
   self.inorder(root.lchild)
   print root.elem
   self.inorder(root.rchild)

After order traversal in postorder, we first recursive traversal sequence after use to access the left subtree and right subtree, root last visit

Left sub-tree -> right subtree -> root

def postorder(self, root):
   """递归实现后续遍历"""
   if root == None:
     return
   self.postorder(root.lchild)
   self.postorder(root.rchild)
   print root.elem

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, and share some learning methods need to pay attention to small detailsHere Insert Picture Description

Released six original articles · won praise 0 · Views 8

Guess you like

Origin blog.csdn.net/chengxun02/article/details/104976398