Questions tagged [data-structures]

Questions about ways of storing data so that it can be used advantageously by algorithms.

Questions about ways of storing data so that it can be used advantageously by algorithms.

In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently. Data structures can implement one or more particular abstract data types (ADT), which are the means of specifying the contract of operations and their complexity.

2220 questions
129
votes
5 answers

What's the difference between a binary search tree and a binary heap?

These two seem very similar and have almost an identical structure. What's the difference? What are the time complexities for different operations of each?
Piper
  • 1,779
  • 2
  • 12
  • 13
112
votes
7 answers

Why is it best to use a prime number as a mod in a hashing function?

If I have a list of key values from 1 to 100 and I want to organize them in an array of 11 buckets, I've been taught to form a mod function $$ H = k \bmod \ 11$$ Now all the values will be placed one after another in 9 rows. For example, in the…
CodyBugstein
  • 3,017
  • 11
  • 31
  • 46
102
votes
5 answers

What are the reasons to learn different algorithms / data structures serving the same purpose?

I have been wondering about this question since I was an undergraduate student. It is a general question but I will elaborate with examples below. I have seen a lot of algorithms - for example, for maximum flow problems, I know around 3 algorithms…
shole
  • 1,210
  • 1
  • 10
  • 10
75
votes
4 answers

(When) is hash table lookup O(1)?

It is often said that hash table lookup operates in constant time: you compute the hash value, which gives you an index for an array lookup. Yet this ignores collisions; in the worst case, every item happens to land in the same bucket and the lookup…
64
votes
4 answers

Why are Red-Black trees so popular?

It seems that everywhere I look, data structures are being implemented using red-black trees (std::set in C++, SortedDictionary in C#, etc.) Having just covered (a,b), red-black & AVL trees in my algorithms class, here's what I got out (also from…
50
votes
9 answers

Does there exist a priority queue with $O(1)$ extracts?

There are a great many data structures that implement the priority-queue interface: Insert: insert an element into the structure Get-Min: return the smallest element in the structure Extract-Min: remove the smallest element in the structure Common…
Alex ten Brink
  • 9,206
  • 3
  • 36
  • 63
46
votes
2 answers

What is the difference between radix trees and Patricia tries?

I am learning about radix trees (aka compressed tries) and Patricia tries, but I am finding conflicting information on whether or not they are actually the same. A radix tree can be obtained from a normal (uncompressed) trie by merging nodes with…
w128
  • 563
  • 1
  • 4
  • 9
43
votes
2 answers

Efficient data structures for building a fast spell checker

I'm trying to write a spell-checker which should work with a pretty large dictionary. I really want an efficient way to index my dictionary data to be used using a Damerau-Levenshtein distance to determine which words are closest to the misspelled…
Charles Menguy
  • 1,193
  • 1
  • 10
  • 12
42
votes
1 answer

Imagine a red-black tree. Is there always a sequence of insertions and deletions that creates it?

Let's assume the following definition of a red-black tree: It is a binary search tree. Each node is colored either red or black. The root is black. Two nodes connected by an edge cannot be red at the same time. Here should be a good definition of a…
alisianoi
  • 419
  • 3
  • 10
39
votes
2 answers

Hash tables versus binary trees

When implementing a dictionary ('I want to look up customer data by their customer IDs'), the typical data structures used are hash tables and binary search trees. I know for instance that the C++ STL library implements dictionaries (they call them…
Alex ten Brink
  • 9,206
  • 3
  • 36
  • 63
35
votes
11 answers

Why is data in computer science considered to be discrete?

I understand that "structure" of data is totally dependent on Boolean Algebra, but: Why is data considered to be a discrete mathematical entity rather than a continuous one? Related to this: What are the drawbacks, or invariants, that are…
evil_potato
  • 1,372
  • 2
  • 12
  • 11
34
votes
5 answers

Is there an anti-Bloom filter?

A Bloom filter makes it possible to efficiently keep track of whether various values have already been encountered during processing. When there are many data items then a Bloom filter can result in a significant memory saving over a hash table. …
32
votes
3 answers

Will hardware/implementation affect the time/space complexity of algorithms?

I’m not even a CS student, so this might be a stupid question, but please bear with me... In the pre-computer era, we can only implement an array data structure with something like an array of drawers. Since one have to locate the drawer with…
30
votes
2 answers

Not all Red-Black trees are balanced?

Intuitively, "balanced trees" should be trees where left and right sub-trees at each node must have "approximately the same" number of nodes. Of course, when we talk about red-black trees*(see definition at the end) being balanced, we actually mean…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
29
votes
3 answers

Retrieving the shortest path of a dynamic graph

I'm studying shortest paths in directed graphs currently. There are many efficient algorithms for finding the shortest path in a network, like dijkstra's or bellman-ford's. But what if the graph is dynamic? By saying dynamic I mean that we can…
1
2 3
99 100