gpt4 book ai didi

javascript - 为什么DOM树是oder preorder,深度优先遍历?

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:43 25 4
gpt4 key购买 nike

为什么DOM树是先序深度优先遍历

与 BFT 等其他遍历相比,这种设计选择有什么优势?

我只是在调查 DOM standard并找到了 preceding 和 following 的定义:

An object A is preceding an object B if A and B are in the same tree and A comes before B in tree order.

An object A is following an object B if A and B are in the same tree and A comes after B in tree order.

Just like most programming paradigms the Web platform has finite hierarchical tree structures, simply named trees. The tree order is preorder, depth-first traversal.

最佳答案

深度优先遍历通常是最简单的遍历样式,因为您可以递归地或使用显式堆栈进行;广度优先需要一个队列,从某种意义上说,它是一个更复杂的数据结构。但我认为有一个比传统或简单性更简单的答案:(X)HTML 树的深度搜索遍历导致文本节点按呈现顺序遍历。

考虑一下这个相对简单的 HTML子树。

或者,以原始形式:

<p>Consider this <emph>relatively</emph> simple <a href="...">HTML</a> subtree</p>

作为一棵树(省略空格和属性):

                      <P>
|
+-----------+----+----+-----+------+
______|______ __|___ ___|__ _|_ ___|___
Consider this <EMPH> simple <A> subtree
| |
____|_____ __|__
relatively HTML

深度优先遍历:

<P>, Consider this, <EMPH>, relatively, simple, <A>, HTML, subtree

广度优先遍历:

<P>, Consider this, <EMPH>, simple, <A>, subtree, relatively, HTML

关于javascript - 为什么DOM树是oder preorder,深度优先遍历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com