Questions tagged [data-structures]

A data structure is a way of organizing data in a fashion that allows particular properties of that data to be queried and/or updated efficiently.

Data structures are ubiquitous in software. Hash tables, balanced trees, and dynamic arrays are essential building blocks for large, complicated systems and almost all programmers have encountered them at some point. More complicated structures like binary heaps can speed up complicated systems, while simpler concepts like stacks and queues make it possible to more elegantly and concisely encode algorithms.

Of course, this is just the tip of the iceberg when it comes to data structures. Theoreticians have been developing progressively better and better data structures over the past years, many of which are used extensively in modern software, and many are only useful from a theoretical perspective.

Data structures are closely related to algorithms. Often, a good choice of data structure can lead to a marked improvement in an algorithm's runtime. For example, Dijkstra's algorithm with a naive implementation of a priority queue runs in quadratic time, but with a fast Fibonacci heap can be shown to run in O(m + n lg n).

Below is a (not at all comprehensive) list of some of the more popular data structures and their variants:

  • Dynamic arrays
    • Dynamic table
    • Tiered vector
    • Hashed array tree
    • Extendible array
  • Linked lists
    • Singly-linked lists
    • Doubly-linked lists
    • XOR-linked lists
    • Skip lists
  • Hash tables
    • Chained hash tables
    • Linear probing hash tables
    • Quadratic probing hash tables
    • Double hash tables
    • Cuckoo hash tables
    • Perfect hash tables
    • Dynamic perfect hash tables
  • Binary trees
    • Red/black trees
    • Interval trees
    • Binary search trees
    • Segment trees
    • AVL trees
    • AA trees
    • Splay trees
    • Scapegoat trees
    • Treap
  • Priority queues
    • Binary heap
    • Binomial heap
    • Fibonacci heap
    • van Emde Boas tree
    • Skew binomial heap
    • Brodal queue
  • Radix trees
    • Trie
    • Patricia tree
    • Ternary search tree
  • Multiway trees
    • B tree
    • B+ tree
    • B* tree
  • Geometric trees
    • Quadtree
    • Octree
    • kd-Tree
    • BSP-tree
    • R-tree
  • Geometric structures
    • Winged-edge
    • Quadedge
  • Network connectivity structures
    • Disjoint-set forest
    • Link/cut tree
    • Top tree
  • Graphs
    • DAG
    • Directed graph
    • Undirected graph
    • Hypergraph

Links:

33477 questions
11
votes
4 answers

What is the difference between a "container" and a "data structure"?

What is a container? As I understand it: An abstract data type is merely a logical description of the way the data will be stored and the operations that will be permitted on that data. For example, a stack is defined as a data type with the…
ordinary
  • 5,943
  • 14
  • 43
  • 60
11
votes
1 answer

Intelligent purely functional sets

Set computations composed of unions, intersections and differences can often be expressed in many different ways. Are there any theories or concrete implementations that try to minimize the amount of computation required to reach a given answer? For…
J D
  • 48,105
  • 13
  • 171
  • 274
11
votes
1 answer

How to populate a Clojure record from a map?

Is there something like struct-map for records? If not, should I use a struct (the docs discourage use of structs)? Maybe I am doing the wrong thing completely? I have a rather complex function which currently takes a map of options. I am trying…
andrew cooke
  • 45,717
  • 10
  • 93
  • 143
11
votes
2 answers

CouchDB - hierarchical comments with ranking. Hacker News style

I'm trying to implement a basic way of displaying comments in the way that Hacker News provides, using CouchDB. Not only ordered hierarchically, but also, each level of the tree should be ordered by a "points" variable. The idea is that I want a…
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
11
votes
2 answers

Finding a ray that intersects a polygon as many times as possible

Here is an interesting exercise: Let P be a simple, but not necessarily convex, polygon and q an arbitrary point not necessarily in P. Design an efficient algorithm to find a line segment originating from q that intersects the maximum number of…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
11
votes
8 answers

Is it possible to calculate the Chinese Zodiac, or must I use a lookup table?

I'm building an application that will tell your Chinese sign. I looked around but only found charts (from 1900 to 2020), and no logic to create something more dynamic. Is there no logic for determining a Chinese zodiac?
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
11
votes
5 answers

ArrayList with returned index on add value

I am looking for a java data structure similar to an ArrayList that when I do an add or a push with only a value argument an index will be returned for me automatically. For example: ArrayList elements = new ArrayList(); String…
Ryan R.
  • 2,478
  • 5
  • 27
  • 48
11
votes
3 answers

Efficient implementation of immutable (double) LinkedList

Having read this question Immutable or not immutable? and reading answers to my previous questions on immutability, I am still a bit puzzled about efficient implementation of simple LinkedList that is immutable. In terms of array tha seems to be…
Bober02
  • 15,034
  • 31
  • 92
  • 178
10
votes
3 answers

Efficient Data Structure For Substring Search?

Assume I have a set of strings S and a query string q. I want to know if any member of S is a substring of q. (For the purpose of this question substring includes equality, e.g. "foo" is a substring of "foo".) For example assume the function that…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
10
votes
2 answers

Data structure for efficiently retrieving the nearest element from a set

tl;dr How can something like Mathematica's Nearest be implemented efficiently? Mathematica has a function called Nearest which will take a list of "things" (they can be numbers, coordinates in n-dimensional space, strings, etc.), and will return a…
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
10
votes
3 answers

Preferred way to compare a structure to zero

Today I came across a situation where I needed to decide if an entire structure that consists of about 40 elements is zero - meaning that each of the elements is zero. When thinking how to make it as fast and efficient as possible, I thought of 3…
stdcall
  • 27,613
  • 18
  • 81
  • 125
10
votes
2 answers

How to check/find if an item is in a DEQUE

In the code above the else-if part gives me error. The meaning of else-if is: else if the value of x isn't in the deque then... #include #include #include #include #include deque visited; char…
george mano
  • 5,948
  • 6
  • 33
  • 43
10
votes
1 answer

How do you partition an array into 2 parts such that the two parts have equal average?

How do you partition an array into 2 parts such that the two parts have equal average? Each partition may contain elements that are non-contiguous in the array. The only algorithm I can think of is exponential can we do better?
CommonMan
  • 3,868
  • 2
  • 24
  • 35
10
votes
12 answers

When to use the Stack collection in C#?

I do understand how Stack() and Stack works, but I really can't see any scenarios where an array, List or IEnumerable isn't a better and easier choice. Can anyone provide me a real world example of the use of Stack?
jhovgaard
  • 560
  • 4
  • 18
10
votes
3 answers

Finding elementary intervals in overlapping intervals

I came across a good question while preparing for some programming interviews. Given a set of possibly overlapping intervals, you need to write a function to return all elementary intervals among them. For example: if you are given intervals as the…
Kowshik
  • 1,541
  • 3
  • 17
  • 25