Python basic tutorial: Python implementation tree depth-first traversal and breadth-first traversal detailed explanation

@This article comes from the public number: csdn2299, like you can pay attention to the public number programmer academy
This article describes the depth-first traversal and breadth-first traversal of the Python implementation tree. Share with you for your reference, as follows:

Breadth first (level traversal)

Starting from the root of the tree, traversing the entire tree from top to bottom and from left to right
Insert picture description here
The difference between the number of nodes and the binary tree is that the binary tree has only two nodes on the left and right

Breadth priority: 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 kinds of depth-first algorithms: pre-order traversal, middle-order traversal, and post-order traversal. Insert picture description here
Root node-> left subtree-> 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 in-order traversal, we recursively use in-order traversal to access the left subtree, then access the root node, and finally recursively use in-order traversal to access the right subtree

Left subtree-> root node-> right subtree

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

Post-order traversal In post-order traversal, we first use recursive traversal to access the left and right subtrees, and finally access the root node

Left subtree-> right subtree-> root node

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

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

34 original articles published · Liked12 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105477085