You are on page 1of 6

Term Paper On HEAP FUNCTION (A.D.

A)

Introduction In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.) The maximum number of children each node can have depends on the type of heap, but in many types it is at most two. The heap is one maximally-efficient implementation of an abstract data type called a priority queue. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort. A heap data structure should not be confused with the heap which is a common name for dynamically allocated memory. The term was originally used only for the data structure. Some early popular languages such as Lisp provided dynamic memory allocation using heap data structures, which gave the memory area its name.

Heap Functions As a heap is an abstract data structure and can establish a set of functions to maintain the heap. T These functions are:
1. Init

2. 3. 4. 5. 6. 7. 8. 9.

make_heap() push_heap() pop_heap() sort_heap() add remove isFull isEmpty

1. INIT: The init function sets the node (in this case array index) to add the next node. The isFull and isEmpty functions also operate the same way that we have seen before.

2. ADD : The add function will add the new item to the next available slot in the heap, and then adjust the heap so that the heap condition is maintained. This may involve swapping the contents of parent and child nodes.

Suppose we are adding the value 6 to the heap. We will initially place it at node 5, which will breach the heap condition as 6 < 7. In this case the add function must restore the heap condition by swapping the contents of element [2] and element [5]. There may be cases where further swaps must be made up the heap (consider the case where the value 1 is added to node 6). Note that the only promise that a heap makes is that the first value is at the root node of the heap, and that we may not be able to say anything about the relative order between nodes 2 and 3. 3. REMOVE: To implement the remove function, we make the assumption that we want to retrieve the first value from the heap. This is easy enough to do, but the remove function must also maintain the heap property.

One way for the remove function to maintain the heap property can be achieved using the following steps:

Place the value from the last (rightmost) node in the heap Reduce the number of nodes counter by one Compare the new root node value with its two children If needed, exchange the root node value with the lower valued of the two children Continue down the heap, maintaining the heap property for each subtree Once the heap property has been re-established, the remaining lowest value must be at the root node ready for the next removal.

4. Make_heap : make_heap() takes a range, specified by random access iterators, and converts it into a heap. The number of steps required is a linear function of the number of elements in the range. void make_heap (RandomAccessIterator first, RandomAccessIterator last [, Compare ]); 5. Push_heap() : A new element is added to a heap by inserting it at the end of a range (using the push_back() member function of a vector or deque, for example), followed by an invocation of the algorithm push_heap(). The push_heap() algorithm restores the heap property, performing at most a logarithmic number of operations. void push_heap (RandomAccessIterator first, RandomAccessIterator last [, Compare ]); 6.Pop_Heap() : The algorithm pop_heap() swaps the first and final elements in a range, then restores to a heap the collection without the final element. The largest value of the original collection is therefore still available as the last element in the range (accessible, for example, using the back() member function in a vector, and removable using the pop_back() member function), while the remainder of the collection continues to have the heap property.

The pop_heap() algorithm performs at most a logarithmic number of operations. void pop_heap (RandomAccessIterator first, RandomAccessIterator last [, Compare ]); 7.Sort_Heap ( ): The algorithm pop_heap() swaps the first and final elements in a range, then restores to a heap the collection without the final element. The largest value of the original collection is therefore still available as the last element in the range (accessible, for example, using the back() member function in a vector, and removable using the pop_back() member function), while the remainder of the collection continues to have the heap property. The pop_heap() algorithm performs at most a logarithmic number of operations. void pop_heap (RandomAccessIterator first, RandomAccessIterator last [, Compare ]); FUNCTIONS IN HEAP FOR MEMORY MANAGEMENT IN SYSTEMS A private heap is a block of one or more pages in the address space of the calling process. After creating the private heap, the process uses functions such as HeapAlloc and HeapFree to manage the memory in that heap. The heap functions can also be used to manage memory in the process's default heap, using the handle returned by the GetProcessHeap function. New applications should use the heap functions instead of the global and local functions for this purpose. There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions. For a complete list of functions, see the table in Memory Management Functions.

HeapCreate function creates a private heap object from which the calling process can allocate memory blocks by using the HeapAllocfunction. HeapCreate specifies both an initial size and a maximum size for the heap. The initial size determines the number of committed, read/write pages initially allocated for the heap. The maximum size determines the total number of reserved pages. These pages create a contiguous block in the virtual address space of a process into which the heap can grow. Additional pages are automatically committed from this reserved space if requests by HeapAlloc exceed the current size of committed pages, assuming that the physical storage for it is available. Once the pages are

committed, they are not decommitted until the process is terminated or until the heap is destroyed by calling the HeapDestroyfunction. The HeapAlloc function allocates a specified number of bytes from a private heap and returns a pointer to the allocated block. This pointer can be used in the HeapFree, HeapReAlloc, HeapSize, and HeapValidate functions. Memory allocated by HeapAlloc is not movable. The address returned by HeapAlloc is valid until the memory block is freed or reallocated; the memory block does not need to be locked. A possible use for the heap functions is to create a private heap when a process starts up, specifying an initial size sufficient to satisfy the memory requirements of the process. If the call to the HeapCreate function fails, the process can terminate or notify the user of the memory shortage; if it succeeds, however, the process is assured of having the memory it needs. Memory requested by HeapCreate may or may not be contiguous. Memory allocated within a heap by HeapAlloc is contiguous. You should not write to or read from memory in a heap except that allocated by HeapAlloc, nor should you assume any relationship between two areas of memory allocated by HeapAlloc. HeapDestroy function destroys a private heap object. It decommits and releases all the pages of the heap object, and it invalidates the handle to the heap.

You might also like