3 methods to traverse the tree

Source: Internet
Author: User

According to the definition, a binary tree consists of three parts: Root, left subtree, and right subtree. Therefore, the traversal of Binary Trees can also be divided into three "subtasks ":
① Access the root node;
② Traverse the left subtree (that is, access all nodes on the left subtree in sequence );
③ Traverse the right subtree (that is, access all nodes on the right subtree in sequence ).
Because left and right Subtrees are Binary Trees (which can be empty Binary Trees), traversal of them can continue to be decomposed as described above until each subtree is empty binary tree. It can be seen that the order of the above three subtasks determines the order of traversal. If the three subtasks are represented by D, L, and R, there are 6 possible orders: DLR, LDR, LRD, DRL, RDL, and rld. It is usually limited to "first left and then right", that is, subtask ② is completed before subtask ③, so there are only the first three orders left, traversal by these three methods is called first root traversal (or first-order traversal), middle root (or middle-order) traversal, and rear root (or back-order) traversal. The three traversal methods are defined as follows.
If the binary tree to be traversed is empty, perform the null operation. Otherwise, perform the following operations in sequence:
① Access the root node;
② Traverse the left subtree with the root;
③ Traverse the right subtree first.
If the binary tree to be traversed in the root traversal is empty, perform the null operation. Otherwise, perform the following operations in sequence:
① The middle root traverses the left subtree;
② Access the root node;
③ The middle root traverses the right subtree.
If the binary tree to be traversed by the root traversal is empty, perform the null operation. Otherwise, perform the following operations in sequence:
① The back root traverses the left subtree;
② The back root traverses the right subtree;
③ Access the root node.
Obviously, the difference between the above three traversal methods is that the "Timing" of the sub-task "accessing the root node" is different. If the sub-task is executed first (last, in the middle, then, the system traverses the root (root and root.
Traverse a binary tree in a certain way to obtain the access sequence of all nodes on the binary tree.


To list all nodes in a tree in a certain order, you only need to call the corresponding process for the root. For example, for the tree in figure 7, pre-sequential traversal, middle-order traversal, and post-sequential traversal will respectively obtain the pre-sequential table: a B e f I j c d G H; middle-order list: E B I f j a c g d h; post sequence table: e I j f B C G H D.

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.