What is a heap?--reference

A heap is a partially sorted binary tree. Although a heap is not
completely in order, it conforms to a sorting principle: every node has a value
less (for the sake of simplicity, we will assume that all orderings are from
least to greatest) than either of its children. Additionally, a heap is a
"complete tree" -- a complete tree is one in which there are no gaps between
leaves. For instance, a tree with a root node that has only one child must have
its child as the left node. More precisely, a complete tree is one that has
every level filled in before adding a node to the next level, and one that has
the nodes in a given level filled in from left to right, with no
breaks.

Why use a
heap?

A heap can be thought of as a priority queue; the most
important node will always be at the top, and when removed, its replacement will
be the most important. This can be useful when coding algorithms that require
certain things to processed in a complete order, but when you don‘t want to
perform a full sort or need to know anything about the rest of the nodes. For
instance, a well-known algorithm for finding the shortest distance between nodes
in a graph, Dijkstra‘s Algorithm, can be optimized by using a priority
queue.

Heaps can also be used to sort data. A heap sort is
O(nlogn) efficiency, though it is not the fastest possible sorting algorithm.
Check out thistutorial
heap sort
 for more information related to heap
sort. 

How
do you implement a heap?

Although the concept of a heap is
simple, the actual implementation can appear tricky. How do you remove the root
node and still ensure that it is eventually replaced by the correct node? How do
you add a new node to a heap and ensure that it is moved into the proper
spot?

The answers to these questions are more straight
forward than meets the eye, but to understand the process, let‘s first take a
look at two operations that are used for adding and removing nodes from a heap:
upheaping and downheaping. 

Upheap:
The upheap process is used to add a node to a heap. When you upheap a node, you
compare its value to its parent node; if its value is less than its parent node,
then you switch the two nodes and continue the process. Otherwise the condition
is met that the parent node is less than the child node, and so you can stop the
process. Once you find a parent node that is less than the node being upheaped,
you know that the heap is correct--the node being upheaped is greater than its
parent, and its parent is greater than its own parent, all the way up to the
root. 

Downheap: The downheap process
is similar to the upheaping process. When you downheap a node, you compare its
value with its two children. If the node is less than both of its children, it
remains in place; otherwise, if it is greater than one or both of its children,
then you switch it with the child of lowest value, thereby ensuring that of the
three nodes being compared, the new parent node is lowest. Of course, you cannot
be assured that the node being downheaped is in its proper position -- it may be
greater than one or both of its new children; the downheap process must be
repeated until the node is less than both of its
children. 

When
you add a new node to a heap, you add it to the rightmost unoccupied leaf on the
lowest level. Then you upheap that node until it has reached its proper
position. In this way, the heap‘s order is maintained and the heap remains a
complete tree.

Removing
the root node from a heap is almost as simple: when you take the node out of the
tree, you replace it with "last" node in the tree: the node on the last level
and rightmost on that level.

Once the top node has been
replaced, you downheap the node that was moved until it reaches its proper
position. As usual, the result will be a proper heap, as it will be complete,
and even if the node in the last position happens to be the greatest node in the
entire heap, it will do no worse than end up back where it
started. 

Efficiency
of a heap

Whenever you work with a heap, most of the time
taken by the algorithm will be in upheaping and downheaping. As it happens, the
maximum number of levels of a complete tree is log(n)+1, where n is the number
of nodes in the tree. Because upheap or downheap moves an element from one level
to another, the order of adding to or removing from a heap is O(logn), as you
can make switches only log(n) times, or one less time than the number of levels
in the tree (consider that a two level tree can have only one
switch).

reference from
:http://www.cprogramming.com/tutorial/computersciencetheory/heap.html

What is a heap?--reference,布布扣,bubuko.com

时间: 2024-08-28 17:38:52

What is a heap?--reference的相关文章

OpenJDK与HashMap

OpenJDK的非堆JDK增强提议(JDK Enhancement-Proposal,JEP)试图标准化一项基础设施,它从Java6开始,只能在HotSpot和OpenJDK内部使用.这种设施能够像管理堆内存那样管理非堆内存,同时避免了使用堆内存所带来的一些限制.对于上百万短期存在的对象/值来说,堆内存工作起来是很好的,但是如果你想要增加一些其他的需求,如几十亿的对象/值的话,假若你想避免持续增加的GC暂停,那么你需要做一些更加有创造性的工作.在有些场景下,你还需要完全避免暂停.非堆提供了构建“

[Java Basics] Stack, Heap, Constructor

Good about Java: friendly syntax, memory management[GC can collect unreferenced memory resources], object-oriented features, portability. Stack Stores method invocations, local variables(include object reference, but the object itself is still stored

Java Reference Types

References Java provides two different types/classes of Reference Objects: strong and weak. Weak Reference Objects can be further divided into soft and phantom. Strong Reference StringBuilder builder = new StringBuilder(); This is the default type/cl

jvm的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集(转)

深入Java虚拟机:JVM中的Stack和Heap(转自:http://www.cnblogs.com/laoyangHJ/archive/2011/08/17/gc-Stack.html) 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认识Stack和Heap,并通过这些原理认清Java中静态方法和静态属性的问题. 一般,JVM的内存分为两部分:Stack和Heap. Stack(栈)是JVM的内存指令区.Stack管理很简单,push

Heap Only Tuples (HOT)

Introduction ------------ The Heap Only Tuple (HOT) feature eliminates redundant index entries and allows the re-use of space taken by DELETEd or obsoleted UPDATEd tuples without performing a table-wide vacuum. It does this by allowing single-page va

a堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有什么异同,以及和数据结构中的堆栈有何关系? 一.Java 堆存储空间 堆内存(堆存储空间)会在Java运行时分配给对象(Object)或者JRE的类.只要我们创建了一个对象,那么在堆中肯定会分配一块存储空间给这个对象.而我们熟知的Java垃圾回收就是在堆存储空间上进行的,用以释放那些没有任何引用指向

C# Heap(ing) Vs Stack(ing) in .NET [C# 堆和栈的使用以及垃圾回收原理]

最近在<C#Corner>上看到了一篇关于.NET内存管理以及垃圾回收的文章,虽说是英文的内容,但还是硬着头皮读了下来.发现并不是我原本想象中的那么枯燥,因为语言通俗而且还有很多图片示意,感觉让我又对"堆"和"栈"以及垃圾回收机制有了更加深刻的理解和认知,记录下来提醒自己尽量书写优质的代码,而不是只管实现功能,不管性能优劣去蛮干.  [文章出自: http://www.c-sharpcorner.com/article/c-sharp-heaping-v

条款21:必须返回对象时,别妄想返回其reference

条款21:必须返回对象时,别妄想返回其reference 引用只是对象的一种别名当使用引用的时候,请确认他的另一个身份在哪? class Rational { public: Rational(int x, int y) : m_x(x), m_y(y){} //返回const是属于类型保护,friend修饰,以后条款详说 friend const Rational operator + (const Rational &lhs, const Rational &rhs) { Ration

Heap和Heapify

最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQueue. 这里Binary Heap我们使用的是array represetation,数组形式. 第0个元素我们留空,从第一个元素开始存储, 第一个元素也将是PQ里最大的元素. 特点是假如父节点位置是 k, 那么两个子节点的位置就是 2 * k 和 2 * k + 1.这样很方便计算,知道父节点