就内存而言,二叉搜索树与哈希 table
Binary-search tree vs hash table in terms of memory
我在 Internet 上听到了不同的说法,包括有人明确指出与 hast 表相比,BST 需要更少的内存,因为哈希表一次获得的内存比他们现在需要的多。
能否告诉我每种结构与另一种结构相比的优缺点,仅就记忆而言。
二叉搜索树只不过是一个链表,每个节点有 2 个指针。所需的内存是 O(n) 的顺序,即与存储在其中的元素数相同。
另一方面,哈希映射通常实现为数组。所以,上面总是有一些未使用的space。在此处阅读有关如何在 java 中实现哈希映射的信息:http://java-performance.info/memory-consumption-of-java-data-types-2/
HashMap is built on top of the array of Map.Entry objects. The
implementation ensures that this array length is always equal to at
least max( size, capacity ) / load_factor. Default load factor for
HashMap is 0.75 and default capacity is 16. Load factor specifies
which part of an array could be used for storage and is a value
between 0 and 1. The higher is the load factor, the less space is
being wasted, but HashMap starts to work slower due to increased rate
of collisions. The smaller if the load factor, the more memory is
wasted, but the performance of a HashMap is increasing due to smaller
possibility of collisions.
我在 Internet 上听到了不同的说法,包括有人明确指出与 hast 表相比,BST 需要更少的内存,因为哈希表一次获得的内存比他们现在需要的多。
能否告诉我每种结构与另一种结构相比的优缺点,仅就记忆而言。
二叉搜索树只不过是一个链表,每个节点有 2 个指针。所需的内存是 O(n) 的顺序,即与存储在其中的元素数相同。 另一方面,哈希映射通常实现为数组。所以,上面总是有一些未使用的space。在此处阅读有关如何在 java 中实现哈希映射的信息:http://java-performance.info/memory-consumption-of-java-data-types-2/
HashMap is built on top of the array of Map.Entry objects. The implementation ensures that this array length is always equal to at least max( size, capacity ) / load_factor. Default load factor for HashMap is 0.75 and default capacity is 16. Load factor specifies which part of an array could be used for storage and is a value between 0 and 1. The higher is the load factor, the less space is being wasted, but HashMap starts to work slower due to increased rate of collisions. The smaller if the load factor, the more memory is wasted, but the performance of a HashMap is increasing due to smaller possibility of collisions.