Data structure-traversal of a two-tree (c-language descriptive narrative)

Source: Internet
Author: User

Traversal concepts

The so-called traversal (traversal) refers to a search route along a path. Make one visit to each node in the tree in turn. The operation of the Access node relies on a detailed application issue.


Traversal is one of the most important operations on a two-fork tree, and is the basis for other operations on a two-fork tree.

Traversal scenarios

1. Traversal scenarios
From the recursive definition of the two-fork tree, a non-empty two-fork tree consists of three basic parts, the root node and the left and right subtree. So. At any given node, you can run three operations in some order:
(1) The interview node itself (N),
(2) Zuozi (L) traversing the node,
(3) The right sub-tree (R) of the node is traversed.
There are six operating sequences for the above three operations:
NLR, LNR, LRN, NRL, RNL, RLN.


Attention:
The first three sequences are symmetrical with the latter three. So we only discuss the first three order of left and right.



2. Three types of traversal naming
Based on the location of the Access node operation name:
① NLR: Pre-order traversal (Preordertraversal also called (sequential traversal))
--The operation of the access node occurs before traversing its left and right subtrees.
② LNR: Middle sequence Traversal (inordertraversal)
--The operation of the access node occurs in the traversal of its left and right subtree (between).


③ LRN: Post-post traversal (postordertraversal)
--access node operations occur after traversing their left and right subtrees.
Attention:
Because the node being interviewed must be the root of a subtree. So N (Node), L (left Subtlee), and R (right subtree) can be interpreted as the root, the Zoki of the root.

The

NLR, LNR, and LRN are also known as first-root traversal, middle-root traversal, and post-root traversal, respectively.


Traversal algorithm

1. Recursive algorithm definition for middle order traversal:
    If the binary tree is not empty. Run the following actions, for example:
          (1) to traverse the left subtree.
          (2) Visit the root node;
          (3) traverse the right subtree.

2. Recursive algorithm definition of the first order traversal:
    If the binary tree is not empty, then run the following actions, for example:
          (1) To access the root node;
          (2) Traverse left subtree,
          (3) Traverse right subtree.



3. The recursive algorithm for post-routing traversal is defined:
If the binary tree is not empty. Then run the following operations, for example:
(1) traverse the left subtree.
(2) Traverse right subtree.
(3) Visit the root node.

4. Algorithm implementation of Middle sequence traversal
Using binary linked list as storage structure, the middle sequence traversal algorithm can be described as:
void Inorder (Bintree T)
{///algorithm ①~⑥ is to illustrate the increase in the operation of the label
①if (T) {//Assuming that the binary tree is not empty
②inorder (T->lchild);
③printf ("%c", T->data). Interview node
④inorder (T->rchild);
⑤}
⑥}//Inorder

Data structure-traversal of a two-tree (c-language descriptive narrative)

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.