Java implementation of depth traversal and breadth over-counting and its application

Source: Internet
Author: User

The principle and realization of depth traversal and breadth traversal

1, Depth first

The abbreviation for DFS is depth first Search. The process is simply to drill down into each possible branching path to no further depth, and each node can be accessed only once. For the above example, the result of a depth-first traversal is: a,b,d,e,i,c,f,g,h. (Suppose you go to the left of the child node first).

A depth-first traversal of each node requires the use of a data structure such as a heap (stack). Stack is characterized by advanced post-out. The entire traversal process is as follows:

First, a node is pressed into the heap, stack (a);

The A node pops up, and the child node of a c,b is pressed into the heap, at which point B is at the top of the heap, stack (B,C);

The b node pops up and e,d the child node of B into the heap, at which point D is at the top of the heap, stack (D,E,C);

The D node pops up with no child nodes pressed in, at which point E is at the top of the heap, stack (E,C);

The e node pops up, and the child node I of E is pressed into the stack (I,C);

... In turn, the final traversal is complete, and the Java code is probably as follows:

public void Depthfirst () {

stack<map<string, object>> nodestack = new stack<map<string, object>> ();

Map<string, object> node = new hashmap<string, object> ();

Nodestack.add (node);

while (!nodestack.isempty ()) {

node = Nodestack.pop ();

SYSTEM.OUT.PRINTLN (node);

Obtains the node's child node, for the binary tree is obtains the node's left dial hand nodes and the right child node

list<map<string, object>> children = getChildren (node);

if (children! = null &&!children.isempty ()) {

for (Map Child:children) {

Nodestack.push (child);

}

}

}

}

?//node use map to store

2. Breadth First

The English abbreviation is BFS namely breadth firstsearch. The process test is to access each layer of nodes in turn, access to the next level, and each node can only be accessed once. For the above example, the result of the breadth-first traversal is: a,b,c,d,e,f,g,h,i (assuming that each layer of nodes is accessed from left to right).

Breadth-first traversal of the various nodes, need to use the data structure of the queue (queues), the characteristics of queuing is FIFO, in fact, you can also use a double-ended queue, the difference is that the first two-terminal queue can be inserted and pop-up nodes. The entire traversal process is as follows:

The a node is inserted into the queue first, queue (a);

The A node is ejected, and a child node b,c is inserted into the queue, at which point B is at the top of the queue, C is at the end of the queue, and queue (B,C);

The b node pops up and the child node of B is d,e into the queue, at which point C is at the top of the queue, E is at the end of the queue, and queue (c,d,e);

The C node pops up and the child node of C is f,g,h into the queue, at which point D is at the top of the queue, H is at the tail of the queue, and queue (D,E,F,G,H);

The D node pops up and D has no child nodes, at which point E is at the top of the queue, H is at the end of the queue, and queue (E,F,G,H);

... In turn, the final traversal is complete, and the Java code is probably as follows:

public void Breadthfirst () {

deque<map<string, object>> nodedeque = new arraydeque<map<string, object>> ();

Map<string, object> node = new hashmap<string, object> ();

Nodedeque.add (node);

while (!nodedeque.isempty ()) {

node = Nodedeque.peekfirst ();

SYSTEM.OUT.PRINTLN (node);

Obtains the node's child node, for the binary tree is obtains the node's left dial hand nodes and the right child node

list<map<string, object>> children = getChildren (node);

if (children! = null &&!children.isempty ()) {

for (Map Child:children) {

Nodedeque.add (child);

}

}

}

}

It uses a double-ended queue, which is the same as using a queue.

Second, the application---to traverse the tree with breadth precedence, and to record all paths that meet certain requirements

Java implementation of depth traversal and breadth over-counting and its application

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.