A heap is a tree data structure where higher levels of the tree always contain greater (or lesser, if it's set up that way) values than lower levels. "The" heap is a bunch of free RAM that a program has available for dynamic allocation. They're both called "heap," but what does the one have to do with the other?
It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. Every time when we made an object it always creates in Heap-space and the referencing information to these objects are always stored in Stack-memory.
A heap is a tree-like data structure where there is a relationship between the parent and child nodes. The figure below is an example of a heap. Heap Example. In this heap example, the relationship between the parent and child is the parent is always greater than all of its children.
A memory heap is called a heap in the same way you would refer to a laundry basket as a "heap of clothes". This name is used to indicate a somewhat messy place where memory can be allocated and deallocated at will.
The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.
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...
See this very site for an exploration of the origin of the name "heap" for the free store of memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With