Data Structure Notes - Basic Concepts of Trees

Definition of a tree

The one-to-one linear structure has been introduced before, but in reality, there are many-to-many situations that need to be dealt with. This is the one-to-many data structure to be introduced today - tree.

Tree (Tree): is a finite set of n (n>=0) nodes. When n=0, it is called an empty tree. In any non-empty tree:

  1. There is one and only one specific node called the root (Root);
  2. When n>1, the remaining nodes can be divided into m (m>0) disjoint finite sets T 1 , T 2 , ···, T m , each of which is itself a tree, and The subtree called the root (SubTree), as shown in the figure:
    write picture description here

The definition of the tree is actually the recursive method we mentioned when we talked about the stack. That is, the concept of tree is also used in the definition of tree, which is a relatively new definition method. The subtree T1 and subtree T2 in the figure below are the subtrees of the root node A. Of course, the tree composed of D, G, H, and I is a subtree with B as the root node, and the tree composed of E and J is a subtree with C as the root node.
write picture description here

There are two more points to emphasize about the definition of a tree:

  1. When n>0, the root node is unique, and it is impossible to have multiple root nodes. Don't mix it with the big tree in reality. The tree in reality has many roots, which is the real tree, the tree in the data structure There is only one root node.
  2. When m>0, the number of subtrees is unlimited, but they must be mutually disjoint . The two structures in the following figure do not meet the definition of a tree, because they have intersecting subtrees:

write picture description here

1. Classification of nodes

A node of a tree contains a data element and branches that point to its subtrees. The number of subtrees a node has is called the degree of the node. A node whose degree is 0 is called a leaf node or terminal node; a node whose degree is not 0 is called a non-terminal node or branch node. In addition to the root node, branch nodes are also called internal nodes. The degree of a tree is the maximum degree of each node in the tree.
write picture description here

2. Relationship between nodes

The root of a node's subtree is called the node's child, and correspondingly, the node is called the child's parent. Children of the same parents call each other brothers. The ancestors of a node are all nodes on the branch from the root to the node. Conversely, any node in the subtree rooted at a node is called the descendant of the node.

write picture description here

3. Other concepts of trees

The level of nodes is defined from the root, the root is the first level, and the children of the root are the second level. If a node is at level l, the root of its subtree is at level l+1. Nodes whose parents are at the same level are called cousins. The maximum level of nodes in a tree is called the depth or height of the tree.
write picture description here

If the subtrees of the nodes in the tree are regarded as ordered from left to right and cannot be interchanged, the tree is called an ordered tree, otherwise it is called an unordered tree.

A forest is a collection of m (m >= 0) disjoint trees. For each node in the tree, the set of subtrees is the forest.

tree storage structure

When it comes to storage structure, we must talk about sequential storage and chain storage.

There can be multiple children of a node in the tree, which means that no matter in which order all nodes in the tree are stored in the array, the storage location of the nodes cannot directly reflect the logical relationship, so the simple order The storage structure does not meet the tree implementation requirements.

Three different representations are introduced today: parent representation, child representation, and child sibling representation.

1. Parental expression

Except for the root node, every other node does not necessarily have children, but must have and only one parent.

We assume that the nodes of the tree are stored in a set of contiguous spaces, and at each node, an indicator is attached to indicate the position of its parent node in the array . That is to say, in addition to knowing who it is, each node also knows where its parents are.

write picture description here

Where data is the data field, which stores the data information of the node, and parent is the pointer field, which stores the subscript of the parent of the node in the array.

Since the root node has no parents, we agree that the location field of the root node is set to -1, which means that all our nodes have its parents. The following tree can be represented like this:

write picture description here

write picture description here

Such a storage structure can easily find its parent node according to the parent pointer of the node, so the time complexity is O(1), until the parent is -1, it means that the root of the tree node is found. But if you want to know what the child node is, sorry, you need to traverse the entire structure.

Can it be improved? Of course, we can add a domain of the leftmost child of the node, let's call it the eldest child domain, so that its children can be easily found. If there are no children, the first child field is set to -1:

write picture description here

In this way, it is not easy for us to find other children except the first child.

In another scenario, if we are very concerned about the relationship between brothers, the parent representation cannot reflect such a relationship, we can add a right sibling field to reflect the sibling relationship, that is, for each node, save a Brother's subscript:

write picture description here

But if there are many children of the node, more than 2, we not only pay attention to the parents of the node, but also pay attention to the node, children and brothers, and the traversal time is relatively high, then this structure can be expanded to include the parent domain and the eldest child domain. and the right sibling domain, etc.

The design of storage structures is a very flexible process. Whether a storage structure is designed reasonably depends on whether the operation based on the storage structure is suitable, convenient and time complexity .

2. Child representation

Think differently. Since each node in the tree may have multiple subtrees, we can consider using multiple linked lists, that is, each node has multiple pointer fields, and each pointer points to the root node of a subtree, which we call the multiple linked list representation. .

However, the degree of each node of the tree is different, so there are two solutions to the limitation:

Option One

The number of pointer fields is equal to the degree of the tree:

write picture description here

Where data is the data field, child1 to childd are pointer fields, which are used to point to the child nodes of the node.

For the tree mentioned above, the degree of the tree is 3, so our number of pointer fields is 3, which is implemented as follows:

write picture description here

This method is obviously a waste of space when the degrees of each node in the tree are very different, because many nodes have empty pointer fields. However, if the degree of each node of the tree is not much different, it means that the opened space is fully utilized.

Option II

In this scheme, the number of pointer fields of each node is equal to the degree of the node, and we dedicate a location to store the number of pointer fields of nodes:

write picture description here

Where data is the data field, degree is the degree field, that is, the number of children that store the node, child1 to childd are the pointer fields, pointing to the nodes of each child of the node. as follows:

write picture description here

Although this method overcomes the shortcoming of wasting space and improves the space utilization, because the linked list of each node has a different structure, and the value of the degree of the node needs to be maintained, re-operation will bring about time delays. loss.

Next, we will introduce the child notation. The specific method is: arranging the children of each node and using a singly linked list as the storage structure, then n nodes have n child linked lists. If it is a leaf node, the singly linked list is null. Then the n head pointers form a linear table, which adopts a sequential storage structure and stores it in a one-dimensional array , as shown in the figure:

write picture description here

For this purpose, two node structures are designed, one is the child node of the child linked list:

write picture description here

Where child is the data field, which is used to store the subscript of a node in the header array, and next is the pointer field, which is used to store the pointer to the next child node of a node.

The other is the header node of the header array:

write picture description here

data is the data field, which stores the data information of the node, and firstChild is the head pointer of the child linked list of the node.

3. Child Brother Representation

For any tree, the first child of its node is unique if it exists, and its right sibling is also unique if it exists. Therefore, we set two pointers to the first child of this node and the right sibling of this node.

write picture description here

Where data is the data field, firstChild is the pointer field, which stores the storage address of the first child node of the node, and rightsib is the pointer field, which stores the storage location of the node's right sibling node:

write picture description here

This notation brings Fang Baini to a child who is looking for a node. You only need to find the eldest child of this node through firstChild, find its second brother through the rightsib of the eldest child node, and continue until you find specific child.

In fact, the biggest advantage of this method is to convert an ordinary tree into a binary tree. After sorting out the above diagram, you can get:

write picture description here

Regarding the introduction of the tree, we will continue to get more exciting content and pay attention to my WeChat public account - Android Motor Vehicles
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559294&siteId=291194637