The traversal of the tree and the traversal of the graph

Source: Internet
Author: User

Research and development, do not be affected by the original terminology, in fact, is to think of the original learned or seen can solve the new problems encountered problem, it is a fluke, forget the original terminology, it is only your source of innovation.

Traversal is to make a linear sequence of nodes according to certain rules, and different rules get the linear sequence of different order.

Algorithm is the actual problem of the abstraction of the work steps, do not blindly think about the algorithm, think about how the actual situation, and then extract the algorithm, and then optimize.

In any case, combine with a specific data structure.

one, the traversal of the tree

For the tree traversal, there are three kinds, take the pre-order traversal, the resulting sequence regardless of how to split (sub-string, is to be continuous), began
If the root of the end, followed by the left in front of the right, a little DP similar to the optimal substructure.

Middle order Ldrif (null) {    return;} else{    //output left root  right      }

No matter the former is DFS, but the tree, because of the explicit children field, do not need to set the VIS array to ensure that only one traversal, because the knowledge is convenient once.

Travel (node) {for    (I=1:lengh)    {        //..... Process node Node          if (null! =children) Travel          (children);    Another way of writing is to write a for loop outside the function

the hierarchical traversal of the tree and BFS is very similar, in general is the DFS stack, is not the stack is implicit stack, BFS with the queue, with the explicit column.

1. Initialize a queue, the root is enqueued 2. Take the opponent, and access 3. If the left node is not empty, queue up; the right node is not empty queue 4. Team not empty, loop 2-3, otherwise end
Second, the graph of the traversal2.1 BFS

The top-down, from left to right, also requires vis. is the promotion of the tree level.

2.2 DFS

A vis array is required. is the promotion of the first tree.

2.3 Tree and graph differences and relations

Diagram needs to need Vis is afraid of loop traversal, this is not the same as the tree, the number can not loop traversal.

The same point: from a point to traverse the neighboring nodes, for the tree is left and right children.

Different points: The graph has multiple neighboring points, the binary tree only left and right children, BFS need vis record visited node.

For example A, left B right C, access a, and then BC as a team, and then visit B,a and b adjacent, no vis words a again be visited.

The graph is not connected, the tree's DFS and BFS do not need vis.

A tree is a special kind of diagram.

third, the clue tree

2 additional fields per node point to the predecessor and successor of the node, respectively. Precursor good, directly in travel to add a parameter ParentID, initially null, in for, if outside the direct point to the ParentID, the successor should not be difficult, in the if, mainly to see the tree data structure.

Iv. non-recursive traversal

BFS uses queues to save nodes or subtrees that have not yet been traversed, and DFS stacks.

The traversal of the tree and the traversal of the graph

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.