Introduction to the

First we need to know what depth first and what breadth first is.

Depth-first traversal is to start from a vertex, visit this vertex first, then find the first unvisited neighbor that just visited this node, and then continue to look for its next vertex for access. Repeat this step until all nodes are accessed.

Breadth-first traversal starts from a vertex, visits the vertex first, then finds all the unvisited neighbors that have just visited the node, visits all the nodes of the first neighborhood of these nodes after the visit, and repeats this method until all nodes have been accessed.

Function deepTraversal(node){let nodes=[]; if(node! =null){ nodes.push[node]; let childrens=node.children; for(let i=0; i<childrens.length; i++) deepTraversal(childrens[i]); } return nodes; } function deepTraversal(node){let nodes=[]; if(node! =null){ let stack=[]; Stack. Push (node); stack. Push (node); while(stack.length! =0){ let item=stack.pop(); // The node nodes.push(item); let childrens=item.children; for(let i=childrens.length-1; i>=0; Push (childrens[I]); push(childrens[I]); push(childrens[I]); } } return nodes; } function wideTraversal(node){let nodes=[], I =0; if(node! =null){ nodes.push(node); wideTraversal(node.nextElementSibling); node=nodes[i++]; wideTraversal(node.firstElementChild); } return nodes; } function wideTraversal(node){let nodes=[], I =0; while(node! =null){ nodes.push(node); node=nodes[i++]; let childrens=node.children; for(let i=0; i<childrens.length; i++){ nodes.push(childrens[i]); } } return nodes; }Copy the code

Conclusion:

Depth-first traversal starts from a vertex, visits this vertex first, then finds the first unvisited neighbor node that just visited this node, and then takes this neighbor node as its vertex, continues to find its next new vertex to access, and repeats this step until all nodes are accessed. Breadth-first traversal starts from a vertex, visits this vertex first, then finds all unvisited adjacent points of this node, visits all nodes of the first adjacent point of these nodes after the visit, and repeats this method until all nodes are accessed.