Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Data structures 堆是否被视为抽象数据类型?_Data Structures_Heap_Priority Queue_Abstract Data Type - Fatal编程技术网

Data structures 堆是否被视为抽象数据类型?

Data structures 堆是否被视为抽象数据类型?,data-structures,heap,priority-queue,abstract-data-type,Data Structures,Heap,Priority Queue,Abstract Data Type,我正在学习数据结构课程,对什么是ADT(抽象数据类型)和什么不是(如果它不是ADT,那么它一定是实现?)有点困惑 具体来说,我说的是堆 我在维基百科上读到“heap是一种基于树的特殊数据结构”,这是否意味着它是一种ADT?如果是这样的话,那么我无法理解下面这句话,同样来自维基百科“堆是一种称为优先级队列的抽象数据类型的最高效率实现” 我的意思是,堆可以是ADT,也可以是其他ADT的实现(在本例中是优先级队列的实现?我理解ADT的概念,在二进制堆的上下文中,我理解它可以使用数组来实现,其中arr[

我正在学习数据结构课程,对什么是ADT(抽象数据类型)和什么不是(如果它不是ADT,那么它一定是实现?)有点困惑

具体来说,我说的是堆

我在维基百科上读到“heap是一种基于树的特殊数据结构”,这是否意味着它是一种ADT?如果是这样的话,那么我无法理解下面这句话,同样来自维基百科“堆是一种称为优先级队列的抽象数据类型的最高效率实现”

我的意思是,堆可以是ADT,也可以是其他ADT的实现(在本例中是优先级队列的实现?我理解ADT的概念,在二进制堆的上下文中,我理解它可以使用数组来实现,其中
arr[I]
arr[2i]
arr[2i+1]
我只是对堆是否可以一方面是使用数组实现的ADT,另一方面是实现其他ADT的数据结构感到困惑


希望对此进行一些澄清。

堆不被视为抽象数据类型

Heap是一种专门的基于树的数据结构,是名为Priority Queue的抽象数据类型的实现


请参见

堆不是ADT。它是一种数据结构。


维基百科必须引用:

在计算机科学中,抽象数据类型(ADT)是一种数学模型 数据类型的模型,其中数据类型由其行为定义 (语义学)从数据用户的角度,特别是 就可能的值而言,对这种类型的数据可能进行的操作, 以及这些操作的行为。这与数据形成对比 结构是数据的具体表示形式,是 从实现者而不是用户的角度来看


奖励内容灵感来源于史蒂夫·麦康奈尔的《代码完成-2》。

与在问题域中工作的ADT相比,数据结构是低级实现域项。ADT允许您操作真实世界的实体,而不是低级实现实体。您不必将节点插入链表或将项添加到堆中,而可以将单元格添加到电子表格中,这是wi的一种新型窗口ndow类型,或其他乘客到列车模拟

  • 您可以清楚地看到,heap定义了一些语义,如insert()、heapify()、peek()、getTop()等,详细列出了这些语义,因此它是一个数据结构

  • 然而,如果你模拟一个俄罗斯方块游戏,添加一个新的方块将简单地放在它落下的地方,这个俄罗斯方块游戏的UI实际上是一个ADT


我会试图以另一种方式澄清这一困惑。引用维基百科:

虽然优先级队列通常是通过堆实现的,但它们是 概念上不同于堆。优先级队列是一个抽象的队列 类似“列表”或“地图”的概念;正如列表可以实现一样 通过链表或数组,可以实现优先级队列 使用堆或各种其他方法,例如无序数组

因此,heap只是优先级队列抽象数据类型(ADT)的一种实现,它可以在下面列出的许多其他类型中实现,但可能不限于:

  • 无序数组
  • 无序列表
  • 有序数组
  • 有序列表
  • 二叉搜索树(BST)
  • 平衡二叉搜索树(AVL树)
  • 二进制堆(由OP请求)
将优先级队列ADT中主要操作的堆实现的时间效率与其他可能的实现进行比较:

----------------------------------------------------------------------------
| 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

我给你们看的是维基百科上的完整一行,而你们只引用了让你们困惑的那一部分。如果你们只看下一部分,也许你们会更好地理解它

堆是一种称为优先级队列的抽象数据类型的最高效率实现,事实上,优先级队列通常被称为“堆”,不管它们是如何实现的。堆的一个常见实现是二进制堆,其中树是二进制树

这里写着

事实上,优先级队列通常被称为“堆”,不管它们是如何实现的

由于堆数据结构(DS)的流行,在实现优先级队列ADT时,优先级队列ADT通常被称为堆

在维基百科的下一行引用

堆的一个常见实现是二进制堆,其中树是二叉树

这里第一堆表示优先级队列,二进制堆表示短堆表示数据结构

因此,当您看到heap(在ADT上下文中)时,它实际上意味着优先级队列ADT,heap是它的一个实现。因此heap是DS

此外,您还引用了以下内容:

“heap是一种专门的基于树的数据结构”

它只是意味着它是一个DS而不是ADT。

1)Java软件结构,国际版[John Lewis,Joseph Chase]

抽象数据类型(ADT)是一种数据类型,其值和操作 不是在编程语言中固有地定义的。它是 抽象只是因为它的实现细节必须 已定义,并且应该对用户隐藏。因此,, 是一种抽象数据类型

2)算法,罗伯特·塞吉威克和凯文·韦恩第四版

使用抽象数据类型,您不需要知道如何实现数据类型就可以使用它

因此,如果您只是在设计这样的行为:

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,因为您可以实现
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();
}
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