Tree storage structure-data structure


Preface

The logical structure of the data

  1. Linear structure

  2. Non-linear structure

    • Tree structure
    • Graphic structure

The tree structure introduced in this chapter is a non-linear storage structure that stores a collection of data elements with a "one-to-many" relationship.


1. Definition of tree

Tree structure

  • Branch between nodes
  • Has a hierarchical relationship

As shown in the figure:
Insert picture description here
Tree: is a finite set of n (n >= 0) nodes.

If n = 0, it is called an empty tree.

If n> 0, then it meets the following two conditions:

  1. There is one and only one specific node called the root (Root).
  2. The remaining nodes can be divided into m (m >= 0) disjoint finite sets T1, T2, T3,..., Tm. Each set is itself a tree, and is called the subtree of the root (SubTree ). For example: The tree composed of nodes B, E, F, K, and L is a subtree of the entire tree.

For data A, it is related to data B, C, and D; for data B, it is related to E and F. This is the "one-to-many" relationship.

Second, the nodes of the tree

Node : Each data element stored in a tree structure is called a "node". For example, in Figure 1(A), data element A is a node;

Parent node (parent node), child node and sibling node : For nodes A, B, C, and D in Figure 1 (A), A is the parent node of nodes B, C, and D (Also called "parent node"), and B, C, and D are all child nodes of node A (also called "child node"). For B, C, and D, they all have the same parent node, so they are siblings to each other.

Tree root node (referred to as "root node"): Every non-empty tree has and only one node called the root. In Figure 1(A), node A is the root node of the entire tree.

The basis for judging the root of the tree is: if a node does not have a parent node, then this node is the root node of the entire tree.

Leaf node : If the node does not have any child nodes, then this node is called a leaf node (leaf node). For example, in Figure 1(A), nodes K, L, F, G, M, I, and J are all leaf nodes of this tree.

Three, subtree and empty tree

Subtree : As shown in Figure 1(A), the root node of the entire tree is node A, and if you look at the parts composed of nodes B, E, F, K, and L, it is also a tree, and the nodes B is the root node of this tree. Therefore, the tree composed of the nodes B, E, F, K, and L is called the subtree of the entire tree; similarly, the node E, K, and L is also a subtree, and the root node is E.

Note: A single node is also a tree, but the root node is itself. In Figure 1(A), nodes K, L, F, etc. are all trees, and they are all subtrees of the entire tree.

After knowing the concept of subtree, the tree can also be defined like this: a tree is composed of a root node and several subtrees.

Empty tree : If the set itself is empty, then the formed tree is called an empty tree. There are no nodes in the empty tree.

Supplement: In the tree structure, each subtree with the same root node cannot overlap each other. For example, in Figure 1(A), in addition to the root node A, the remaining elements each constitute three subtrees. The root nodes are B, C, and D. These three subtrees do not have the same nodes. If there is, it destroys the structure of the tree and cannot be counted as a tree.

Fourth, the degree and level of nodes

For a node, the number of subtrees (how many branches the node has) is called the degree of the node (Degree). For example, in Figure 1(A), there are 3 subtrees under the root node A, so the degree of node A is 3.

The degree of a tree is the maximum value of the degree of each node in the tree. Tree 1 (A) shows, the maximum value of each node is 3, so the whole grain of the tree value is 3.

Node level : starting from the root of a tree, the root layer is the first layer, the root child nodes are the second layer, and so on. For Figure 1(A), node A is in the first layer, B, C, and D are in the second layer, E, F, G, H, I, and J are in the third layer, and K, L, and M are in the third layer. Four floors.

The depth (height) of a tree is the largest level of nodes in the tree. The depth of the tree in Figure 1(A) is 4.

If the parent nodes of two nodes are not the same, but their parent nodes are at the same level, then the two nodes are cousins ​​of each other . For example, in Figure 1(A), the parent nodes of node G and E, F, H, I, and J are all in the second layer, so the relationship between them is cousins.

Fourth, ordered trees and unordered trees

If the subtrees of the nodes in the tree are viewed from left to right, there are regulations on who is on the left and who is on the right. This tree is called an ordered tree; otherwise, it is called an unordered tree.

In an ordered tree, the leftmost subtree of a node is called the "first child", and the rightmost subtree is called the "last child".

Take Figure 1(A) as an example, if it is an ordered tree itself, the subtree with node B as the root node is the first child of the whole tree, and node D is the root node The child tree of is the last child of the whole tree.

Five, forest

A collection of m (m >= 0) disjoint trees is called a forest. In Figure 1(A), the three subtrees with B, C, and D as the root nodes can be called forests.

As mentioned earlier, a tree can be understood as being composed of a root node and a number of subtrees, and these subtrees are themselves a forest, so a tree can also be understood as being composed of a root node and a forest. Expressed by a formula: Tree = (root, F)

Among them, root represents the root node of the tree, and F represents the forest composed of m (m >= 0) trees.

Six, other representations of the tree

In addition to the tree representation in Figure 1 (A), there are other representation methods:
Insert picture description here
Figure 2 (A) is represented in the form of a nested set.

Figure 2 (B) uses the concave notation.

The most commonly used representation method is to use the generalized table. Figure 1 (A) is expressed in a generalized table as:
(A, (B (E (K, L), F), C (G), D (H (M), I, J)))


to sum up

The tree-type storage structure is similar to the genealogy of a family, and each node may also have the relationship of father and son, brother, and cousin. This section focuses on the basic terminology of trees. Most of the abstract C language Chinese website . Please feel free to correct any errors.

Guess you like

Origin blog.csdn.net/qq_52208569/article/details/115333099