Heap Data Structure

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Heap

A specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C

Extract Minimum Element (Defined)

Finding the minimum element of a min-heap is easy: it's always at the top. First, remove the minimum element and swap it with the last element in the heap (the bottom-most, right-most element). Then we bubble down this element, swapping it with one of it's children until the min-heap property is restored. The swap occurs with the smaller child to maintain heap ordering.

Typical Heap Implementation (Defined)

Heaps are usually implemented in an array (fixed size or dynamic array), and do not require pointers between elements. After an element is inserted into or deleted from a heap, the heap property may be violated and the heap must be balanced by internal operations.

Insert in Min Heap (Defined)

When we insert into a min-heap we always start by inserting the element at the bottom. We insert at the rightmost spot as as to maintain the complete tree property. Then, we fix the tree by swapping the new element with it's parent, until we find an appropriate spot for the element. We essentially bubble up the minimum element. Takes O(log n) time.

insert (Operation)

adding a new key to the heap (a.k.a., push)

heapify (Operation)

create a heap out of given array of elements

create-heap (Operation)

create an empty heap

delete (Operation)

delete an arbitrary node (followed by moving last node and sifting to maintain heap)

find-max/min (Operation)

find a maximum item of a max-heap, or a minimum item of a min-heap, respectively (a.k.a. peek)

meld (Operation)

joining two heaps to form a valid new heap containing all the elements of both, destroying the original heaps.

merge/union (Operation)

joining two heaps to form a valid new heap containing all the elements of both, preserving the original heaps.

sift-down (Operation)

move a node down in the tree, similar to sift-up; used to restore heap condition after deletion or replacement.

sift-up (Operation)

move a node up in the tree, as long as needed; used to restore heap condition after insertion. Called "sift" because node moves up the tree until it reaches the correct level, as in a sieve.

replace (Operation)

pop root and push a new key. More efficient than pop followed by push, since only need to balance once, not twice, and appropriate for fixed-size heaps.

size (Operation)

return the number of items in the heap.

isEmpty (Operation)

return true if the heap is empty, false otherwise.

extract-max/min (Operation)

returns the node of maximum value from a max heap [or minimum value from a min heap] after removing it from the heap (a.k.a., pop)

increase/decrease key (Operation)

updating a key within a max- or min-heap, respectively


Ensembles d'études connexes

Chapter 19 Magnetism (for final)

View Set

Constitution Test Review- Constitution Principles

View Set

The Scout Law - Cub Scouts - Boy Scouts of America

View Set

chapter 52 Removable Prosthodontics

View Set

Chambers of the Heart/Valve (The Cardiovascular System: Heart)

View Set

CHAPTER 4 EMPLOYEE TRAINING AND DEVELOPMENT

View Set

Chapter 31: Abdominal and Genitourinary Injuries

View Set

MAT 135 CH. 4 - Scatter Diagrams

View Set

Research Methods Exam 1: Ch. 1-7

View Set