“a”和“a”之间有什么关系? 堆和“the” 堆?
堆是一种树形数据结构,其中树的较高级别始终包含比较低级别更大(或更少,如果以这种方式设置)的值。 “”堆是程序可用于动态分配的一堆空闲 RAM。 它们都被称为“堆”,但是其中一个与另一个有什么关系呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
堆是一种树形数据结构,其中树的较高级别始终包含比较低级别更大(或更少,如果以这种方式设置)的值。 “”堆是程序可用于动态分配的一堆空闲 RAM。 它们都被称为“堆”,但是其中一个与另一个有什么关系呢?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
老实说,没什么。 我想,“堆”这个词只是简单地与它的日常(非技术)用法一起使用,并作为相当好的类比分别应用于这两个概念。
在第一种情况(树数据结构含义)中,描述堆是最合适的,因为“更大”的对象被放置在树中更高的位置(其中“更大”由任意键函数确定) -即,有一种较小的物体堆积在较大的物体之上(或者较大的物体堆积在上面,取决于您如何看待它)。 这就是我的解释; 无论谁第一个将“堆”这个名字应用到这个数据结构中,都认为这个名字在他的脑海中是合适的,但它就被卡住了。
在第二种情况(RAM 块)中,堆的名称可能更明显一些。 这里的“堆”只是“以高度任意顺序排列的大量事物的集合”,这似乎适用于常见用途,也适用于动态分配的内存块。
无论如何,我不会担心这两个想法之间可以得出的抽象隐喻相似之处。 将它们完全分开对待,在任何情况下都不会出错。
编辑:看起来基于树的数据结构可能的名字来源于抽象代数堆,这在计算机科学中相当常见。 不过,我不想证实或否认这一点……
Nothing much, to be honest. I would imagine that the word heap was simply taken with it's everday (non-technical) usage and applied to these two concepts individually as reasonably good analogies.
In the first case (tree data structure meaning), the description heap is most appropiate because "greater" objects are placed higher up in the tree (where "greater" is determined by an arbitrary key function) - i.e. there's a sort of piling of smaller objects on top of larger ones (or larger on top, depending how you think of it). This is just how I'd interpret it; whoever first applied the name heap to this data-structure thought it was an appropiate name in his mind, and it's just stuck.
In the second case (chunks of RAM), the name of heap is maybe a bit more evident. "Heap" is just "a large collection of things in a highly arbitrary order" here, which would seem to apply just as well in common usage as it does to dynamically allocated chunks of memory.
In any case, I wouldn't worry about the abstract metaphorical similarities you can draw between the two ideas. Treat them completely seperately and you won't go wrong in any situation.
Edit: It seems the tree-based data structure may have taken its name from the heap of abstract algebra, as is reasonably common within computer science. However, I wouldn't want to confirm or deny this...
他们都有相同的名字,仅此而已。
那里的“堆”从未被安排为实际的堆数据结构。
They both have the same name, that's about it.
There 'the heap' is never arranged as an actual heap data structure.
堆(数据结构)之所以这样称呼,是因为如果你把它画出来,它看起来就像一个堆。 堆(内存)之所以被称为堆,是因为它以某种方式组织起来,但并不完全。 您在堆上积累数据,但其中可能存在漏洞和不规则之处。 就好像你把文件堆成一堆。 有时您会从底部取出一个。 它具有堆的形式,即以某种方式组织但不完全。
The heap (datastructure) is called like that because if you draw it it looks like a heap. The heap (memory) is called a heap because it is somehow organized but not fully. You accumulate data on a heap but you might have holes in it and irregularities. It's as if you'd put papers on a heap. Sometimes you remove one from the bottom. This has a form of a heap, i.e. somehow organized but not fully.
没有什么。 没有关系。
Nothing. No relation.
两者之间的唯一关系是名称“堆”。
The only relationship between the two is the name "heap."
使问题更加复杂的是:在某些系统(例如 Microsoft Windows)上,内存分配意义上存在多个“堆”。 “”堆仅仅是默认堆。 但是如果您调用
HeapAlloc()
,您可以从哪个内存分配中选择您想要的子分配。To futher complicate the question: on some systems (e.g. Microsoft Windows), there are multiple "heaps" in the memory allocation sense. "The" heap is merely the default heap. But if you call
HeapAlloc()
, you can choose from which memory allocation you want a sub-allocation.对于数据结构,我会附议 IJ Kennedy 的答案
这里要记住的概念是高度。
使用这个术语的不幸之处在于,我们大多数人都将“堆”理解为杂乱无章的堆,事实上,这就是内存堆的预期心理图像(< em>与堆栈不同,此内存存储在 RAM 中并不连续)。
我们主要将顺序实现到堆中(想想最小堆和最大堆),而不是简单地将数据临时放入其中。 但是,如果不使用顺序,则“堆”是更通用的术语。 IMO,树对此感觉是一个更自然的术语,但我认为直觉上人们在听到“树”时会想到分叉,而这种结构要记住的重要概念是“深度” ”和“高度”,因为我们通常谈论“平衡堆”或堆的不同“深度级别”。
For the data structure, I would second the answer from I. J. Kennedy
The concept to remember here is height.
The unfortunate thing about using this term is that most of us understand "heap" to mean a disorganized pile, and indeed that is the intended mental image for the memory heap (this memory storage is not contiguous in RAM, unlike the stack).
We mostly implement order into heaps (think min heap and max heap) rather than simply throwing data into them ad hoc. However, if order is not used, heap is the more general term. IMO, tree feels a more natural term for this, but I think intuitively people envision bifurcation when they hear "tree" and the important concept to remember with this structure is "depth" and "height" since we typically speak of "balancing a heap" or different "depth levels" of a heap.
来自answers.com 的定义
堆:放置或扔出的一组东西,一个放在另一个上面:一堆脏抹布躺在角落里。
由于以无序的方式扔东西的概念图像,这只是基本的命名。 正如其他发帖者指出的那样,堆并不是以堆数据结构的形式组织的。 这取决于系统库中的内存分配例程(例如,检查 malloc 的工作原理)
Definition from answers.com
Heap: A group of things placed or thrown, one on top of the other: a heap of dirty rags lying in the corner.
It's just basic naming due to the conceptual image of throwing things in an unordered fashion. As other posters point out, the heap is not organized as a heap data structure. That depends on the memory allocation routines in your system library (eg. check how malloc works)
堆是一种数据结构,实际上是一个具有一些额外属性的完全二叉树。 有两种类型的堆:
在最小堆中,根具有树中的最低值,当您弹出根时,下一个最低的元素位于顶部。 为了将树转换为堆,我们使用 heapify 算法。 在 C++ 中它也被称为优先级队列。 通常,作为一名有竞争力的程序员,我们使用 STL 函数来创建堆,这样我们就不必从头开始忙于创建堆。 最大堆正好相反,最大堆位于根部。 通常使用堆,因为它删除和插入元素的时间复杂度为 O(logN),因此甚至可以使用 10^6 等严格约束。
现在我可以理解您对内存中的堆和堆数据结构之间的混淆,但它们是完全不同的东西。 数据结构中的堆只是一种存储数据的方式。
heap is a data structure which is actually a complete binary tree with some extra properties. There are 2 types of Heaps:
in min heap the root has the lowest value in the tree and when you pop out the root the next lowest element comes on the top. To convert a tree into heap we use heapify algorithm. It is also know as priority queue in c++. and usually as a competitive programmer we use STL function for heap so that we dont have to get into the hustle of creating a heap from scratch. Max heap is just the opposite with largest at the root. Usually heap is used because it has a O(logN) time complexity for removing and inserting elements and hence can even work with tight constraints like 10^6.
Now i can understand you confusion between heap in memory and heap data structure but they are completely different things. Heap in data structure is just a way to store the data.