[Data Structure] Basic concepts of trees | Be familiar with entry-level trees and binary trees

In the process of learning trees , binary trees are more important, but before learning binary trees , you must first understand some number concepts.

tree definition

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

tree nature

The tree has a special node called the root node , and the root node has no predecessor node ;

Except for the root node, the remaining nodes are divided into M disjoint sets T1, T2,..., Tm, where each set Ti (1 <= i <= m) is a tree structure with Tree-like subtrees. The root node of each subtree has one and only one predecessor , and can have 0 or more successors ;

Trees are defined recursively :

  • Any tree can be split into a root and n (n >= 0) subtrees. So the tree will definitely be classified as recursive problem solving. The return condition for the tree recursion problem is that when a leaf is encountered and there are no subtrees, then the return begins.

So what is non-linear?

  • To put it simply, nonlinearity means that the logical structure is not linear. They are not one next to another in the logical structure. The logical structure is an upside-down tree.

The first thing we need to be clear about is that the relationship between data elements should include three aspects:

  • The logical structure of the data;
  • Data storage structure;
  • A collection of operations on data.

Logical structure and physical structure:

  • The logical structure is what we imagine ;
  • A physical structure isactual storage structure in memory .

The linear lists, stacks, pairs, strings, arrays, and generalized lists we learned before are all linear structures.

Let us note here that the physical structure of the array structure is consistent with the logical structure . The array storage is continuous in the logical structure and also continuous in the physical structure.

But linked lists are different from arrays. In our usual drawing process, there are arrows in the linked list and each arrow points to the next one and back in sequence, but in practice, this may not be the case. These structures all come from the heap. The linear table is continuous in logical structure. Through each arrow, the previous one points to the next one. However, in the actual process, there is no arrow, it is the address where the previous one stores the next one.

tree related concepts

  • The nodes in the linked list all have a or fixed pointer, such as a pointer to the next node .
  • In a doubly linked list , there are pointers to the predecessor and pointers to the successor .
  • A node in the tree will have many pointers pointing to its children.

degree of node

Degree of node: The number of subtrees contained in a node is called the degree of the node; for example, the degree of node A is 6, and the degree of node B is 0.

leaf node or terminal node

Leaf node or terminal node: A node with degree 0 is called a leaf node; as shown in the figure above, P/Q/H/I/B/C... are all leaf nodes. 

Non-terminal node or branch node

Non-terminal node or branch node: a node with a degree other than 0; as shown in the figure above, D/E/F/G/J are all non-terminal nodes.

So a number can be seen as a combination of root, branch nodes and leaf nodes.

Parent node or parent node

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

child node or child node

Child node or child node: A node containing the root node of a subtree is called a child node of the node; as shown in the figure above: B is a child node of A.

A node may be both a parent node and a child node.

Here, there are places called "parent nodes". There are not two parent nodes here, it still only has one parent, but it is translated as "parents" for the sake of ambiguity.

Sibling node

Sibling nodes: Nodes with the same parent node are called sibling nodes; as shown in the figure above: B and C are sibling nodes (brothers).

degree of tree

Degree of tree: In a tree, each node has a degree, and the degree of the largest node is called the degree of the tree; as shown in the figure above: the degree of the tree is 6.

tree levels

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

height or depth of tree

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

Array subscripts start from 0.

So do height and depth start from 0 or 1?

  • There are no detailed regulations on the levels of the tree, but it is recommended to start from 1.

Why do array subscripts start from 0?

  • To facilitate calculation, a[i] is equivalent to *(a + i).

What is the relationship between arrays and pointers?

  • The array name is the address of the first element. a[i] is equivalent to *(a + i).

So why does the tree suggest starting from 1 instead of 0?

cousin node

Cousin nodes: Nodes whose parents are on the same level are cousins ​​of each other; as shown in the figure above: H and I are brother nodes of each other.

Ancestor of node

Ancestors of a node: all nodes on the branches from the root to the node; as shown in the figure above: A is the ancestor of all nodes.

descendants

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

forest

Forest: A set of m (m > 0) disjoint trees is called a forest; (the union set learned later belongs to the forest).

Trees, in short, are described by the concept of number + human kinship.

Trees and non-trees

Tree Note:

  • Subtrees cannot intersect ;
  • Except for the root node, each node has one and only one parent node;
  • A tree with N nodes has N-1 edges.

In a tree structure, there cannot be intersection between subtrees, otherwise it is not a tree structure.

Guess you like

Origin blog.csdn.net/2301_78131481/article/details/134571926