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
217
votes
13 answers

What would a "frozen dict" be?

A frozen set is a frozenset. A frozen list could be a tuple. What would a frozen dict be? An immutable, hashable dict. I guess it could be something like collections.namedtuple, but that is more like a frozen-keys dict (a half-frozen dict).…
dugres
  • 12,613
  • 8
  • 46
  • 51
217
votes
34 answers

Array versus linked-list

Why would someone want to use a linked-list over an array? Coding a linked-list is, no doubt, a bit more work than using an array and one may wonder what would justify the additional effort. I think insertion of new elements is trivial in a…
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
215
votes
4 answers

Why do we use arrays instead of other data structures?

As I was programming, I haven't seen an instance where an array is better for storing information than another form thereof. I had indeed figured the added "features" in programming languages had improved upon this and by that replaced them. I see…
Xesaniel
  • 62
  • 3
  • 6
  • 14
215
votes
6 answers

How are multi-dimensional arrays formatted in memory?

In C, I know I can dynamically allocate a two-dimensional array on the heap using the following code: int** someNumbers = malloc(arrayRows*sizeof(int*)); for (i = 0; i < arrayRows; i++) { someNumbers[i] =…
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
214
votes
23 answers

How to print binary tree diagram in Java?

How can I print a binary tree in Java so that the output is like: 4 / \ 2 5 My node: public class Node { Node left, right; A data; public Node(A data){ this.data = data; } }
Tian
  • 2,453
  • 5
  • 18
  • 10
212
votes
6 answers

How to merge YAML arrays?

I would like to merge arrays in YAML, and load them via ruby - some_stuff: &some_stuff - a - b - c combined_stuff: <<: *some_stuff - d - e - f I'd like to have the combined array as [a,b,c,d,e,f] I receive the error: did not find…
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
209
votes
12 answers

How do I clear the std::queue efficiently?

I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner). In one scenario, I want to clear the queue in one shot( delete all jobs from the queue). I don't see any clear method available in…
aJ.
  • 34,624
  • 22
  • 86
  • 128
209
votes
13 answers

Are duplicate keys allowed in the definition of binary search trees?

I'm trying to find the definition of a binary search tree and I keep finding different definitions everywhere. Some say that for any given subtree the left child key is less than or equal to the root. Some say that for any given subtree the right…
Tim Merrifield
  • 6,088
  • 5
  • 31
  • 33
204
votes
3 answers

How does the HyperLogLog algorithm work?

I've been learning about different algorithms in my spare time recently, and one that I came across which appears to be very interesting is called the HyperLogLog algorithm - which estimates how many unique items are in a list. This was particularly…
K2xL
  • 9,730
  • 18
  • 64
  • 101
202
votes
14 answers

Why would anyone use set instead of unordered_set?

C++0x is introducing unordered_set which is available in boost and many other places. What I understand is that unordered_set is hash table with O(1) lookup complexity. On the other hand, set is nothing but a tree with log(n) lookup complexity. Why…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
194
votes
19 answers

Test whether a list contains a specific value in Clojure

What is the best way to test whether a list contains a given value in Clojure? In particular, the behaviour of contains? is currently confusing me: (contains? '(100 101 102) 101) => false I could obviously write a simple function to traverse the…
mikera
  • 105,238
  • 25
  • 256
  • 415
189
votes
12 answers

What's the difference between the data structure Tree and Graph?

Academically speaking, what's the essential difference between the data structure Tree and Graph? And how about the tree based search and Graph based search?
user918304
  • 2,105
  • 3
  • 15
  • 9
188
votes
4 answers

Get "Value" property in IGrouping

I have a data structure like public DespatchGroup(DateTime despatchDate, List products); And I am trying to do... var list = new List(); foreach (var group in dc.GetDespatchedProducts().GroupBy(i => i.DespatchDate)) { …
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
188
votes
8 answers

HashMap get/put complexity

We are used to saying that HashMap get/put operations are O(1). However it depends on the hash implementation. The default object hash is actually the internal address in the JVM heap. Are we sure it is good enough to claim that the get/put are…
Michael
  • 41,026
  • 70
  • 193
  • 341
186
votes
27 answers

How to implement a Map with multiple keys?

I need a data structure which behaves like a Map, but uses multiple (differently-typed) keys to access its values. (Let's not be too general, let's say two keys) Keys are guaranteed to be unique. Something like: MyMap ... With methods…
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100