The traversal of the tree is an important operation of the tree. The so-called traversal refers to the access to all nodes in the tree, ie accesses each node in the tree and only access once. The three most important traversal methods of the tree are called pre-sequence traversal, and sequentially traversal and sequential traversal. When a tree is traversed in these three ways, if the junction is arranged in the order of access nodes, you can get the pre-sequence list, the middle list, and a sequence list of all nodes in the tree. The corresponding node order is called the pre-sequence, order and sequence of nodes, respectively. The three traversal methods of the tree are recursively defined as follows: If t is an empty tree, the pre-sequence traversal traversal, the middle sequence traversal, and the sequence traversal are empty, and the resulting list is a holiday. If t is a single synon tree, then the pre-sequence traversal, the middle sequence traversal, and the rear sequence traverses only access this node. This node itself is the corresponding list to get. Otherwise, set T, as shown in Figure 6, it is T1, T2, .., TK from left to right with n, tree root, and TK, then: Pre-sequence traversal is first access to the tree Root N, then sequentially traversed T1, T2, .., TK. The secondary sequence traversal is the first order to traverse T1, then accessed the root N, then sequentially traverse T2, T2, .., TK. The back sequence traversal of T is to serve T1, T2, .., TK first, and finally accessed the root N.
Figure 6 Tree T
The above three traversal traversal can be described in the same manner as the following three traversal can be described in the following examples, wherein the ADT operation of the tree is used: procedure preorder_traversal (v: nodetype); {presenter multi-pass algorithm} Begin Visite (V); {Access Node V} i: = leftmost_child (v); while i <> ∧ Do Begin Preorder_Traversal (i); {Joint V from left to right, access each son node i} i: = right_sibling (i); END; End; procedure inorder_traversal (v: nodetype); {中 中 历 算 法}} ∧ ∧ {是 是 是 是 是 是;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ( Traverse the left of the left first son node} Visite (V); {access node v} i: = right_sibling (leftmost_child (v)); {i = v left second son} While i <> ∧ do begin inorder_traversal (i); {from the second start to the far right to access each son node i} i: = right_sibling (i); end; end; end; procedure postorder_traversal (v: nodetype); { Sequence calendar algorithm} begin i: = leftmost_child (v); while i <> ∧ do begin preorder_traversal (i); {Joint V from left to right, access each son node i} i: = right_sibling (i); Visite (V); {Access Node V} END; in order to place all nodes in a tree, the tree root will be called to call the corresponding process. For example, the tree in FIG. 7 is traversed, and the sequence traversal and back sequence traversal will be obtained by previous sequence list: A b E F I J C D g h; the middle series: E B I f J a C G D h; Rear Sequence List: E I J F B C G H D A.
Figure 7
Here is a method that can generate a node list of the above three traversal methods. It is assumed that we start from the root of the root, bypassing the outer edge of the tree in the clockwise direction (eg, the route around the tree around Figure 7 is shown in Figure 8). The same node may be passed many times on the way. If we press the first time in the first time, you can get a pre-sequence list; if you press the last time the time sequence list, it is to leave a node to go to his father. Listed, you will get a sequence list. In order to generate a middle series, the leaf junction is to be different from the internal nodes. The leaf junction is listed in the first time, and the internal node is listed in the second time. Figure 8 traversal
In the above three different order, the relative order between the leaves is the same, and they are arranged from left to right between left to right. The difference in three lists is only different between the internal nodes and the order between the internal nodes and the leaves. A tree is a pre-sequence list or a rear sequence list helps to query the ancestor of the node. Assuming that the node V is the serial number (integer) in the rear sequence list as PostOrder (V), we call this integer as the rear sequence number of node V. For example, in FIG. 7, the sequence numbers of nodes E, I and J are 1, 2, and 3, respectively. The sequence number of the node has this characteristic: the number of true sessions set in the node V is DESC (V), then in the rear sequence number of all nodes in the subtree of V, it is just in PostOrder (V ) -Desc (v) between PostOrder (V). So in order to verify the descending of the node x, we only need to judge whether their back-sequence number is satisfied: Postorder (Y) -Desc (y) ≤Postorder (x) ≤Postorder (Y)
The preamble number also has a similar nature.