【Data Structure Learning Record 16】——Trees and Forests

1. The storage structure of the tree

In a large number of applications, people have used various forms of storage structures to represent trees. Now I will describe the three storage structures in detail, and study one of them 最优秀in detail .

1. Parental Notation

We store this tree through a sequence table. The contents of the table include: the content of the tree itself and the subscript of the sequence table of its parents. The parent subscript of its root node is fine -1.
If we require the root node, then we only need to repeatedly take the parent index of a node.
However, the shortcomings are obvious. When seeking a node, you may need to traverse the entire sequence table.
Insert picture description here

2. Child notation

In the child notation, we store it in a way similar to a generalized table. In addition to its own value, a node also stores the root nodes of all its sons. This approach will cause a lot of empty chain domains. Or in the son node, we save one more node's degree, so as to save the empty chain domain at the end.
Of course, we can also think of a more optimized method. Use a singly linked list to store all nodes linearly. If the node has children(s), then string their subscripts into a linked list and assign them to the node. This makes it easy to traverse the child, but it is difficult to know its parents. If we add the parental representation method, then this tree is more perfect.
Insert picture description here

3. Child brother notation

We build a binary tree, the left son of the tree represents its son node, and the right son of the tree represents its sibling node. In this way, it is easy to traverse the son of the node. If you add a parent domain to him, it is also easy to traverse upward.
In view of this method is hierarchical and easy to operate, so we will focus on this method.
Insert picture description here

2. The operation of the brother and child forest

Insert picture description here

1. Convert the forest to a binary tree

Suppose we convert a forest F={T 1 , T 2 , T 3 ……T m } into a binary tree B=(root, LB, RB).

  1. If F is empty, then m=0.
  2. If F is not empty, then the LB of B is the tree expanded by T 1 , and the second RB expands the tree recursively according to the method of 2 {T 2 , T 3 ……T m }

2. Convert Binary Tree to Forest

The same

  1. If B is empty, then it is empty.
  2. Otherwise, T . 1 is generated by recursive LB, T 2 is constituted by RB and LB, T3 formed of an RB and LB RB. This expands recursively.

3. Traversing the forest of brothers and children

According to the definition, we can traverse the forest in order and traverse the forest in order. And the traversal method is very similar to the binary tree.
Insert picture description here

a. First-order traversal (actually DFS)

During pre-order traversal:

  1. Visit the root node
  2. First traverse the subtree forest of the root node of the first tree (that is, traverse the left son)
  3. First traverse the remaining forest after the first tree (that is, repeat 2 with the right son as the root node)
  4. Traverse the above pictureABCDEFGHIJ

b. Post-order traversal

  1. In the middle-order traversal forest, the root node of the first tree
  2. Visit the root node of the first tree
  3. In order to traverse the forest composed of the remaining trees after the first tree
  4. Traverse the above pictureBCDAFEHJIG

4. Description

For this part, since the tree is not a binary tree, we can focus on its three storage methods, the two traversals of the child brother method, and the transformation and restoration of trees and forests.
As for the code, in fact, it is necessary to write, and it is okay to write a binary forest storage. Otherwise, the amount of code is still a bit small, so I won't focus on the introduction.

Guess you like

Origin blog.csdn.net/u011017694/article/details/110095583