Data Structures and Algorithms 1: front binary tree (binary_tree), and in the order (depth first) and breadth-first traversal and code for python

Front binary tree defined in preorder

Here Insert Picture Description

  1. Tree (English: Tree) is a non-presence of any of the only route to FIG. (Undirected graph), between two vertices. Or, as long as no circuit connected graph is the tree
  2. Binary tree (English: Binary tree) is at most two branches per node (in the absence of branching greater than 2 nodes)
    of the tree structure. Branch is commonly referred to as "left subtree" and "right subtree." Binary tree has branches around the order, it can not be reversed.
  3. Complete binary tree: the leaf nodes only occur once in the lower layer and the lowermost layer and the bottom layer of the binary tree nodes are concentrated in a plurality of positions of the leftmost layer.
  4. Balanced binary tree: it is the absolute value of the difference in height empty tree or its two left and right subtrees of not more than 1, and the left and right sub-trees are a balanced binary tree.

For traversal of the tree, there are two methods:
The depth-first traversal: including front, in the three kinds of traversal order;
6. breadth traversal: that is what we usually traverse the level (other data structure to be supported, such as the queue )

  1. Preorder traversal: root ----> left subtree ----> right subtree (for the above picture of the tree traversal result is: EBADCFHGI)
  2. Preorder: left subtree ----> root node ----> right subtree (for the above picture of the tree traversal result is: ABCDEFGHI)
  3. Follow-up traversal: left subtree ----> right subtree ----> root (for the above picture of the tree traversal result is: ACDBGIHFE)
  4. Breadth-first traversal: traverse the level, layer by layer traversal (for the above picture tree traversal result is: EBFADHCGI)
    It should be noted that the most common preorder

Creating trees and tree traversal recursive method

class Node(object):
    """创建一个构造树的类"""
    def __init__(self, value=None, left=None, right=None):
        super(Node, self).__init__()
        self.value = value
        self.left = left    # 左子树
        self.right = right  # 右子树
def PreOrder(root):
    '''
    前序遍历
    '''
    if root == None:
        return 
    print(root.value)
    PreOrder(root.left)
    PreOrder(root.right)
def InOrder(root):
    '''
    中序遍历
    '''
    if root == None:
        return
    InOrder(root.left)
    print(root.value)
    InOrder(root.right)
def Subsequent(root):
    '''
    后序遍历
    '''
    if root == None:
        return
    Subsequent(root.left)
    Subsequent(root.right)
    print(root.value)
def LevelTraaverse(root):
    '''
    广度优先遍历:层次遍历
    '''
    if root == None:
        return
    queue = []          # 新建一个队列
    queue.append(root)  # 将根节点入队
    while len(queue):   # 只要队列不为空,一直循环
        lenth_q = len(queue)
        for i in range(lenth_q):
            r = queue.pop(0)            # 根节点出队,并存放于r
            if r.left is not None:
                queue.append(r.left)    # 左节点入队
            if r.right is not None:
                queue.append(r.right)   # 右节点入队
            print(r.value) 

main functions:

if __name__=='__main__':
    root=Node('E',left=Node('B',left=Node('A'),right=Node('D',left=Node('C'))), right=Node('F',right=Node('H',left=Node('G'),right=Node('I'))))
    print("PreOrder traversal result:")
    PreOrder(root)
    print("InOrder traversal result:")
    InOrder(root)
    print("Subsequent traversal result:")
    Subsequent(root)
    print("LevelTraaverse result:")
    LevelTraaverse(root)

result:
Here Insert Picture Description

Reference:
[1]. Depth study binary tree (a) binary basis
[2]. Binary tree traversal (preamble, in sequence, after, traverse the level, depth-first, breadth-first)

Guess you like

Origin blog.csdn.net/weixin_43038300/article/details/95312115