1. The storage structure of the tree
There are many ways to store trees, either sequential storage or chain storage, regardless of which way, it is necessary to uniquely reflect the logical relationship between the nodes in the tree, the tree has the following 3 commonly used storage structure.
1.1 Parental representations
Each node is stored in a contiguous set of spaces, with a pseudo pointer indicating its parent node's position in the array, the root node being labeled 0, and the pseudo-pointer field-1.
This method of storage utilizes the nature of each node with only a single parent (root node). It is very quick to find the parent node of the knot, but the child node must traverse the whole structure.
1.2 child notation
The child nodes of each node are linked together by a single chain to form a linear structure, and N nodes have n children linked list (the child of the leaf node is the empty table), as shown in
This storage method is convenient for finding children, and finding the parents needs to traverse the N children linked list of the child's linked list pointer field in N nodes.
1.3 Child brother representation
Also known as the binary tree notation, that is, the two-linked list as the storage structure of the tree. Each node contains three parts: a node value, a pointer to the first child node of the node, and a pointer to the next sibling node of the node (along which you can find all the sibling nodes of the node),
This kind of storage is more flexible, the biggest advantage is that the tree can be easily converted to two tree, easy to find the children of the node, the disadvantage is to find the parent node from the current node is more troublesome, you can add a parent domain for each node point to its parent node.
2. The transformation of trees, forests and two-fork trees2.1 Tree converted to two-fork tree
From the physical structure, the child brother representation of the tree and the two-fork tree of the two-linked list of the same notation, there are two pointers, the tree is a point to the first child, a point to the node of the next sibling node; the binary tree points to the left child and the right child respectively. As a result, you can convert a tree to a two-fork tree with different interpretations of the same storage structure .
The tree is converted to a two-tree rule : The left pointer of each node points to its first child node, and the right pointer points to its adjacent sibling node in the tree, which can be expressed as "left child right brother". Since the root node does not have a sibling, so the tree transforms the resulting two fork tree without the right subtree,
2.2 Forest converted to two fork tree
The rules for converting forests to two-tree trees are similar to trees, first, each tree in the forest into a two-fork tree (no right subtree of the two-tree), and then the root of the first tree as the root of the converted two-fork tree, the first tree of the left subtree as the left subtree of the converted binary tree, the second tree as the right sub-tree of the transformed binary tree, The third tree acts as the right sub-tree of the right subtree of the transformed binary roots, and so on.
two tree to forest rules : If the binary tree is not empty, then the two fork root and its left subtree is the first tree of the two-tree form, the right sub-tree of the binary tree can be regarded as a two-fork tree that is converted from the forest except the first tree, applying the same method, Until the end of a two-tree with no right subtree, the original forest was obtained. two fork tree to tree rule similarly , a binary tree conversion to a tree or forest is unique.
3. The traversal of trees and forests
The traversal of a tree consists of a root traversal and a post-root traversal.
(1) The root traversal : If the tree is not empty, first access to the root node, in the order from left to right to traverse the root node of each subtrees tree, the access order and the tree corresponding to the sequence of binary tree order traversal sequence .
(2) after the root traversal : If the tree is not empty, then the root node is traversed from left to right in each subtrees tree, and then access the root node, the access order and the tree corresponding to the binary tree in the order of the sequence traversal.
(3) hierarchical traversal : the idea of hierarchical traversal with two-fork tree is basically the same, and the nodes are accessed hierarchically.
How to traverse a forest:
(1) The first sequence traversal , if the forest is not empty, according to the following rules to traverse:
① access to the root node of the first tree in the forest
② The Serring of the root node in the first tree by first order traversal
③ First-order traversal of the forests of the remaining trees after the first tree
(2) the middle sequence traversal , if the forest is not empty, according to the following rules to traverse:
The Serring of the root node in the first tree in the ① sequence
② access to the root node of the first tree in the forest
③ The forest of the remaining trees after the first tree
Tree and Forest traversal: the corresponding two-fork tree traversal algorithm can be implemented, such as the following table
Table 1-relationship between tree and forest traversal and two-fork tree traversal
Tree |
Forest |
Two-fork Tree |
First root traversal |
First Order traversal |
First Order traversal |
Post-root traversal |
Post-post traversal |
Post-post traversal |
Trees and forests