我想知道二叉树的具体应用是什么。你能举几个例子吗?


当前回答

争论二叉树的性能是没有意义的——它们不是一种数据结构,而是一组数据结构,它们都具有不同的性能特征。虽然不平衡二叉树的搜索性能确实比自平衡二叉树差得多,但对于许多二叉树(如二进制尝试)来说,“平衡”是没有意义的。

二叉树的应用

Binary Search Tree - Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries. Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered. Binary Tries - Used in almost every high-bandwidth router for storing router-tables. Hash Trees - Used in torrents and specialized image-signatures in which a hash needs to be verified, but the whole file is not available. Also used in blockchains for eg. Bitcoin. Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort. Huffman Coding Tree (Chip Uni) - Used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats. GGM Trees - Used in cryptographic applications to generate a tree of pseudo-random numbers. Syntax Tree - Constructed by compilers and (implicitly) calculators to parse expressions. Treap - Randomized data structure used in wireless networking and memory allocation. T-tree - Though most databases use some form of B-tree to store data on the drive, databases which keep all (most) their data in memory often use T-trees to do so.


二叉树比n-ary树更常用于搜索的原因是n-ary树更复杂,但通常不会提供真正的速度优势。

在一个有m节点的(平衡的)二叉树中,从一层移动到下一层需要进行一次比较,并且有log_2(m)层,总共有log_2(m)次比较。

相比之下,一个n元树将需要log_2(n)个比较(使用二叉搜索)来移动到下一层。由于总共有log_n(m)个级别,因此搜索将需要log_2(n)*log_n(m) = log_2(m)个比较。因此,尽管n元树更复杂,但就必要的总体比较而言,它们没有提供任何优势。

(然而,n-ary树在生态位环境中仍然有用。首先想到的例子是四叉树和其他空间划分树,其中每层只使用两个节点划分空间会使逻辑不必要地复杂;以及在许多数据库中使用的b -树,其中限制因素不是在每个级别上进行多少比较,而是一次可以从硬盘加载多少节点)

其他回答

java.util.Set的实现

二叉树最重要的应用之一是平衡二叉搜索树,比如:

红黑树 AVL树 替罪羊树

这些类型的树具有这样的特性,即通过每次插入或删除节点时进行旋转等操作,将左子树和右子树的高度差保持在较小的范围内。

因此,树的整体高度保持为log n的阶数,并且搜索、插入和删除节点等操作在O(log n)时间内执行。c++的STL也以集合和映射的形式实现了这些树。

我认为“纯”二叉树没有任何用处。(教育用途除外) 平衡二叉树,如红黑树或AVL树更有用,因为它们保证O(logn)运算。普通的二叉树最终可能是一个列表(或几乎是列表),在使用大量数据的应用程序中并没有真正的用处。

平衡树通常用于实现映射或集合。 它们也可以用于O(nlogn)排序,即使存在更好的方法。

也可以用于搜索/插入/删除哈希表,它通常比二叉搜索树(平衡与否)有更好的性能。

在需要搜索/插入/删除和排序的应用程序中,(平衡的)二叉搜索树将是有用的。排序可以在适当的位置(几乎,忽略递归所需的堆栈空间),给定一个就绪的构建平衡树。它仍然是O(nlogn),但有一个更小的常数因子,不需要额外的空间(除了新的数组,假设数据必须放入数组中)。另一方面,哈希表不能排序(至少不能直接排序)。

也许它们在一些复杂的算法中也很有用,但说实话,我什么也想不起来。如果我发现更多,我会编辑我的帖子。

其他树如f.e.b +树也广泛应用于数据库中

争论二叉树的性能是没有意义的——它们不是一种数据结构,而是一组数据结构,它们都具有不同的性能特征。虽然不平衡二叉树的搜索性能确实比自平衡二叉树差得多,但对于许多二叉树(如二进制尝试)来说,“平衡”是没有意义的。

二叉树的应用

Binary Search Tree - Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries. Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered. Binary Tries - Used in almost every high-bandwidth router for storing router-tables. Hash Trees - Used in torrents and specialized image-signatures in which a hash needs to be verified, but the whole file is not available. Also used in blockchains for eg. Bitcoin. Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort. Huffman Coding Tree (Chip Uni) - Used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats. GGM Trees - Used in cryptographic applications to generate a tree of pseudo-random numbers. Syntax Tree - Constructed by compilers and (implicitly) calculators to parse expressions. Treap - Randomized data structure used in wireless networking and memory allocation. T-tree - Though most databases use some form of B-tree to store data on the drive, databases which keep all (most) their data in memory often use T-trees to do so.


二叉树比n-ary树更常用于搜索的原因是n-ary树更复杂,但通常不会提供真正的速度优势。

在一个有m节点的(平衡的)二叉树中,从一层移动到下一层需要进行一次比较,并且有log_2(m)层,总共有log_2(m)次比较。

相比之下,一个n元树将需要log_2(n)个比较(使用二叉搜索)来移动到下一层。由于总共有log_n(m)个级别,因此搜索将需要log_2(n)*log_n(m) = log_2(m)个比较。因此,尽管n元树更复杂,但就必要的总体比较而言,它们没有提供任何优势。

(然而,n-ary树在生态位环境中仍然有用。首先想到的例子是四叉树和其他空间划分树,其中每层只使用两个节点划分空间会使逻辑不必要地复杂;以及在许多数据库中使用的b -树,其中限制因素不是在每个级别上进行多少比较,而是一次可以从硬盘加载多少节点)

使用二叉树表示AST的编译器可以使用已知的算法 解析树像海报,有序。程序员不需要提出自己的算法。 因为源文件的二叉树比n元树高,所以构建它需要更多的时间。 以这个生产为例: selstmnt:= "if" "(" expr ")" stmnt "ELSE" stmnt 在二叉树中,它将有3层节点,但n-ary树将有1层(子节点)

这就是为什么基于Unix的操作系统很慢的原因。