Review tree-recursion

Source: Internet
Author: User


traversal

the so-called traversal (traversal) indicates that each node in the tree is accessed once and only once along a search route. The operations performed by the Access Node depend on specific application problems.
traversal is one of the most important operations on a binary tree. It is the basis for other operations on a binary tree.

traversal scheme

1. traversal scheme
the recursive definition of a binary tree shows that a non-empty binary tree consists of the root node and the Left and Right Subtrees. Therefore, three operations can be performed on any given node in a certain order:
(1) Access Node itself (N),
(2) traverse the left subtree (L) of the node,
(3) traverse the right subtree (r) of the node ).
the preceding three operations are executed in six sequence:
NLR, LNR, LRN, NRL, rnL, and RLN.
Note:
the first three order and the last three order are symmetric, so we only discuss the first three order of first left and then right.
2. names of the three traversal types
names based on the locations where Access Node operations occur:
① NLR: preordertraversal (preordertraversal ))
-- the operation on the Access Node takes place before traversing the left and right subtree of the node.
② LNR: inordertraversal
-- the operation of the Access Node occurs in the left and right subtree Of The traversal node ).
③ LRN: postordertraversal
-- the operation of the Access Node occurs after the left and right subtree is traversed.
Note:
because the accessed node must be the root of a subtree, n (node), L (left subtlee), and R (right subtree) it can also be interpreted as the root, the left subtree of the root, and the right subtree of the root. NLR, LNR, and LRN are also called root traversal, root traversal, and root traversal respectively.

traverse algorithms

1. recursive Algorithm definition for sequential traversal:
If the binary tree is not empty, perform the following operations in sequence:
(1) traverse the left subtree;
(2) access the root node.
(3) traverse the right subtree.
2. recursive Algorithm definition for sequential traversal:
If the binary tree is not empty, perform the following operations in sequence:
(1) access the root node;
(2) traverse the left subtree;
(3) traverse the right subtree.
3. recursive Algorithm definition for post-order traversal:
If the binary tree is not empty, perform the following operations in sequence:
(1) traverse the left subtree;
(2) traverse the right subtree;
(3) Access the root node.

4. Implementation of the algorithm for sequential Traversal
The binary linked list is used as the storage structure. The central order traversal algorithm can be described as follows:
Void inorder (bintree T)
{// ① ~ ⑥ It is to explain the labels added to the execution process
① If (t) {// If the binary tree is not empty
② Inorder (t-> lchild );
③ Printf ("% C", T-> data); // Access Node
④ Inorder (t-> rchild );
⑤}
⑥} // Inorder

Traversal Sequence

1. traverse the execution trace of a binary tree
The search routes of the three recursive Traversal Algorithms are the same (as shown in the dotted line ).
The specific line is:
Starting from the root node, it moves counter-clockwise along the outer edge of the binary tree. Each node is routed three times and finally returned to the root node.



2. traversal Sequence
(1) sequencing
When a binary tree is traversed in the middle order, the access order to the nodes is the middle order.
[Example] When the binary tree shown in the middle-order traversal is obtained, the middle-order sequence is:
D B A E C f
(2) First-order sequence
When a binary tree is first traversed, the access order to the nodes is first sequential.
[Example] When the binary tree shown in the first traversal is obtained, the first sequence is:
A B D C E F
(3) Post Sequence
When a binary tree is traversed in the descending order, the access order to the nodes is the descending order.
[Example] After traversing the binary tree shown in the descending order, the obtained descending order sequence is:
D B E F C
Note:
(1) In the search route, if the access node is the first time when it passes through the node, It is a forward traversal; if the access node is the second (or third) when the node goes through, it is a middle-order traversal (or a back-order traversal ). You only need to list all the nodes that pass through the first, second, and third times on the search route to obtain the pre-order, middle-order, and post-order sequences of the binary tree respectively.
(2) The above three sequences are linear sequences with only one Start Node and one terminal node. The other nodes have only one front node and one slave node. To distinguish the concepts of the Forward (parent) node and the next (child) node in a tree structure, the name of the traversal order should be given before and after a node.
Node C in the binary tree shown in [Example]. The frontend node is d, the successor node is E, the frontend node is E, and the successor node is F; the frontend node is f, and the successor node is. However, in terms of the logic structure of the tree, the front node of C is a, and the successor nodes are E and F.

Construction of binary linked list

1. Basic Ideas

It is constructed based on the first-order traversal, that is, the first-order sequence of a binary tree is used as the input structure.
Note:
Virtual nodes must be added to the sequence to show the position of a null pointer.
[Example]
Create a binary tree shown in the following table. The first sequence of input is: Abd, Ce, f, and so on.

2. Constructor
Assume that the input of the virtual node is represented by a space character, and the corresponding constructor is:
Void createbintree (bintree * t)
{// Create a binary linked list. T is the pointer to the root pointer, so modifying * t modifies the real parameter (root pointer) itself.
Char ch;
If (CH = getchar () = '') * t = NULL; // set the pointer to null
Else {// read by non-space
* T = (bintnode *) malloc (sizeof (bintnode); // generate a node
(* T)-> DATA = CH;
Createbintree (& (* t)-> lchild); // construct the left subtree
Createbintree (& (* t)-> rchild); // construct the right subtree
}
}
Note:
When calling this algorithm, the address of the root pointer of the binary linked list to be established should be used as the real parameter.
[Example]
If root is a pointer (that is, its type is bintree), after createbintree (& root) is called, root points to the root node of the constructed binary linked list.
Other methods for constructing a binary linked list [see references]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.