Data Structures

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

A data structure is an arrangement of data in computer's memory or external storage, designed to facilitate a specific way to access or modify the data, quickly and usefully. Data structures include arrays, linked lists, stacks, queues, binary trees, search trees, heaps, hash tables, bloom filters, union-find, etc.

Data structures are crucial ingredients in the design of fast algorithms. A data structure is responsible for organizing data in a way that supports fast queries. Different data structures support different types of operations and are suited for different kinds of tasks.

Data Structures as Sets

All data structures used to represent data in the memory of a computer are essentially sets. Unlike a mathematical set, the sets manipulated by computers are finite, and their composition change in time, so they are called dynamic sets. We know from mathematics that the elements of a set are distinct. Data structure sets also consist of distinct elements, if we conceptualize the fact that the elements are maintained in distinct memory locations. In a typical implementation of a dynamic set, each element is represented by an object (memory location) whose attributes can be examined and manipulated if we have a pointer or a reference to the objects. Different objects have different references.

Some dynamic sets assume that one of the element's attributes is an identifying key. If the keys are all different, we can think of the dynamic set as being a set of key values. Some dynamic sets assume that the keys are drawn from a totally ordered set, such as the real numbers, or the set of all words under the usual alphabetic ordering. A total ordering allows defining a minimum element of the set, or to speak of the next element larger than the given element.

Dictionary

A dynamic set that allows insertion of new elements, deletion of existing elements and membership testing (or search) for a specific element is called a dictionary. Insertion and deletion modify the set, and they are called modifying operations. Search does not modify the set and it belongs to the query category of operations.

Modifying Dictionary Operations

Assuming that X is a pointer, or a reference, not a key, a dictionary supports the following modifying operations:

  • INSERT(X) - adds the element pointed by X to the set.
  • DELETE(X) - removes the element pointed by X.

Queries

  • SEARCH(K) - a query that given a key value K, returns a pointer to an element with the given key, or NULL if there is no such element.

Queries on Totally Ordered Sets

If the set is totally ordered, the following operations may be exposed:

  • MINIMUM() - returns the pointer to the element with the smallest key.
  • MAXIMUM() - returns the pointer to the element with the largest key.
  • PREDECESSOR(X) - a query that, given an element X whose key is from a totally ordered set, returns a pointer to the next smaller element, or NULL if X is the minimum element.
  • SUCCESSOR(X) - a query that, given an element X whose key is from a totally ordered set, returns a pointer to the next larger element, or NULL if X is the maximum element.

A totally ordered set, also known as a linearly ordered set, is formally defined here.

We usually measure the time taken the execute an operation in terms of the size of the set n. Asymptotic notation is used to describe the behavior of these operations, as a function of the set size.

Data Structure Characteristics

A data structure can be described based on several characteristics:

  • space requirements.
  • what operations the data structure supports.
  • what is the running time of those operations.

In general, fewer operations a data structure supports, the faster the operations and smallest the space overhead required by the data structure will be.

Arrays

An array is a fundamental data structure in computer science. Arrays are how data is physically stored in memory. An array consists in a contiguous set of memory locations. The elements are maintained in linear order, which is determined by the array index. Given the pointer to the first element of the array, accessing any element is a Θ(1) operation that consists in adding the index multiplied with the element size to the first element pointer to obtain the pointer of the desired element.

In Java, fast copies for primitive arrays with System.arraycopy(source, sourcePosition, destination, destinationPosition, count).

Static Sorted Arrays

Static Sorted Arrays

Dynamic Sets

Graphs

Graphs

Trees

Trees

Heaps

Heaps

Balanced Binary Search Trees

Balanced Binary Search Trees

Other Data Structures


[Next]