We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<div id="a"> <p id="haha"></p> </div> <div id="b"> <div id="d"> <span id="e"></span> <span id="f"></span> </div> </div> <div id="c"></div>
最弟弟的方法,当树的高度很大的时候函数调用栈会十分的深
// DFS 递归法实现 dfs(document.body, el => console.log(`${el.tagName}#${el.id}`)) function dfs (t, cb) { // 对当前节点进行处理 cb(t) // 若该节点是叶子节点,直接返回 if (!t.children.length) return // 若该节点是父节点,查询其叶子节点 Array.prototype.forEach.call(t.children, el => dfs(el, cb)) }
借助栈,复杂度为 O(n),树高度很大时会优于递归法
// DFS 迭代法,栈实现 dsf_stack(document.body, el => console.log(`${el.tagName}#${el.id}`)) function dsf_stack (t, cb) { let stack = [] stack.push(t) while (stack.length > 0) { let node = stack.pop() // 出栈 cb(node) // 若该节点为父节点,则将其子节点压入栈中 if (node.children.length) Array.prototype.forEach.call(node.children, el => stack.push(el)) } }
用迭代法实现,借助队列,复杂度也为 O(n)
// WFS 迭代法,队列实现 wsf_queue(document.body, el => console.log(`${el.tagName}#${el.id}`)) function wsf_queue (t, cb) { let queue = [] queue.unshift(t) while (queue.length > 0) { let node = queue.pop() // 出队 cb(node) // 若该节点为父节点,则将其子节点入队 if (node.children.length) Array.prototype.forEach.call(node.children, el => queue.unshift(el)) } }
The text was updated successfully, but these errors were encountered:
AymaxLi
No branches or pull requests
树的遍历
Demo
DFS
递归法
最弟弟的方法,当树的高度很大的时候函数调用栈会十分的深
迭代法(栈)
借助栈,复杂度为 O(n),树高度很大时会优于递归法
WFS
用迭代法实现,借助队列,复杂度也为 O(n)
The text was updated successfully, but these errors were encountered: