Subsections


1.2 Interfaces

When discussing data structures, it is important to understand the difference between a data structure's interface and its implementation. An interface describes what a data structure does, while an implementation describes how the data structure does it.

An interface, sometimes also called an abstract data type, defines the set of operations supported by a data structure and the semantics, or meaning, of those operations. An interface tells us nothing about how the data structure implements these operations; it only provides a list of supported operations along with specifications about what types of arguments each operation accepts and the value returned by each operation.

A data structure implementation, on the other hand, includes the internal representation of the data structure as well as the definitions of the algorithms that implement the operations supported by the data structure. Thus, there can be many implementations of a single interface. For example, in Chapter 2, we will see implementations of the List interface using arrays and in Chapter 3 we will see implementations of the List interface using pointer-based data structures. Each implements the same interface, List, but in different ways.

1.2.1 The Queue, Stack, and Deque Interfaces

The Queue interface represents a collection of elements to which we can add elements and remove the next element. More precisely, the operations supported by the Queue interface are

Notice that the $ \ensuremath{\mathrm{remove}()}$ operation takes no argument. The Queue's queueing discipline decides which element should be removed. There are many possible queueing disciplines, the most common of which include FIFO, priority, and LIFO.

A FIFO (first-in-first-out) Queue, which is illustrated in Figure 1.1, removes items in the same order they were added, much in the same way a queue (or line-up) works when checking out at a cash register in a grocery store. This is the most common kind of Queue so the qualifier FIFO is often omitted. In other texts, the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ operations on a FIFO Queue are often called $ \ensuremath{\mathrm{enqueue}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{dequeue}()}$, respectively.

Figure 1.1: A FIFO Queue.
\includegraphics[width=\textwidth ]{figs-python/queue}

A priority Queue, illustrated in Figure 1.2, always removes the smallest element from the Queue, breaking ties arbitrarily. This is similar to the way in which patients are triaged in a hospital emergency room. As patients arrive they are evaluated and then placed in a waiting room. When a doctor becomes available he or she first treats the patient with the most life-threatening condition. The $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation on a priority Queue is usually called $ \ensuremath{\mathrm{delete\_min}()}$ in other texts.

Figure 1.2: A priority Queue.
\includegraphics[width=\textwidth ]{figs-python/prioqueue}

A very common queueing discipline is the LIFO (last-in-first-out) discipline, illustrated in Figure 1.3. In a LIFO Queue, the most recently added element is the next one removed. This is best visualized in terms of a stack of plates; plates are placed on the top of the stack and also removed from the top of the stack. This structure is so common that it gets its own name: Stack. Often, when discussing a Stack, the names of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ are changed to $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{pop}()}$; this is to avoid confusing the LIFO and FIFO queueing disciplines.

Figure 1.3: A stack.
\includegraphics[width=\textwidth ]{figs-python/stack}

A Deque is a generalization of both the FIFO Queue and LIFO Queue (Stack). A Deque represents a sequence of elements, with a front and a back. Elements can be added at the front of the sequence or the back of the sequence. The names of the Deque operations are self-explanatory: $ \ensuremath{\mathrm{add\_first}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove\_first}()}$, $ \ensuremath{\mathrm{add\_last}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{remove\_last}()}$. It is worth noting that a Stack can be implemented using only $ \ensuremath{\mathrm{add\_first}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove\_first}()}$ while a FIFO Queue can be implemented using $ \ensuremath{\mathrm{add\_last}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove\_first}()}$.

1.2.2 The List Interface: Linear Sequences

This book will talk very little about the FIFO Queue, Stack, or Deque interfaces. This is because these interfaces are subsumed by the List interface. A List, illustrated in Figure 1.4, represents a sequence, $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_0,\ldots,\ensuremath{\ensure...
...\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1}$, of values. The List interface includes the following operations:

  1. $ \ensuremath{\mathrm{size}()}$: return $ \ensuremath{\ensuremath{\mathit{n}}}$, the length of the list
  2. $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}})}$: return the value $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}}$
  3. $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$: set the value of $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}}$ equal to $ \ensuremath{\ensuremath{\mathit{x}}}$
  4. $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$: add $ \ensuremath{\ensuremath{\mathit{x}}}$ at position $ \ensuremath{\ensuremath{\mathit{i}}}$, displacing $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\en...
