Note that there are some explanatory texts on larger screens.

plurals
  1. POImplement an immutable deque as a balanced binary tree?
    primarykey
    data
    text
    <p>I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure.</p> <p>There seem to be different ways of doing this. AFAIK, <a href="https://stackoverflow.com/questions/1993760/how-is-memory-efficient-non-destructive-manipulation-of-collections-achieved-in-f/1993931#1993931">immutable data structures are generally hierarchical</a>, so that major parts of it can be reused after modifying operations such as the insertion or removal of an item.</p> <p>Eric Lippert has <a href="http://blogs.msdn.com/b/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx" rel="nofollow noreferrer">two</a> <a href="http://blogs.msdn.com/b/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double-ended-queue.aspx" rel="nofollow noreferrer">articles</a> on his blog about this topic, along with sample implementations in C#.</p> <p>Both of his implementations strike me as more elaborate than is actually necessary. Couldn't deques simply be implemented as binary trees, where elements can only be inserted or removed on the very "left" (the <em>front</em>) and on the very "right" (the <em>back</em>) of the tree?</p> <pre><code> o / \ … … / \ … … / \ / \ front --&gt; L … … R &lt;-- back </code></pre> <p>Additionally, the tree would be kept reasonably balanced with rotations:</p> <ul> <li>right rotations upon insertion at the front or upon removal from the back, and</li> <li>left rotations upon removal from the front or insertion at the back.</li> </ul> <p>Eric Lippert is, in my opinion, a very smart person whom I deeply respect, yet he apparently didn't consider this approach. Thus I wonder, was it for a good reason? Is my suggested way of implementing deques naïve?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload