computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, peek is an operation on certain
abstract data type
In computer science, an abstract data type (ADT) is a mathematical model for data types, 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 ...
s, specifically sequential collections such as stacks and queues, which returns the value of the top ("front") of the collection without removing the element from the collection. It thus returns the same value as operations such as "pop" or "dequeue", but does not modify the data.
The name "peek" is similar to the basic "push" and "pop" operations on a stack, but the name for this operation varies depending on data type and language. Peek is generally considered an inessential operation, compared with the more basic operations of adding and removing data, and as such is not included in the basic definition of these data types. However, since it is a useful operation and generally easily implemented, it is frequently included in practices, and in some definitions peek is included as basic, with pop (or analog) defined in terms of peek; see
abstract definition
Abstract may refer to:
*"Abstract", a 2017 episode of the animated television series ''Adventure Time''
* ''Abstract'' (album), 1962 album by Joe Harriott
* Abstract algebra, sets with specific operations acting on their elements
* Abstract of ti ...
.
Data types
Sequential types for which peek is often implemented include:
*
Stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
Priority queue
In computer science, a priority queue is an abstract data type similar to a regular queue (abstract data type), queue or stack (abstract data type), stack abstract data type.
In a priority queue, each element has an associated ''priority'', which ...
Double-ended queue
In computer science, a double-ended queue (abbreviated to deque, ) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail). It is also often called a head-t ...
(deque)
*
Double-ended priority queue
In computer science, a double-ended priority queue (DEPQ)Operations on deques have varied names, often "front" and "back" or "first" and "last". The name "peak" is also occasionally found, in the sense of "top, summit", though this also occurs as a spelling error for the verb "peek".
Abstract definition
Intuitively, peek returns the same value as pop, but does not change the data. Behavior when the collection is empty varies – most often this yields an underflow error, identically to a pop on an empty collection, but some implementations provide a function which instead simply returns (without error), essentially implementing if isempty then return, else peek.
This behavior can be axiomatized in various ways. For example, a common VDM ('' Vienna Development Method'') description of a stack defines ''top'' (peek) and ''remove'' as atomic, where ''top'' returns the top value (without modifying the stack), and ''remove'' modifies the stack (without returning a value).Jones: "Systematic Software Development Using VDM" In this case ''pop'' is defined in terms of ''top'' and ''remove.''
Alternatively, given ''pop,'' the operation ''peek'' can be axiomatized as:
:''peek''(''D'') = ''pop''(''D'')
:''peek''(''D''), ''D'' = ''D''
meaning "returns the same value as ''pop''", and "does not change the underlying data" (value of data after peek same as before peek).
Implementation
Peek can generally be implemented very easily in simple routine taking ''O''(1) time and no added space, by a simple variant of the pop operation. Most sequential data types are implemented by a data structure containing a
reference
A reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''nam ...
to the end, and thus peek is simply implemented by dereferencing this. In some cases it is more complicated, however.
For some data types, such as stacks, this can be replicated in terms of more basic operations, but for other data types, such as queues, it cannot. Even if peek can be replicated in terms of other operations, it is almost always more efficient to implement it separately, as this avoids modifying the data, and it is easy to implement, as this simply consists of returning the same value as the "pop" (or analogous operation), but then not modifying the data.
For the stack, priority queue, deque, and DEPQ types, peek can be implemented in terms of pop and push (if done at same end). For stacks and deques this is generally efficient, as these operations are ''O''(1) in most implementations, and do not require memory allocation (as they decrease the size of the data) – the two ends of a deque each functioning as a stack. For priority queues and DEPQs, however, dequeuing and enqueuing often take ''O''(log ''n'') time (for example if implemented as a
binary heap
A binary heap is a heap (data structure), heap data structure that takes the form of a binary tree. Binary heaps are a common way of implementing priority queues. The binary heap was introduced by J. W. J. Williams in 1964 as a data structure fo ...
), while ''O''(1) performance of "peek" (here generally called "find-min" or "find-max") is a key desired characteristic of priority queues, and thus peek is almost invariably implemented separately.
For queue, because enqueuing and dequeuing occur at opposite ends, peek cannot be implemented in terms of basic operations, and thus is often implemented separately.
One case in which peek is not trivial is in an ordered list type (i.e., elements accessible in order) implemented by a
self-balancing binary search tree
In computer science, a self-balancing binary search tree (BST) is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.Donald ...
. In this case find-min or find-max take ''O''(log ''n'') time, as does access to any other element. Making find-min or find-max take ''O''(1) time can be done by caching the min or max values, but this adds overhead to the data structure and to the operations of adding or removing elements.
References
* Horowitz, Ellis: "Fundamentals of Data Structures in Pascal", Computer Science Press, 1984, p. 67
{{Data structures
Abstract data types