e01.org

Finger tree

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. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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. data FingerTree a The digits of the finger tree can be transformed into a list like so: data Digit a = One a | Two a a | Three a a a | Four a a a a 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 Node^n 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. &reducer\ (\prec)\ (Node2 \ a \ b)\ z &=&\ a\ \prec(b\ \prec\ z) \\ &reducer\ (\prec)\ (Node3 \ a \ b \ c)\ z &=&\ a\ \prec(\ b\prec(c\ \prec\ z )) \\ \\ &reducel\ (\succ)\ z\ (Node2 \ b \ a)\ &=&\ (z\ \succ\ b)\ \succ\ a \\ &reducel\ (\succ)\ z\ (Node3 \ c \ b \ a)\ &=&\ ((z\ \succ\ c) \succ\ b) \succ\ a \\ \end{align} \begin{align} \mathrm{instance}& \ Reduce \ FingerTree \ \mathrm{where} && \\ &reducer\ (\prec)\ (Empty)\ z\ &=&\ z \\ &reducer\ (\prec)\ (Single \ x)\ z &=&\ x\ \prec\ z \\ &reducer\ (\prec)\ (Deep \ pr \ m \ sf)\ z &=&\ pr\ \prec'\ (m\ \prec\ (sf\ \prec'\ z)) \\ & \ \ \ \ where \\ & \ \ \ \ \ \ \ \ (\prec')\ = reducer\ (\prec) \\ & \ \ \ \ \ \ \ \ (\prec)\ = reducer\ (reducer\ (\prec)) \\ \\ & reducel\ (\succ)\ z\ (Empty)\ &=&\ z \\ & reducel\ (\succ)\ z\ (Single\ \ x)\ &=&\ z\ \succ\ x \\ & reducel\ (\succ)\ z\ (Deep\ \ pr \ m \ sf)\ &=&\ ((z\ \succ'\ pr)\ \succ\ m)\ \succ'\ sf) \\ & \ \ \ \ where \\ & \ \ \ \ \ \ \ \ (\succ')\ = reducel\ (\succ) \\ & \ \ \ \ \ \ \ \ (\succ)\ = reducel\ (reducel\ (\succ))\\ \end{align} Finger trees can efficiently implement random-access sequences. This should support fast positional operations including accessing the nth element and splitting a sequence at a certain position. To do this, we annotate the finger tree with sizes. newtype Size = Size{ getSize :: N } deriving (Eq, Ord) instance Monoid Size where ∅ = Size 0 Size m ⊕ Size n = Size (m + n) The N is for natural numbers. The new type is needed because the type is the carrier of different monoids. Another new type is still needed for the elements in the sequence, shown below. newtype Elem a = Elem{ getElem :: a } newtype Seq a = Seq (FingerTree Size (Elem a)) instance Measured (Elem a) Size where ||Elem|| = Size 1 These lines of code show that instance works a base case for measuring the sizes and the elements are of size one. The use of newtypes doesn't cause a run-time penalty in Haskell because in a library, the Size and Elem types would be hidden from the user with wrapper functions. With these changes, the length of a sequence can now be computed in constant time.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Finger tree". 1790280142. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1376552856

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·