Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use depth first traversal and breadth first traversal algorithm in JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use depth-first traversal and breadth-first traversal algorithm in JavaScript". In daily operation, I believe that many people have doubts about how to use depth-first traversal and breadth-first traversal algorithm in JavaScript. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "depth-first traversal and breadth-first traversal algorithm in JavaScript". Next, please follow the editor to study!

The details are as follows:

1. Recursive method of depth-first traversal

Function deepTraversal (node) {var nodes = []; if (node! = null) {nodes.push (node); var children = node.children; for (var I = 0; I

< children.length; i++) deepTraversal(children[i]); } return nodes;} 2、深度优先遍历的非递归写法 function deepTraversal(node) { var nodes = []; if (node != null) { var stack = []; stack.push(node); while (stack.length != 0) { var item = stack.pop(); nodes.push(item); var children = item.children; for (var i = children.length - 1; i >

= 0; iMel -) stack.push (childrens [I]);}} return nodes;}

3. The recursive method of breadth-first traversal:

Error report: Maximum call stack size exceeded (…)

Function wideTraversal (node) {var nodes = []; var I = 0; if (! (node = = null)) {nodes.push (node); wideTraversal (node.nextElementSibling); node = nodes [iTunes +]; wideTraversal (node.firstElementChild);} return nodes;}

4. Non-recursive writing method of breadth-first traversal.

Function wideTraversal (selectNode) {var nodes = []; if (selectNode! = null) {var queue = []; queue.unshift (selectNode); while (queue.length! = 0) {var item = queue.shift (); nodes.push (item); var children = item.children; for (var I = 0; I < children.length; iTunes +) queue.push (childrens [I]);} return nodes At this point, the study on "how to use depth-first traversal and breadth-first traversal algorithm in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report