[Frontiers of Binary Tree] Tree


insert image description here


1 The concept of a tree

A tree is a non-linear data structure, which is a set of hierarchical relationships composed of n (n>=0) finite nodes.It's called a tree because it looks like an upside-down tree, meaning it has the roots pointing up and the leaves pointing down

insert image description here

  • Trees have a special node calledroot nodeThe root node has no predecessors
  • Except the root node, the other nodes are divided into M (M>0) disjoint sets T1, T2, ..., Tm, each of which Ti (1<= i <= m) is a structure and Tree-like subtrees.The root node of each subtree has one and only one predecessor, and can have 0 or more successors.
     
    Tips: In the tree structure, there can be no intersection between subtrees, otherwise it is not a tree structure.
     
    insert image description here

2. Related concepts of trees

insert image description here

Degree of node : The number of subtrees contained in a node is called the degree of the node; as shown in the figure 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: nodes such as B, C, H, I, etc. are leaf nodes
 
③Non -terminal node or branch node : a node with a degree other than 0; as shown in the figure above : Nodes such as D, E, F, G... are branch nodes.
 
④Parent node or parent node : If a node contains child nodes, this node is called the parent node of its child nodes; as shown in the figure 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 in the figure above: B is the child node of A.
 
⑥Sibling nodes : Nodes with the same parent node are called sibling nodes; As shown in the figure above: B and C are sibling nodes
 
⑦Tree degree : In a tree, the degree of the largest node is called the degree of the tree; As shown in the above figure: tree The degree is 6.
 
⑧The hierarchy 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.
 
⑨Height or depth of the tree : the maximum level of nodes in the tree; as shown in the figure above: the height of the tree is 4.
 
⑩Cousin nodes : Nodes whose parents are on the same layer are cousins; as shown in the above figure: H and I are sibling nodes.
 
node's ancestors: All nodes on the branch from the root to this node; as shown in the figure 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 : A collection of m (m>0) disjoint trees is called a forest ;


3. Tree Representation

The tree structure is more complicated than the linear table, and it is more troublesome to store and express.Since the value range is saved, the relationship between nodes and nodes is also saved.
In practice, there are many ways to represent trees, such as: parent representation, child representation, child parent representation, and child sibling representation . Here we simply understand itMost commonly used child sibling notation

Code:

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

insert image description here


4. The practical use of trees (representing the directory tree structure of the file system)

insert image description here

In the above, first find the first child bin in the second row, and bin finds other sibling nodes in the second row through the sibling nodes.
And because the child pointer of the first node in line 2 is empty, the end is no longer visiting downwards.
Next, the second node in the second row finds the first node in the third row through the child pointer, and then finds other sibling nodes through the sibling nodes.
Other nodes continue to repeat the above process, and finally find all the data.


insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/132384104