In computer programming, a data structure is a predefined format for efficiently storing, accessing, and processing data in a computer program. Some data structures are a programming language built-in component, and others may require the inclusion of a library or module before the structure can be used.
The decision of what data structures to use and how they’re used is one of the most important steps in designing and writing a computer program.
Primitive data structures
The following are primitive data structures, also known as data types. Each of the following primitives can hold a single value, such as 1, 3.14, or the lowercase letter a.
“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds
Primitive data structures
Boolean
Character
Integer
Floating-point
Double-precision floating-point
Pointer
Fixed-point
Compound data structures
Linear structures
Array
Associative array
List
Queue
Stack
String
Tuple
Tree structures
Abstract syntax tree
Binary tree
B-tree
BSP tree
Heap
Merkle tree
R-tree
Hash structures
Bloom filter
Hash table
Rolling hash
Trie
Graph structures
Adjacency list
Adjacency matrix
Graph-structured stack
Hypergraph
Scene graph
Related information
Boolean
Character
Integer
Floating-point
Double-precision floating-point
Pointer
Fixed-point
Linear structures
Array
Associative array
List
Queue
Stack
String
Tuple
Tree structures
Abstract syntax tree
Binary tree
B-tree
BSP tree
Heap
Merkle tree
R-tree
Hash structures
Bloom filter
Hash table
Rolling hash
Trie
Graph structures
Adjacency list
Adjacency matrix
Graph-structured stack
Hypergraph
Scene graph
Array
Associative array
List
Queue
Stack
String
Tuple
Abstract syntax tree
Binary tree
B-tree
BSP tree
Heap
Merkle tree
R-tree
Bloom filter
Hash table
Rolling hash
Trie
Adjacency list
Adjacency matrix
Graph-structured stack
Hypergraph
Scene graph
Compound data structures
Compound data structures can contain multiple elements. These elements are usually values of a primitive data type, such as an integer or character. Some compound structures are restricted to a single data type, while others permit a mixture of data types. Some can contain data structures as elements.
Each of the following compound structures has its own benefits and use cases.
Linear data structures
A linear data structure is a compound data structure whose elements are arranged in a logical sequence. In these structures, every element is followed by exactly one other element, unless it is the last one.
Tree structures
A tree is a compound structure whose elements are arranged in a parent-child hierarchy, similar to branches on a tree. The elements are called nodes. A tree contains a single root node, which has no parents. Traversal usually occurs only in one direction, from the node to one of its children (called “descendents”). Then, traversal continues recursively, to one of the children of that node, until the desired node is reached. If a node has no children, it is called a leaf node.
If a tree is balanced, all of its branches are same “depth” — all leaf nodes are the same distance from the root node. If a tree is unbalanced, there’s no restriction of how long a branch can be in relation to the others. Unbalanced trees are simpler to implement, but may be less efficient in worst-case scenarios.
Hash structures
In computer science, a hash function is a function that produces a fixed-length number, often represented in hexadecimal, that can be calculated from a given set of input data. Regardless of the input size, as long as the input has not changed, it always produces the same fixed-length hexadecimal number. These types of functions have important uses in computer science, because they can validate or index data, even at a massive scale.
The following are examples of data structures designed to store the results of hash functions, many being special cases of structures described in the sections above.
Graph structures
A graph is a data structure that organizes data according to the relationships of its elements in a geometric space. The elements are usually vertices (points in the graph) and edges (the connections between vertices).
If the vertices in a graph can be traversed in only one direction (A→B, but not B→A), it is called a directed graph. If vertices can be traversed in either direction, it is called an undirected graph. If it’s possible to traverse edges and return at the starting vertex, it is called a cyclic graph. If such traversal is not possible, it is called an acyclic graph.
The following are examples of data structures used to efficiently store and process graphs.
Computer acronyms, Computer science, Programming terms
Related information
- List of computer programming languages.
- How to create a computer program.