Is Heap considered an Abstract Data Type?

折月煮酒 提交于 2020-01-02 01:45:07

问题


I'm taking Data-Structure course and got a little confused about what is considered to be an ADT (Abstract Data Type) and what isn't (and if it isn't an ADT then it must be the implementation?).

Specifically, I'm talking about Heap.

I've read in Wikipedia that " heap is a specialized tree-based data structure" does that mean it is an ADT? if so, then I can't understand the following line, also from Wikipedia "The heap is one maximally efficient implementation of an abstract data type called a priority queue".

I mean, can Heap be an ADT and also be an implementation of some other ADT (in this case an implementation of priority queue? I understand the concept of ADT and in the context of Binary Heap I understand that it can be implemented using array where arr[i] is the parent of arr[2i] and arr[2i + 1] I'm just confused about whether a Heap can be in one hand an ADT which implemented using array and on the other hand a data-structure that implements other ADT.

Would like to get some clarifications about this.


回答1:


Heap is not an ADT. It is a Data Structure.


The obligatory Wikipedia quote:

In computer science, an abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. This contrasts with data structures, which are concrete representations of data, and are the point of view of an implementer, not a user.


Bonus content inspired from Code Complete -2 by Steve McConnell.

Data Structures are low-level implementation domain items, in contrast with ADTs which work in the problem domain. ADTs let you manipulate real-world entities rather than low-level, implementation entities. Instead of inserting a node into a linked-list or adding an item to a heap, you can add a cell to a spreadsheet, a new type of window to window types, or another passenger to a train simulation.

  • You can clearly see that heap has defined semantics like insert(), heapify(), peek(), getTop() etc. - listed here in detail. Hence it is a data structure.

  • However, if you simulate a game of Tetris where adding a new block will simply go and sit on top of wherever it falls, this Tetris game's UI will actually be an ADT.




回答2:


Heap is not considered an abstract data type.

Heap is a specialized tree-based data structure that is an implementation of the abstract data type called Priority Queue.

See https://en.wikipedia.org/wiki/Heap_(data_structure)




回答3:


I would try to clarify about this confusion other way round. Quoting wikipedia from here:

While priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is an abstract concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array.

So heap is just an implementation of Priority Queue abstract data type(ADT) which can be implemented in many other listed below but might no be limited to:

  • Unordered Array
  • Unordered List
  • Ordered Array
  • Ordered List
  • Binary Search Tree (BST)
  • Balanced Binary Search Tree (AVL-tree)
  • Binary heap (asked by OP)

Comparing the time efficiency of heap implementation for main operations in Priority Queue ADT with other possible implementations:

----------------------------------------------------------------------------
| Implementation | Insertion   | Deletion(DeleteMax/DeleteMin)|Find Max/Min
----------------------------------------------------------------------------
| Unordered Array| 1           | n                            | n
----------------------------------------------------------------------------
| Unordered List | 1           | n                            | n
----------------------------------------------------------------------------
| Ordered Array  | n           | 1                            | 1
----------------------------------------------------------------------------
| Ordered List   | n           | 1                            | 1
----------------------------------------------------------------------------
| BST            | logn (avg.) | logn (avg.)                  | logn (avg.)
----------------------------------------------------------------------------
| Balanced BST   | log n       | log n                        | log n
----------------------------------------------------------------------------
| Binary Heaps   | log n       | log n                        | 1



回答4:


I am showing you the complete line from wikipedia whereas you quoted just the part of line which confused you.maybe you have better understood it if you just got to next part of line.

The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact priority queues are often referred to as "heaps", regardless of how they may be implemented. A common implementation of a heap is the binary heap, in which the tree is a binary tree

here it says that

in fact priority queues are often referred to as "heaps", regardless of how they may be implemented.

because of the popularity of heap data structure(DS), in implementing priority queues ADT.priority queues ADT are often referred as heap

In the next line of wikipedia quote

A common implementation of a heap is the binary heap, in which the tree is a binary tree

Here first heap means priority queue and binary heap means in short heap means data structure.

hence forth when you see heap (in context of ADT) it actually means priority queue ADT and heap is a implementation of it.hence heap is a DS.

also where you quotes this:

" heap is a specialized tree-based data structure"

it simply means it is a DS not ADT.




回答5:


1) Java Software Structures,International Edition [John Lewis, Joseph Chase]

An abstract data type (ADT) is a data type whose values and operations are not inherently defined within a programming language. It is abstract only in that the details of its implementation must be defined and should be hidden from the user. A collection, therefore, is an abstract data type.

2) Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne

Using abstract data types You do not need to know how a data type is implemented in order to be able to use it.

So if you only are designing a behaviour like this:

3) Wikipead heap operations:

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.

Realy this is only an ADT, and the implementation is the Data Structure, in fact, one of the two books defined the heap as ADT

So, by 1 and 2, indicate that 3 can be a ADT of a heap, because you can implementet the heap with an array and with a pointers for example. and the same for the priority queue, but the time complexity can change

In the Java Software Structures,International Edition [John Lewis, Joseph Chase] has the HeapADT

public interface HeapADT<T> extends BinaryTreeADT<T>
{
/**
* Adds the specified object to this heap.
*
* @param obj the element to be added to this heap
*/
public void addElement (T obj);
/**
* Removes element with the lowest value from this heap.
*
* @return the element with the lowest value from this heap
*/
public T removeMin();
/**
* Returns a reference to the element with the lowest value in
* this heap.
*
* @return a reference to the element with the lowest value in this heap
*/
public T findMin();
}

but in Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne has the PriorityQueue as an 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

And as a DS: https://algs4.cs.princeton.edu/24pq/MaxPQ.java.html

And I think that they only say this:

The binary heap is a data structure that can efficiently support the basic priority-queue operations. In a binary heap, the keys

Because they are talk about the implementation:

are stored in an array such that each key is guaranteed to be larger than (or equal to) the keys at two other specific positions.

Another example could be say, about List, List could be an adt and static array and dinamic array are the data structures to implement a List, but another authors define the ADT to, because is the expected behaviour.

You can chek this arrays adt in this another book: DATA STRUCTURES IN C++ by N. S. KUTTI, P. Y. PADHYE (you can check here)

So finnaly, if you are defining the behaviour of something I would say that is a ADT, and if you are doing the implementation I would say that is a DS.

Edit:

Another contradiction between books

Data Structures & Algorithms in Java Robert Lafore

A priority queue is a more specialized data structure



来源:https://stackoverflow.com/questions/38124644/is-heap-considered-an-abstract-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!