...\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1}$;
    Set $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{j+1}=\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_j$, for all $ j\in\{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\}$, increment $ \ensuremath{\ensuremath{\mathit{n}}}$, and set $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_i=\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$
  5. $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ remove the value $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}}$, displacing $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\en...
...\ensuremath{\mathit{x}}}}_{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1}$;
    Set $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{j}=\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{j+1}$, for all $ j\in\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-2\}$ and decrement $ \ensuremath{\ensuremath{\mathit{n}}}$
Notice that these operations are easily sufficient to implement the Deque interface:
$\displaystyle \ensuremath{\ensuremath{\mathrm{add\_first}(\ensuremath{\mathit{x}})}}$ $\displaystyle \Rightarrow$ $\displaystyle \ensuremath{\ensuremath{\mathrm{add}(0,\ensuremath{\mathit{x}})}}$  
$\displaystyle \ensuremath{\ensuremath{\mathrm{remove\_first}()}}$ $\displaystyle \Rightarrow$ $\displaystyle \ensuremath{\ensuremath{\mathrm{remove}(0)}}$  
$\displaystyle \ensuremath{\ensuremath{\mathrm{add\_last}(\ensuremath{\mathit{x}})}}$ $\displaystyle \Rightarrow$ $\displaystyle \ensuremath{\ensuremath{\mathrm{add}(\mathrm{size}(),x})}$  
$\displaystyle \ensuremath{\ensuremath{\mathrm{remove\_last}()}}$ $\displaystyle \Rightarrow$ $\displaystyle \ensuremath{\ensuremath{\mathrm{remove}(\mathrm{size}()-1})}$  

Figure 1.4: A List represents a sequence indexed by $ 0,1,2,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$. In this List a call to $ \ensuremath{\mathrm{get}(2)}$ would return the value $ c$.
\includegraphics[width=\textwidth ]{figs-python/list}

Although we will normally not discuss the Stack, Deque and FIFO Queue interfaces in subsequent chapters, the terms Stack and Deque are sometimes used in the names of data structures that implement the List interface. When this happens, it highlights the fact that these data structures can be used to implement the Stack or Deque interface very efficiently. For example, the ArrayDeque class is an implementation of the List interface that implements all the Deque operations in constant time per operation.

1.2.3 The USet Interface: Unordered Sets

The USet interface represents an unordered set of unique elements, which mimics a mathematical set. A USet contains $ \ensuremath{\ensuremath{\mathit{n}}}$ distinct elements; no element appears more than once; the elements are in no specific order. A USet supports the following operations:

  1. $ \ensuremath{\mathrm{size}()}$: return the number, $ \ensuremath{\ensuremath{\mathit{n}}}$, of elements in the set
  2. $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$: add the element $ \ensuremath{\ensuremath{\mathit{x}}}$ to the set if not already present;
    Add $ \ensuremath{\ensuremath{\mathit{x}}}$ to the set provided that there is no element $ \ensuremath{\ensuremath{\mathit{y}}}$ in the set such that $ \ensuremath{\ensuremath{\mathit{x}}}$ equals $ \ensuremath{\ensuremath{\mathit{y}}}$. Return $ \ensuremath{\ensuremath{\mathit{true}}}$ if $ \ensuremath{\ensuremath{\mathit{x}}}$ was added to the set and $ \ensuremath{\ensuremath{\mathit{false}}}$ otherwise.
  3. $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$: remove $ \ensuremath{\ensuremath{\mathit{x}}}$ from the set;
    Find an element $ \ensuremath{\ensuremath{\mathit{y}}}$ in the set such that $ \ensuremath{\ensuremath{\mathit{x}}}$ equals $ \ensuremath{\ensuremath{\mathit{y}}}$ and remove $ \ensuremath{\ensuremath{\mathit{y}}}$. Return $ \ensuremath{\ensuremath{\mathit{y}}}$, or $ \ensuremath{\ensuremath{\mathit{nil}}}$ if no such element exists.
  4. $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$: find $ \ensuremath{\ensuremath{\mathit{x}}}$ in the set if it exists;
    Find an element $ \ensuremath{\ensuremath{\mathit{y}}}$ in the set such that $ \ensuremath{\ensuremath{\mathit{y}}}$ equals $ \ensuremath{\ensuremath{\mathit{x}}}$. Return $ \ensuremath{\ensuremath{\mathit{y}}}$, or $ \ensuremath{\ensuremath{\mathit{nil}}}$ if no such element exists.

