1) Java, [ , ]
(ADT) - , . , . , , .
2) , 4-
, , .
, :
3) Wikipead:
Basic
find-max (or find-min): find a maximum item of a max-heap, or a minimum item of a min-heap, respectively (a.k.a. peek)
insert: adding a new key to the heap (a.k.a., push[4])
extract-max (or extract-min): 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[5])
delete-max (or delete-min): removing the root node of a max heap (or min heap), respectively
replace: 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.[6]
Creation
create-heap: create an empty heap
heapify: create a heap out of given array of elements
merge (union): joining two heaps to form a valid new heap containing all the elements of both, preserving the original heaps.
meld: joining two heaps to form a valid new heap containing all the elements of both, destroying the original heaps.
Inspection
size: return the number of items in the heap.
is-empty: return true if the heap is empty, false otherwise.
Internal
increase-key or decrease-key: updating a key within a max- or min-heap, respectively
delete: delete an arbitrary node (followed by moving last node and sifting to maintain heap)
sift-up: 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.
sift-down: move a node down in the tree, similar to sift-up; used to restore heap condition after deletion or replacement.
, ADT, - , , ADT
, 1 2 , 3 ADT , , . ,
Java Software Structures [ , ] HeapADT
public interface HeapADT<T> extends BinaryTreeADT<T>
{
public void addElement (T obj);
public T removeMin();
public T findMin();
}
, 4- PriorityQueue ADT:
public class MaxPQ< Key extends Comparable<Key>>
MaxPQ() create a priority
queueMaxPQ(int max) create a priority queue of initial capacity max
MaxPQ(Key[] a) create a priority queue from the keys in a[]
void insert(Key v) insert a key into the priority queueKey
max() return the largest keyKey
delMax() return and remove the largest key
boolean isEmpty() is the priority queue empty?
int size() number of keys in the priority queue
DS: https://algs4.cs.princeton.edu/24pq/MaxPQ.java.html
, :
- , .
:
, ( ) .
, List List adt, List, ADT , .
: C++ . . , . . ( )
, , -, , ADT, , , DS.
:
& Java
-