Ask HN: What are some cool but obscure data structures you know about?
Here&#x27;s a few fun ones: the Burrows–Wheeler transform, suffix arrays in general, Kanerva&#x27;s fully distributed representation, reduced-affine-arithmetic numbers, LSM trees, binary array sets, sparse array sets, and gap buffers.  (I&#x27;m not sure how nobody had mentioned gap buffers yet on the thread, but if they did I didn&#x27;t see it.)<p>— ⁂ —<p>The Burrows-Wheeler transform is the <i>second</i> character of each suffix in a (cyclic) suffix array, and there turns out to be a simple algorithm to recover the original text from this transform; live demo: <a href="http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;sw&#x2F;bwt" rel="nofollow">http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;sw&#x2F;bwt</a>.  But the new text is very easily compressible, which is the basis for bzip2.  As mentioned by Blahah and dahart, this is only one of many interesting things you can do with suffix arrays; they can also do efficient regular expression search, for example.  This is more interesting since three practical linear-time and -space suffix array construction algorithms were discovered in the early 02000s.<p>— ⁂ —<p>Pentti Kanerva devised a &quot;fully distributed representation&quot; in the 01990s, which has some features in common with bloom filters — every association stored in an FDR record is stored to an equal extent (probabilistically) in every bit of the record, so erasing some of the bits erases none of the associations but increases the error probability for all of them.  You assign a random or pseudorandom bitvector (of, say, 16384 bits) to every atomic value to be stored, represent an association of two (or more) atoms as their XOR, and then take the bitwise majority rule of all the associations to be stored as the record.  To retrieve an association, you XOR the record with an atom, then compute the correlation of the XOR result with all possible atoms; the correlation with the correct atoms will be many standard deviations away from the mean, unless the number of associations stored in the record is high enough that conventional data structures wouldn&#x27;t have been able to store it either.<p>This and some related work has blossomed into a field called &quot;hyperdimensional computing&quot; and &quot;vector symbolic architectures&quot;.  But the problem of finding the nearest atom or atoms seems, on its face, to make this impractical: if you have 16384 possible atoms and 16384 bits in your bitvector, you would seem to need at least 16777216 bit operations to compute all the associations.<p>I realized the other night that if you use the rows of a Walsh–Hadamard matrix as the &quot;pseudorandom&quot; atom representations, you can compute all the correlations in linearithmic time with the fast Walsh–Hadamard transform.  Moreover, Cohn and Lempel&#x27;s result from 01977 &quot;on fast M-sequence transforms&quot; is that after a simple transposition you can also use the FWHT to compute all the correlations with the complete output of a maximal LFSR (a so-called &quot;M-sequence&quot;) in linearithmic time, so you can also use the cyclic shifts of an LFSR output for your atoms without losing efficiency.  Probably someone else has already figured this out previously, but I hadn&#x27;t found it in the literature.<p>— ⁂ —<p>I&#x27;m not sure if the quantities used in reduced affine arithmetic count as a &quot;data structure&quot;?  They&#x27;re a data <i>type</i>, at least, but they&#x27;re not a <i>container</i>.  The idea is that you execute some arbitrary numerical algorithm, not merely on floating-point numbers, or even on dual numbers as in forward-mode automatic differentiation, nor even yet on (min, max) intervals as in ordinary interval arithmetic, but on a vector of coefficients [<i>a</i>₀, <i>a</i>₁, <i>a</i>₂...<i>aₙ</i>], which represents a linear function <i>a</i>₀<i>x</i>₀ + <i>a</i>₁<i>x</i>₁ + <i>a</i>₂<i>x</i>₂ + ... <i>aₙxₙ</i>, where <i>x</i>₀ = 1 and each of the other <i>xᵢ</i> ∈ [-1, 1], so this function is really a representation of an interval.  Then you can assign each of the <i>xᵢ</i> (except <i>x</i>₀ and <i>xₙ</i>) to be the unknown error in one of the input parameters to your algorithm, whose magnitude is determined by the <i>aᵢ</i> value in the value of that parameter; and when you introduce rounding error, you increase <i>aₙ</i> enough to account for it.  (The non-reduced kind of affine arithmetic just increases <i>n</i> without limit to account for new roundoff errors.)<p>Reduced affine arithmetic has two advantages over the usual kind of interval arithmetic.  First, it cancels errors correctly; if you calculate (<i>y</i>+1)(<i>y</i>+2)&#x2F;(<i>y</i>+3) with interval arithmetic, with some uncertainty in the value of <i>y</i>, you usually get an unnecessarily loose bound on the result because interval arithmetic assumes that the uncertainties in the numerator and the denominator are uncorrelated, when in fact for many values of <i>y</i> they partly cancel out.  (You can&#x27;t apply this cancelation to <i>aₙ</i>, the error stemming from all the roundoffs in your algorithm, just the other <i>aᵢ</i>.)  But the more interesting result is that RAA gives you a linear function that approximates the calculation result <i>as a linear function of the inputs</i>, which is pretty similar to calculating the gradient — but with error bounds on the approximation!<p>— ⁂ —<p>LSM trees are only about as &quot;obscure&quot; as bloom filters, since they underlie several prominent filesystems, LevelDB, and most of the world&#x27;s search engines, but if you don&#x27;t know about bloom filters, you might not know about LSM trees either.  I wrote a tutorial explanation of LSM trees in <a href="https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;500lines&#x2F;tree&#x2F;master&#x2F;search-engine" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;500lines&#x2F;tree&#x2F;master&#x2F;search-engine</a>, though the name &quot;LSM tree&quot; hadn&#x27;t yet gone mainstream.<p>Binary array sets, as described in <a href="https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;binary-array-set" rel="nofollow">https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;binary-array-set</a>, are closely related to LSM trees, to the point that you could describe them as an application of LSM trees.  They are a simple data structure for the exact version of the problem bloom filters solve approximately: maintaining a set S of items supporting the operations S.add(item) and S.contains?(item).  Insertion (.add) is amortized constant time, and membership testing (.contains?) is O(log² <i>N</i>).<p>— ⁂ —<p>A different and more efficient data structure for the exact set membership problem, limited to the case where the items to be stored are up to <i>M</i> integers in the range [0, <i>N</i>), supports <i>constant-time</i> creation, clearing, insertion, size measurement, and membership testing, and linear-time enumeration, but I think cannot be implemented in modern standard C because of its rules about reading uninitialized data.  (By allowing creation to be linear time you can implement it in standard C.)  It uses two arrays, <i>A</i>[<i>M</i>] and <i>B</i>[<i>N</i>] and a variable <i>n</i>, and <i>i</i> is a member of the set iff <i>B</i>[<i>i</i>] &lt; <i>n</i> ∧ <i>A</i>[<i>B</i>[<i>i</i>]] == <i>i</i>, from which it is straightforward to work out the other methods.<p>I don&#x27;t remember what this data structure is called, but I definitely didn&#x27;t invent it. I described it and some uses for it in <a href="https:&#x2F;&#x2F;dercuano.github.io&#x2F;notes&#x2F;constant-time-sets-for-pixel-painting.html" rel="nofollow">https:&#x2F;&#x2F;dercuano.github.io&#x2F;notes&#x2F;constant-time-sets-for-pixe...</a>.  You can associate additional values with this structure to use it as a sparse array.<p>Oh, I see kcbanner posted this one too, with a link to Russ Cox&#x27;s writeup, which is better than mine and explains that it was published by Briggs and Torczon in 01993, but probably not originally invented because it was an exercise in an Aho, Hopcroft, and Ullman textbook in 01974 and in <i>Programming Pearls</i>: <a href="https:&#x2F;&#x2F;research.swtch.com&#x2F;sparse" rel="nofollow">https:&#x2F;&#x2F;research.swtch.com&#x2F;sparse</a><p>— ⁂ —<p>A gap buffer is a data structure sort of similar to Huet&#x27;s zipper but without the dynamic allocation; Emacs famously uses them for its editing buffers.  It consists of an array containing of a leading chunk of text, followed by a gap, followed by a trailing chunk of text.  Edits are always made in the gap; to start editing in a different position, you have to move some text from the end of the leading chunk to the beginning of the trailing chunk or vice versa.<p>Suppose you want to edit a 531-byte file; you allocate a larger buffer, say 1024 bytes, and read those 531 bytes into the beginning of the buffer.  If the user starts inserting text after byte 200, you begin by moving bytes 201–530 (330 bytes) to the end of the 1024-byte buffer (positions 694–1024); then you can insert the user&#x27;s text at positions 201, 202, 203, etc., without having to do any more work to move text.  If the user moves up to byte 190 and inserts more text, you only have to move bytes 191–203 or whatever to the end of the buffer.<p>A gap buffer is very fast in the usual case of making a sequence of small edits at the same location or close-together locations, and because the constant factor of copying a large block of bytes from one place to another is so small, even moving the gap from one end of a buffer to another is generally plenty fast enough for interactive use.  Moreover, operations like string search are very fast in a gap buffer, while they tend to be very slow in fancier data structures with better asymptotic complexity, such as piece tables and ropes; and it uses a predictable and relatively small amount of extra memory.<p>— ⁂ —<p>However, like FORTH, fancy data structures are a bit of an attractive nuisance: they tempt you to get into trouble.  When I first came across Sedgewick&#x27;s <i>Algorithms in C</i> it was a revelation to me: here were so many clever ways of breaking down problems that made apparently impossible problems easy.  (I had come across sorting algorithms before but I didn&#x27;t really understand them.)  I immediately and erroneously concluded that this was the knowledge I had been missing to program all the things that I didn&#x27;t know how to program, and I began my counterproductive data-structure-fetishism phase.<p>Much better advice is found in <i>The Practice of Programming</i>: almost all programs can be written without any data structures but arrays, hash tables, linked lists, and, for things like parsing, symbolic algebra, or filesystems, trees.  So just use those if you can.<p>Occasionally, a program is only possible, or is dramatically better, using fancy data structures like LSM-trees, bloom filters, suffix arrays, skip lists, HAMTs, union find, zippers, treaps, BDDs, ropes, gap buffers, Fenwick trees, splay trees, and so on.  And that can be super fun!  But this situation is pretty rare, and it&#x27;s important not to get yourself into the position where you&#x27;re implementing a red-black tree or HAMTs when it would have been a lot easier to write your program in a brute-force way.  Or where your successors are fixing bugs in your Fenwick tree implementation.<p>Also, it&#x27;s important to be aware that, quite aside from the difficulty of writing and understanding the code, fancy data structures often have extra costs <i>at runtime</i> which can outweigh their benefits.  It&#x27;s easy to discover that your splay tree runs <i>slower</i> than sequential search over an unsorted array, for example, and of course it uses much more RAM.