Javascript-ONLY DOM Tree Traversal - DFS and BFS?

前端 未结 3 619
小鲜肉
小鲜肉 2021-02-05 14:38

Can anyone provide either code, pseudocode, or even provide good links to implementing DFS and BFS in plain JavaScript (No JQuery, or any helper libraries)? I\'ve been trying to

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 15:21

    Let's use the following HTML code as an example:

    DFS will always go to the next level of nodes first, and only if there are no more un-traversed child nodes will it step to a next node on the current level.

    A DFS would traverse the nodes of the example in the following order:

    a, aa, aaa, aab, ab, aba, abb
    

    BFS will always traverse all the nodes in the current level first and only after that will it go to the next level of nodes.

    A BFS would traverse the nodes of the example in the following order:

    a, aa, ab, aaa, aab, aba, abb
    

    There isn't a definite answer which one of these you should use. Usually it depends on your needs.

    Implementation details:

    For a DFS people often use a stack.

    Pseudo code:

    stack my_stack;
    list visited_nodes;
    my_stack.push(starting_node);
    
    while my_stack.length > 0
       current_node = my_stack.pop();
    
       if current_node == null
           continue;
       if current_node in visited_nodes
          continue;
       visited_nodes.add(current_node);
    
       // visit node, get the class or whatever you need
    
       foreach child in current_node.children
          my_stack.push(child);
    

    This code will go until there is any nodes in the stack. In each step we get the top node in the stack and if it's not null and if we haven't visited it before than we visit it and add all its children to the stack.

    Queue is usually used for the BFS.

    Pseudo code:

    queue my_queue;
    list visited_nodes;
    my_queue.enqueue(starting_node);
    
    while my_queue.length > 0
       current_node = my_queue.dequeue();
    
       if current_node == null
           continue;
       if current_node in visited_nodes
          continue;
       visited_nodes.add(current_node);
    
       // visit node, get the class or whatever you need
    
       foreach child in current_node.children
          my_queue.enqueue(child);
    

    This code will go until there is any nodes in the queue. In each step we get the first node in the queue and if it's not null and if we haven't visited it before than we visit it and add all its children to the queue.

    Note that the main difference between the two algorithm is the data type we use.

提交回复
热议问题