These definitions are a bit fussy about distinguishing $ \ensuremath{\ensuremath{\mathit{x}}}$, the element we are removing or finding, from $ \ensuremath{\ensuremath{\mathit{y}}}$, the element we may remove or find. This is because $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{y}}}$ might actually be distinct objects that are nevertheless treated as equal. Such a distinction is useful because it allows for the creation of dictionaries or maps that map keys onto values.

To create a dictionary/map, one forms compound objects called Pairs, each of which contains a key and a value. Two Pairs are treated as equal if their keys are equal. If we store some pair $ (\ensuremath{\ensuremath{\ensuremath{\mathit{k}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{v}}}})$ in a USet and then later call the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ method using the pair $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}=(\ensuremath{\ensuremath{\ensuremath{\mathit{k}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{nil}}}})$ the result will be $ \ensuremath{\ensuremath{\ensuremath{\mathit{y}}}}=(\ensuremath{\ensuremath{\ensuremath{\mathit{k}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{v}}}})$. In other words, it is possible to recover the value, $ \ensuremath{\ensuremath{\mathit{v}}}$, given only the key, $ \ensuremath{\ensuremath{\mathit{k}}}$.


1.2.4 The SSet Interface: Sorted Sets

The SSet interface represents a sorted set of elements. An SSet stores elements from some total order, so that any two elements $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{y}}}$ can be compared. In code examples, this will be done with a method called $ \ensuremath{\mathrm{compare}(\ensuremath{\mathit{x}},\ensuremath{\mathit{y}})}$ in which

$\displaystyle \ensuremath{\ensuremath{\mathrm{compare}(\ensuremath{\mathit{x}},...
...{\mathit{x}}}}=\ensuremath{\ensuremath{\ensuremath{\mathit{y}}}}$}
\end{cases}$

An SSet supports the $ \ensuremath{\mathrm{size}()}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ methods with exactly the same semantics as in the USet interface. The difference between a USet and an SSet is in the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ method:
  1. $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$: locate $ \ensuremath{\ensuremath{\mathit{x}}}$ in the sorted set;
    Find the smallest element $ \ensuremath{\ensuremath{\mathit{y}}}$ in the set such that $ \ensuremath{\ensuremath{\ensuremath{\mathit{y}}}} \ge \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$. Return $ \ensuremath{\ensuremath{\mathit{y}}}$ or $ \ensuremath{\ensuremath{\mathit{nil}}}$ if no such element exists.

This version of the $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation is sometimes referred to as a successor search. It differs in a fundamental way from $ \ensuremath{\mathrm{USet}.\mathrm{find}(\ensuremath{\mathit{x}})}$ since it returns a meaningful result even when there is no element equal to $ \ensuremath{\ensuremath{\mathit{x}}}$ in the set.

The distinction between the USet and SSet $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operations is very important and often missed. The extra functionality provided by an SSet usually comes with a price that includes both a larger running time and a higher implementation complexity. For example, most of the SSet implementations discussed in this book all have $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operations with running times that are logarithmic in the size of the set. On the other hand, the implementation of a USet as a ChainedHashTable in Chapter 5 has a $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation that runs in constant expected time. When choosing which of these structures to use, one should always use a USet unless the extra functionality offered by an SSet is truly needed.

opendatastructures.org