Persistent data structure
A persistent data structure, also called a not ephemeral data structure, is a data structure in computing that always keeps the previous version of itself when it is changed. This is different from persistent storage. Because of this, these structures are effectively immutable. Their operations do not change the structure in place. Instead, they always produce a new, updated structure. The term was introduced in Driscoll, Sarnak, Sleator, and Tarjan's 1986 article.
Persistent data structures come in different types. A data structure is partially persistent if all of its versions can be accessed, but only the newest version can be changed. It is fully persistent if every version can be both accessed and changed. If the structure also has a meld or merge operation, which can create a new version from two earlier versions, it is called confluently persistent. Data structures that are not persistent are called ephemeral.
These data structures are especially common in logical and functional programming. Languages in these styles of programming discourage the use of mutable data, and some forbid it completely.
Partial versus full persistence
There are two main ways a persistent data structure can work. In the partial persistence model, a programmer can look at any earlier version, but can only change the newest one. This means the versions line up in a linear ordering, one after another. In the fully persistent model, a programmer can both change and look at any version. In some cases, the performance of working with older versions may get worse, as is true with the rope data structure.
A data structure is called confluently persistent if, on top of being fully persistent, two versions of it can be combined to make a new version that is still fully persistent.
Techniques for preserving previous versions
One simple method copies the whole structure for every change, using a platform provided ephemeral data structure such as an array. This is slow, because the entire backing structure must be copied for each write. Copy-on-write memory management can make updates cheaper. The fat node method instead records every change inside the nodes themselves, without erasing old values. Each node can grow extra values, and each value has a version stamp showing when it was changed. Reading the structure takes extra time, because the right version must be found at each node. Using a van Emde Boas tree at each node can speed up reading, but it makes updates slower. Path copying works on a structure of pointer-linked nodes. When a node is about to change, copies are made of all nodes on the path to it, and these changes are passed back up until the root node is reached. Looking up data then takes extra lookup time, which grows with the number of changes.
Driscoll, Sarnak, Sleator, and Tarjan combined fat nodes and path copying into one method. It works on a linked data structure where each node has a limited number of incoming pointers. Each node stores one small "modification box" holding a single change and a timestamp. When a node is read, its modification box is checked against the version being requested, so the right values are used. If a box is already full when a new change arrives, a copy of the node is made instead, and the change is passed up to the parent node. Its root nodes are kept in a sorted array. This algorithm keeps the work for each change small, and a potential function is used to show that the average cost stays low.
Generalized form of persistence
Path copying is one simple way to make a data structure, such as a binary search tree, persistent. A general method that works for any data structure is also useful. This method uses a graph of vertices, where each vertex has a fixed number of edges and a label that holds its data. Three operations can be used on the graph: creating a new vertex, changing an edge, and changing a label. The goal is to be able to access any version of the graph at any given time.
Each vertex keeps a table of rows, and each row stores its edges, its label, and the time of the operation. When a table is full, a new table is created and the old one becomes inactive. Creating a vertex makes a new table, and changing an edge or label adds a new row, or creates a new table if the current one is full. A method called the credit scheme shows that the total work for a sequence of operations stays limited.
Applications of persistent data structures
One useful problem that persistence can solve efficiently is the Next Element Search. Imagine n line segments that do not cross each other and are all parallel to the x-axis. The goal is to build a data structure that can take a point p and return the segment directly above it, if there is one. A simple method sweeps a vertical line from left to right and stops at each segment endpoint. This splits the plane into 2n+1 vertical strips, and a copy of the sorted list of segments is saved for each strip. A query then uses two binary searches and takes O(log(n)) time. However, the space needed can grow as large as O(n²), which is the main problem with this method. A persistent data structure keeps the same query time but needs much less space. Instead of copying everything at each step, it copies only the parts that change, since each step adds or removes just one segment. Each insertion or deletion takes O(log(n)) time, so the problem can be solved with O(log(n)) query time and O(n·log(n)) space. Please find below the source code for an example related to the next search problem.
Examples of persistent data structures
Purely functional data structures are automatically persistent. A simple example is the singly linked list, a list where each item holds a reference to the next one. New items can be added to the front while the old tail is shared, so the old list is never changed. Many structures, such as red–black trees, stacks, and treaps, can easily be made persistent. Others, like dequeues and structures that allow random access, need a bit more work. Linked lists are the basic data structure in ML-derived languages such as Haskell, where a list node cannot be changed once it is made, and the garbage collector removes it when nothing refers to it. Lisp-style languages like Scheme and Racket also support this kind of list. Persistent binary search trees work in a similar way, creating new nodes while sharing most of the old ones. Keeping track of this sharing usually requires garbage collection, which is why it is common in functional programming languages.
A persistent hash array mapped trie keeps its earlier versions when it is updated, and it is often used as a general-purpose persistent map. Phil Bagwell first described the original version in a 2001 paper, which presented a changeable hash table. Rich Hickey later changed it to be fully persistent for the Clojure programming language. The structure uses a hash function to turn each key into a number, and it uses parts of that number to move through a sparse array at each level. Items that share the same result may be stored together, depending on hash collisions. Most versions use a branching factor of 32, which keeps the number of steps very small in practice.
Usage in programming languages
Haskell is a pure functional language... let me restate that correctly: Haskell is a pure functional language that does not allow data to be changed, so all of its data structures are persistent. Changing a structure in a way that would make earlier versions invalid would break referential transparency. Like many languages in the Lisp family, Clojure has a linked list that is persistent by enforced rule, not just by habit. Its persistent vectors, maps, and sets also fit the read-only parts of the Java collections framework. Clojure's designers prefer these structures because they have value semantics, which makes them easy to share between threads and helps with parallel computing, since operations can be retried easily to avoid data races and to use atomic compare and swap semantics. The Elm programming language is purely functional like Haskell, so all of its data is persistent. Its custom virtual DOM takes advantage of this, and its developers reported in 2016 that it could render HTML faster than JavaScript frameworks such as React, Ember, and Angular.
The Java programming language is not particularly functional, but its core package includes CopyOnWriteArrayList and CopyOnWriteArraySet, which are persistent. Fully persistent collections are found in third-party libraries or other languages that run on Java's virtual machine. In JavaScript, popular frontend frameworks like React use unchanging state, often with Redux. Newer React and Redux applications mostly use Immer.js, which lets programmers write direct changes that are safely turned into unchanged states. Prolog terms are naturally unchangeable, so its data structures are usually persistent. The Scala programming language encourages persistent data structures, including linked lists, red–black trees, and persistent hash array mapped tries.
Garbage collection
Persistent data structures often share parts of their memory between versions. Because of this, using them well usually requires some kind of automatic garbage collection system, such as reference counting or mark and sweep.
Some computer platforms that use persistent data structures let programmers choose not to use garbage collection. This can lead to memory leaks, but in some cases it can help an application run faster overall.

Quiz time!
5 questions about Persistent data structure
What does a persistent data structure always keep when it is changed?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Persistent data structure, available under CC BY-SA 4.0.