The concept and structure of the tree (an article is enough to let you know the tree)

Jingle, Jingle

The old iron who is learning data structure, come here

Do you also have such troubles

The data structure taught by the school teacher is very superficial

I bought too many books in written language, and I can't understand them.

Especially when it comes to the tree chapter, the whole person is blinded

If you have the same trouble, then you can click in now!

Pay attention to the high energy ahead, don't be dazed

Next, let's get into today's high energy! ! !


tree concept

 

A tree is a nonlinear data structure, which consists of n (n>=0) finite nodes to form a set with hierarchical relationship. It is called a tree because it looks like an upside-down tree, which means it has the roots up and the leaves down.

  • There is a special node, called the root node, the root node has no predecessor node, but can have successor nodes
  • All nodes except the root node have unique predecessors, and all nodes can have 0 or more successor nodes
  • Trees are defined recursively

Trees and non-trees? ? ?

The characteristics of the tree: ① The subtrees do not intersect

                  ② Except for the root node, each node has and only one parent node (the root node has no parent node)

                  ③ A tree with N nodes has N-1 edges

 

The relationship of each node of the tree 

 

Degree of a node: The number of subtrees contained in a node is called the degree of the node; as shown above: A is 6

Leaf node or terminal node: a node with a degree of 0 is called a leaf node; as shown in the figure above: B, C, H, I... and other nodes are leaf nodes

Non-terminal nodes or branch nodes: nodes whose degree is not 0; as shown in the figure above: D, E, F, G... and other nodes are branch nodes

Parent node or parent node: If a node contains a child node, the node is called the parent node of its child node; as shown above: A is the parent node of B

Child node or child node: The root node of the subtree contained in a node is called the child node of the node; as shown above: B is the child node of A

Sibling nodes: nodes with the same parent node are called sibling nodes; as shown above: B and C are sibling nodes 

The degree of the tree: in a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 6

The level of nodes: starting from the definition of the root, the root is the first layer, the child nodes of the root are the second layer, and so on;

The height or depth of the tree: the maximum level of nodes in the tree; as above: the height of the tree is 4 

Ancestors of a node: all nodes on the branch from the root to the node; as shown above: A is the ancestor of all nodes

Descendants: Any node in the subtree rooted at a node is called the descendant of the node. As shown above: all nodes are descendants of A

Forest: The set of m (m>0) trees that do not intersect with each other is called a forest; (the learning and search in the data structure is essentially a forest)

 

 representation of tree

The tree structure is more complicated than the linear table, and it is more troublesome to store and represent. In practice, there are many representations of trees, such as parent representation, child sibling representation, child representation and so on.

 Let's take a look at the child's brother notation and parents' notation here

 ①Children's brother representation:

typedef int DataType;
struct Node
{
    struct Node* _firstChild1;    // 第一个孩子结点
    struct Node* _pNextBrother;   // 指向其下一个兄弟结点
    DataType _data;               // 结点中的数据域
};

 

 ②Parents notation

 

The use of the tree in practice (representing the directory tree structure of the file system) 

 

QQ:2186529582

Next blog post: Binary tree remember to join us! ! !

 

 

 

Guess you like

Origin blog.csdn.net/m0_66488562/article/details/123632852