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
462
votes
34 answers

Split List into Sublists with LINQ

Is there any way I can separate a List into several separate lists of SomeObject, using the item index as the delimiter of each split? Let me exemplify: I have a List and I need a List> or List[],…
Felipe Lima
  • 10,530
  • 4
  • 41
  • 39
434
votes
15 answers

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement...
Roddy
  • 66,617
  • 42
  • 165
  • 277
427
votes
19 answers

What do I use for a max-heap implementation in Python?

Python includes the heapq module for min-heaps, but I need a max-heap. What should I use for a max-heap implementation in Python?
Douglas Mayle
  • 21,063
  • 9
  • 42
  • 57
422
votes
17 answers

Why should hash functions use a prime number modulus?

A long time ago, I bought a data structures book off the bargain table for $1.25. In it, the explanation for a hashing function said that it should ultimately mod by a prime number because of "the nature of math". What do you expect from a $1.25…
theschmitzer
  • 12,190
  • 11
  • 41
  • 49
418
votes
4 answers

How are Python's Built In Dictionaries Implemented?

Does anyone know how the built in dictionary type for python is implemented? My understanding is that it is some sort of hash table, but I haven't been able to find any sort of definitive answer.
ricree
  • 35,626
  • 13
  • 36
  • 27
416
votes
17 answers

JavaScript hashmap equivalent

As made clear in update 3 on this answer, this notation: var hash = {}; hash[X] does not actually hash the object X; it actually just converts X to a string (via .toString() if it's an object, or some other built-in conversions for various…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
412
votes
7 answers

Preserving order with LINQ

I use LINQ to Objects instructions on an ordered array. Which operations shouldn't I do to be sure the order of the array is not changed?
Matthieu Durut
  • 4,838
  • 3
  • 20
  • 16
406
votes
13 answers

What is the difference between tree depth and height?

This is a simple question from algorithms theory. The difference between them is that in one case you count number of nodes and in other number of edges on the shortest path between root and concrete node. Which is which?
Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
365
votes
14 answers

Difference between binary tree and binary search tree

Can anyone please explain the difference between binary tree and binary search tree with an example?
Neel
  • 9,913
  • 16
  • 52
  • 74
348
votes
14 answers

What are the differences between B trees and B+ trees?

In a b-tree you can store both keys and data in the internal and leaf nodes, but in a b+ tree you have to store the data in the leaf nodes only. Is there any advantage of doing the above in a b+ tree? Why not use b-trees instead of b+ trees…
simplfuzz
  • 12,479
  • 24
  • 84
  • 137
345
votes
13 answers

How to correctly use lists?

Brief background: Many (most?) contemporary programming languages in widespread use have at least a handful of ADTs [abstract data types] in common, in particular, string (a sequence comprised of characters) list (an ordered collection of values),…
doug
  • 69,080
  • 24
  • 165
  • 199
344
votes
3 answers

What are the underlying data structures used for Redis?

I'm trying to answer two questions in a definitive list: What are the underlying data structures used for Redis? And what are the main advantages/disadvantages/use cases for each type? So, I've read the Redis lists are actually implemented with…
Homer6
  • 15,034
  • 11
  • 61
  • 81
338
votes
14 answers

What is C# analog of C++ std::pair?

I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you!
Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
337
votes
13 answers

In Python, when to use a Dictionary, List or Set?

When should I use a dictionary, list or set? Are there scenarios that are more suited for each data type?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
333
votes
19 answers

How can I implement a tree in Python?

How can I implement a general tree in Python? Is there a built-in data structure for this?
vishnu
  • 3,985
  • 4
  • 19
  • 6