What is DOM Traversal: Breadth-First Search?
Walk the DOM tree level by level using a queue.
Type a tool name, format or task
Walk the DOM tree level by level using a queue. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Walk the DOM tree level by level using a queue. Put the root in a queue. Take the first node from the queue, visit it and add its children to the back. Repeat until the queue is empty. Time complexity: O(n). Space complexity: O(w) for the widest level w. Finding the closest matching element, rendering a tree level by level and scheduling updates breadth-first.
Walk the DOM tree level by level using a queue.
Time: O(n). Space: O(w) for the widest level w.
Finding the closest matching element, rendering a tree level by level and scheduling updates breadth-first.
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
Breadth-first search visits the tree level by level using a queue, looking for <button>.
function bfs(root, target, visit) {const queue = [root];while (queue.length) {const node = queue.shift();visit(node);if (node.tag === target) return node;queue.push(...node.children);}return null;}
Tip: use the arrow keys to step and the space bar to play or pause.