Difference between priority queue and a heap

后端 未结 5 1268
我在风中等你
我在风中等你 2021-01-30 16:38

It seems that a priority queue is just a heap with normal queue operations like insert, delete, top, etc. Is this the correct way to interpret a priority queue? I know you can b

5条回答
  •  时光说笑
    2021-01-30 17:09

    The C++ Standard Template Library provides the make_heap, push_heap and pop_heap algorithms for heaps (usually implemented as binary heaps), which operate on arbitrary random access iterators. It treats the iterators as a reference to an array, and uses the array-to-heap conversion. It also provides the container adaptor priority_queue, which wraps these facilities in a container-like class. However, there is no standard support for the decrease/increase-key operation.

    priority_queue referes to abstract data type defined entirely by the operations that may be performed on it. In C++ STL prioroty_queue is thus one of the sequence adapters - adaptors of basic containers (vector, list and deque are basic because they cannot be built from each other without loss of efficiency), defined in header ( in my case actually). As can be seen from its definition, (as Bjarne Stroustrup says):

    container adapter provides a restricted interface to a container. In particular, adapters do not provide iterators; they are intended to be used only through their specialized interfaces.

    On my implementation prioroty_queue is described as

    /**
       *  @brief  A standard container automatically sorting its contents.
       *
       *  @ingroup sequences
       *
       *  This is not a true container, but an @e adaptor.  It holds
       *  another container, and provides a wrapper interface to that
       *  container.  The wrapper is what enforces priority-based sorting 
       *  and %queue behavior.  Very few of the standard container/sequence
       *  interface requirements are met (e.g., iterators).
       *
       *  The second template parameter defines the type of the underlying
       *  sequence/container.  It defaults to std::vector, but it can be
       *  any type that supports @c front(), @c push_back, @c pop_back,
       *  and random-access iterators, such as std::deque or an
       *  appropriate user-defined type.
       *
       *  The third template parameter supplies the means of making
       *  priority comparisons.  It defaults to @c less but
       *  can be anything defining a strict weak ordering.
       *
       *  Members not found in "normal" containers are @c container_type,
       *  which is a typedef for the second Sequence parameter, and @c
       *  push, @c pop, and @c top, which are standard %queue operations.
       *  @note No equality/comparison operators are provided for
       *  %priority_queue.
       *  @note Sorting of the elements takes place as they are added to,
       *  and removed from, the %priority_queue using the
       *  %priority_queue's member functions.  If you access the elements
       *  by other means, and change their data such that the sorting
       *  order would be different, the %priority_queue will not re-sort
       *  the elements for you.  (How could it know to do so?)
    

    template:

      template,
           typename _Compare  = less >
        class priority_queue
        {
    

    In opposite to this, heap describes how its elements are being fetched and stored in memory. It is a (tree based) data structure, others are i.e array, hash table, struct, union, set..., that in addition satisfies heap property: all nodes are either [greater than or equal to] or [less than or equal to] each of its children, according to a comparison predicate defined for the heap.

    So in my heap header I find no heap container, but rather a set of algorithms

      /**
       * @defgroup heap_algorithms Heap Algorithms
       * @ingroup sorting_algorithms
       */ 
    

    like:

    • __is_heap_until
    • __is_heap
    • __push_heap
    • __adjust_heap
    • __pop_heap
    • make_heap
    • sort_heap

    all of them (excluding __is_heap, commented as "This function is an extension, not part of the C++ standard") described as

       *  @ingroup heap_algorithms
       *
       *  This operation... (what it  does)
    

提交回复
热议问题