ArticleslgStudy

computer science

Finger tree

Finger tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Finger tree rather than just read about it. In short: In computer science, a finger tree is a purely functional data structure that can be used to efficiently implement other functional data structures. A finger tree gives amortized constant time access to the "fingers" (leaves) of the tree, which is where data is stored, and concatenation and splitting logarithmic time in the size of the smaller piece.

Finger tree — main illustration
Finger tree — illustration

Key takeaways

  • Finger tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Finger tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Finger tree from memory before moving on to harder problems.

Reference excerpt

In computer science, a finger tree is a purely functional data structure that can be used to efficiently implement other functional data structures. A finger tree gives amortized constant time access to the "fingers" (leaves) of the tree, which is where data is stored, and concatenation and splitting logarithmic time in the size of the smaller piece. It also stores in each internal node the result of applying some associative operation to its descendants. This "summary" data stored in the internal nodes can be used to provide the functionality of data structures other than trees.

Overview

Ralf Hinze and Ross Paterson state a finger tree is a functional representation of persistent sequences that can access the ends in amortized constant time. Concatenation and splitting can be done in logarithmic time in the size of the smaller piece. The structure can also be made into a general purpose data structure by defining the split operation in a general form, allowing it to act as a sequence, priority queue, search tree, or priority search queue, among other varieties of abstract data types. A finger is a point where one can access part of a data structure; in imperative languages, this is called a pointer. In a finger tree, the fingers are structures that point to the ends of a sequence, or the leaf nodes. The fingers are added on to the original tree to allow for constant time access to fingers. In the images shown below, the fingers are the lines reaching out of the spine to the nodes. A finger tree is composed of different layers which can be identified by the nodes along its spine. The spine of a tree can be thought of as the trunk in the same way trees have leaves and a root. Though finger trees are often shown with the spine and branches coming off the sides, there are actually two nodes on the spine at each level that have been paired to make this central spine. The prefix is on the left of the spine, while the suffix is on the right. Each of those nodes has a link to the next level of the spine until they reach the root.

The first level of the tree contains only values, the leaf nodes of the tree, and is of depth 0. The second level is of depth 1. The third is of depth 2 and so on. The closer to the root, the deeper the subtrees of the original tree (the tree before it was a finger tree) the nodes points to. In this way, working down the tree is going from the leaves to the root of the tree, which is the opposite of the typical tree data structure. To get this nice and unusual structure, we have to make sure the original tree has a uniform depth. To ensure that the depth is uniform, when declaring the node object, it must be parameterized by the type of the child. The nodes on the spine of depth 1 and above point to trees, and with this parameterization they can be represented by the nested nodes.

Transforming a tree into a finger tree We will start this process with a balanced 2–3 tree. For the finger tree to work, all the leaf nodes need to also be level. A finger is "a structure providing efficient access to nodes of a tree near a distinguished location." To make a finger tree we need to put fingers to the right and left ends of the tree and transform it like a zipper. This gives us that constant amortized time access to the ends of a sequence. To transform, start with the balanced 2–3 tree. Take the leftmost and rightmost internal nodes of the tree and pull them up so the rest of the tree dangles between them as shown in the image to the right. Combines the spines to make a standard 2–3 finger tree. This can be described as:

The digits in the examples shown are the nodes with letters. Each list is divided by the prefix or suffix of each node on the spine. In a transformed 2–3 tree it seems that the digit lists at the top level can have a length of two or three with the lower levels only having length of one or two. In order for some application of finger trees to run so efficiently, finger trees allow between one and four subtrees on each level.

The digits of the finger tree can be transformed into a list like so: And so on the image, the top level has elements of type a, the next has elements of type Node a because the node in between the spine and leaves, and this would go on meaning in general that the nth level of the tree has elements of type Noden a, or 2–3 trees of depth n. This means a sequence of n elements is represented by a tree of depth Θ(log n). Even better, an element d places from the nearest end is stored at a depth of Θ(log d) in the tree. This process can also be done with other trees, like the 2–3–4 tree at right. This helps to see that the Digits are actually the annotations which were inside the tree, and each digit is conceptually associated with a subtree that may be empty.

Reductions instance Reduce Node wherereducer (≺) (Node2 a b) z= a ≺(b ≺ z)reducer (≺) (Node3 a b c) z= a ≺( b≺(c ≺ z))reducel (≻) z (Node2 b a) = (z ≻ b) ≻ areducel (≻) z (Node3 c b a) = ((z ≻ c)≻ b)≻ a

instance Reduce FingerTree wherereducer (≺) (Empty) z = zreducer (≺) (Single x) z= x ≺ zreducer (≺) (Deep pr m sf) z= pr ≺′ (m ≺″ (sf ≺′ z)) where (≺′) =reducer (≺) (≺″) =reducer (reducer (≺))reducel (≻) z (Empty) = zreducel (≻) z (Single x) = z ≻ xreducel (≻) z (Deep pr m sf) = ((z ≻′ pr) ≻″ m) ≻′ sf) where (≻′) =reducel (≻) (≻″) =reducel (reducel (≻))

Deque operations Finger trees also make efficient deques. Whether the structure is persistent or not, all operations take Θ(1) amortized time. The analysis can be compared to Okasaki's implicit deques, the only difference being that the FingerTree type stores Nodes instead of pairs.

Application Finger trees can be used to build other trees. For example, a priority queue can be implemented by labeling the internal nodes by the minimum priority of its children in the tree, or an indexed list/array can be implemented with a labeling of nodes by the count of the leaves in their children. Other applications are random-access sequences, described below, ordered sequences, and interval trees. Finger trees can provide amortized O(1) pushing, reversing, popping, O(log n) append and split; and can be adapted to be indexed or ordered sequences. And like all functional data structures, it is inherently persistent; that is, older versions of the tree are always preserved.

… excerpt ends here. Continue reading the full article.

Illustrations

Finger tree: Shows a 2–3 tree (top) can be pulled up into a finger tree (bottom)
Shows a 2–3 tree (top) can be pulled up into a finger tree (bottom)
Finger tree: Finger tree version of a random 2-3-4 tree
Finger tree version of a random 2-3-4 tree

Worked examples

Example 1 — a first encounter with Finger tree

Start with the simplest possible case. Write down what Finger tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Finger tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Finger tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Finger tree

In research
Finger tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Finger tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Finger tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Amortized data structures, Functional data structures, Trees (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Finger tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Finger tree” →

How to study Finger tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Finger tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Finger tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Finger tree in simple terms?

In computer science, a finger tree is a purely functional data structure that can be used to efficiently implement other functional data structures. A finger tree gives amortized constant time access to the "fingers" (leaves) of the tree, which is where data is stored, and concatenation and splitti…

Why does Finger tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Finger tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Finger tree.

Tags

  • Amortized data structures
  • Functional data structures
  • Trees (data structures)

Keep exploring