Python tree and tree algorithm

The concept of a tree: A tree is an abstract data type (ADT) that is used to simulate a data set with a tree-like structure. It consists of n> = 1 finite nodes to form a set with a hierarchical relationship
-each node has zero or Multiple child nodes;
—nodes without parent nodes are called root nodes
—each non-root node has only one parent node
—except for the root node, each child node can be divided into multiple disjoint subtrees
Insert picture description here

Some terms of the tree:
(1) The degree of a node: the number of subtrees contained in a node is called the degree of the node
(2) the degree of the tree: the degree of the largest node in a tree
(3) the leaf node: the degree Zero node
(4) Parent node: A node contains child nodes, then this node is called the node's child node
(5) Child node: A node contains a subtree node, called the node's child node
(6) Sibling nodes: Nodes with the same
parent node are called sibling (7) nodes: starting from the definition of the root node, the root is the first layer, the children of the root are the second layer, and so on
(8) Height or depth: the maximum level of the nodes in the tree
(9) cousin nodes: the nodes whose parent nodes are at the same level are cousins ​​of each other
(10) The ancestors of the node: all nodes on the branch from the root to the node
(11) descendants: Any node in a subtree rooted at a certain node is called the descendant
(12) forest of the node : the forest consists of a collection of n disjoint trees

Types of trees:
(1) Unordered tree: There is no order relationship between the child nodes of any node in the tree. This tree is called an unordered tree.
(2) Ordered tree:
– (1) Binary tree: each node contains at most The tree of two subtrees is called a binary tree. The types of binary trees are: complete binary tree, balanced binary tree, sorted binary tree
– (2) Huffman tree
– (3) B tree

Tree storage
(1) Sequential storage: storing the data structure in a fixed array, the traversal speed has advantages, but the space is relatively large
(2) chain storage: binary tree usually uses chain storage, the defect is the pointer field pointer The number of is indefinite, the solution: convert the multi-fork tree into a binary tree for processing

Since the number of nodes cannot be grasped, the storage representations of common trees are converted into binary trees for processing, and the number of child nodes is at most 2

Some common tree application scenarios
1.xml, html, etc., then when writing a parser for these things, it is inevitable to use the tree
2. The routing protocol is to use the tree algorithm
3.mysql database index 4.file
system directory Structure
5. So many classic AI algorithms are actually tree searches. In addition, the decision tree in machine learning is also a tree structure
. The use of trees in real life is very common

Binary tree: A binary tree is a tree structure with up to two subtrees per node. Subtrees are usually called left and right subtrees

The characteristics of the binary tree:
(1) Property 1: There are at most 2 ^ (i-1) nodes on the i-th layer of
the binary tree (2) The binary tree of depth k has at most 2 ^ i-1 node
(3) Any binary tree , If the number of leaf nodes is N0 and the total number of nodes with degree 2 is N1, then N0 = N1 + 1
(4) The depth of a complete binary tree with n nodes must be: log (n + 1)

Types of binary trees:
complete binary trees: except for the last layer, the number of nodes in each layer reaches the maximum number, and the leaf nodes are arranged in order from left to right

Full binary tree: every node except the leaf node has left and right cotyledons and the leaf node is at the bottom of the binary tree, each layer is full of nodes

Data structure of binary tree: python implementation

节点的数据结构:数据区、左链接区、右链接区
class Node(object):
    """节点类"""
    def __init__(self, elem=0, lchild=None, rchild=None):
        self.elem = elem
        self.lchild = lchild
        self.rchild = rchild
 树的创建,创建一个树结构,添加根节点,开始为空树,然后按照广度优先(层序遍历:横向)来添加节点
 【拓展: ---深度优先(纵向:通常有三种遍历方式)】
 class Tree(object):
    """树类"""
    def __init__(self, root=None):
        self.root = root
   """通过层序遍历来添加节点"""

    def add(self, elem):
        """为树添加节点"""
        node = Node(elem)
        #如果树是空的,则对根节点赋值
        if self.root == None:
            self.root = node
        else:
            queue = []
            queue.append(self.root)
            #对已有的节点进行层次遍历
            while queue:
                #弹出队列的第一个元素
                cur = queue.pop(0)
                if cur.lchild == None:
                    cur.lchild = node
                    return
                elif cur.rchild == None:
                    cur.rchild = node
                    return
                else:
                    #如果左右子树都不为空,加入队列继续判断
                    queue.append(cur.lchild)
                    queue.append(cur.rchild)
Published 129 original articles · Like 43 · Visits 100,000+

Guess you like

Origin blog.csdn.net/nbxuwentao/article/details/104606165