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.

Overview

Finger tree used as a simple queue with amortized O(1) put & get operations. Integers 1 to 21 are inserted to the right & extracted from the left. Square blocks represent values, "Digit" (sky blue) can have 1–4 children, "Node" (dark blue) can have 2–3 children, white circle is for "Empty", red node represents "Single" value & green nodes represent "Deep" values. Note that for each step we take down the spine, single values & digit children get nested with a new level of nodes.

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.[1]

A finger is a point where one can access part of a data structure; in imperative languages, this is called a pointer.[2] 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.[2]

2–3 tree and it transformed into a finger tree
Shows a 2–3 tree (top) can be pulled up into a finger tree (bottom)

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.[3]

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."[1] 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:[1]

data FingerTree a 
  = Empty 
  | Single a 
  | Deep (Digit a) (FingerTree (Node a)) (Digit a)

data Node a 
  = Node2 a a 
  | Node3 a a a

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:[1]

type 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 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.[1]

Reductions



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.[1]

Application

Finger trees can be used to build other trees.[4] 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.[1]

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.

Random-access sequences

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.[1]

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.

First publication

Finger trees were first published in 1977 by Leonidas J. Guibas,[5] and periodically refined since (e.g. a version using AVL trees,[6] non-lazy finger trees, simpler 2–3 finger trees shown here,[1] B-Trees and so on)

Implementations

Finger trees have since been used in the Haskell core libraries (in the implementation of Data.Sequence), and an implementation in OCaml exists[7] which was derived from a proven-correct Coq implementation.[8] There is also a verified implementation in Isabelle (proof assistant) from which programs in Haskell and other (functional) languages can be generated.[9] Finger trees can be implemented with or without lazy evaluation,[10] but laziness allows for simpler implementations.

See also

References

  1. ^ a b c d e f g h i Hinze, Ralf; Paterson, Ross (2006), "Finger Trees: A Simple General-purpose Data Structure" (PDF), Journal of Functional Programming, 16 (2): 197–217, doi:10.1017/S0956796805005769, S2CID 6881581.
  2. ^ a b Gibiansky, Andrew. "Finger Trees - Andrew Gibiansky". andrew.gibiansky.com. Retrieved 2017-10-26.
  3. ^ "Finger Trees Done Right (I hope)". Good Math, Bad Math. Retrieved 2017-10-26.
  4. ^ Sarkar, Abhiroop. "Finger Tree - The ultimate data structure?". abhiroop.github.io. Archived from the original on 2017-10-26. Retrieved 2017-10-26.
  5. ^ Guibas, L. J.; McCreight, E. M.; Plass, M. F.; Roberts, J. R. (1977), "A new representation for linear lists", Conference Record of the Ninth Annual ACM Symposium on Theory of Computing, pp. 49–60.
  6. ^ Tsakalidis, A. K. (1985), "AVL-trees for localized search", Information and Control, 67 (1–3): 173–194, doi:10.1016/S0019-9958(85)80034-6.
  7. ^ Caml Weekly News
  8. ^ Matthieu Sozeau :: Dependent Finger Trees in Coq
  9. ^ Nordhoff, Benedikt; Körner, Stefan; Lammich, Peter. "Finger Trees". Archive of Formal Proofs. Retrieved 26 November 2021.
  10. ^ Kaplan, H.; Tarjan, R. E. (1995), "Persistent lists with catenation via recursive slow-down", Proceedings of the Twenty-Seventh Annual ACM Symposium on the Theory of Computing, pp. 93–102.

Read other articles:

All SaintsInformasi latar belakangNama lainAll Saints 1.9.7.5 (1993–95)AsalLondon, InggrisGenre Pop R&B elektronika Tahun aktif 1993–2001 2006–2008 2013–sekarang Label ZTT London Parlophone Situs weballsaintsofficial.co.ukAnggotaMelanie BlattShaznay LewisNicole AppletonNatalie AppletonMantan anggotaSimone Rainford All Saints adalah sebuah grup musik asal Inggris-Kanada yang dibentuk tahun 1993.[1] Mereka didirikan sebagai All Saints 1.9.7.5 oleh anggota Melanie Blatt, Shaz...

 

 

Demokrat Kristen adalah sebutan bagi para penganut paham demokrasi Kristen sebagai ideologi politik yang ide utamanya adalah mengkonsepsikan penyatuan antara konservatisme religius, khususnya Kristen Katholik Roma dengan demokrasi, dan liberalisme. Paham ini berkembang di Eropa Barat, khususnya di negara-negara daratan Eropa – tidak termasuk Inggris - seperti Jerman dan Prancis.[1][2][3][4][5] Latar Belakang Pada 1945, pasca Perang Dunia II, ideologi ...

 

 

Untuk kegunaan lain, lihat Palu (disambiguasi). Palu Palu, pemukul, tukul, atau martil adalah alat yang digunakan untuk memberikan tumbukan kepada benda. Palu umum digunakan untuk memaku, memperbaiki suatu benda, penempaan logam dan menghancurkan suatu objek. Palu dirancang untuk tujuan tertentu dengan variasi dalam bentuk dan struktur. Bentuk umum palu terdiri dari gagang palu dan kepala palu, dengan sebagian besar berat berada di kepala palu. Desain dasar palu agar mudah digunakan, tetapi a...

Frank RiceKartu lobi menampilkan Rice (ketiga dari kiri) dalam Spook Ranch (1925)Lahir(1892-05-13)13 Mei 1892Muskegon, Michigan, Amerika SerikatMeninggal9 Januari 1936(1936-01-09) (umur 43)Los Angeles, California, Amerika SerikatTahun aktif1912-1936 Frank Rice (13 Mei 1892 – 9 Januari 1936) adalah seorang pemeran film Amerika Serikat. Ia tampil dalam lebih dari 120 film antara 1912 dan 1936. Ia lahir di Muskegon, Michigan, dan meninggal di Los Angeles, California ak...

 

 

Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada Maret 2016. SMP Negeri 4 TulakanInformasiDidirikan1998Jumlah kelas13Jurusan atau peminatanumumRentang kelasVII, VIII, IXKurikulumKurikulum Tingkat Satuan PendidikanJumlah siswa447AlamatLokasiDesa Wonosidi, Kecamatan Tulakan, Pacitan, Jawa TimurMoto SMP Neg...

 

 

Ini adalah nama Tionghoa; marganya adalah Oei. Artikel ini tidak memiliki referensi atau sumber tepercaya sehingga isinya tidak bisa dipastikan. Tolong bantu perbaiki artikel ini dengan menambahkan referensi yang layak. Tulisan tanpa sumber dapat dipertanyakan dan dihapus sewaktu-waktu.Cari sumber: Oei Tjong Hauw – berita · surat kabar · buku · cendekiawan · JSTOR Oei Tjong Hauw adalah ketua partai Chung Hwa Hui (CHH), partai kaum peranakan Tionghoa di...

Racing team This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Wayne Taylor Racing – news · newspapers · books · scholar · JSTOR (November 2020) (Learn how an...

 

 

American racecar driver Hurley HaywoodHaywood in 1994Nationality AmericanBorn (1948-05-04) May 4, 1948 (age 75)Chicago, Illinois24 Hours of Le Mans careerYears1977–1983, 1985–1987, 1990–1991, 1993–1994Best finish1st (1977, 1983, 1994)Class wins3 (1977, 1983, 1994) The Porsche 936 which Hurley Haywood drove to victory at the 1977 24 Hour of Le Mans Harris Hurley Haywood (born May 4, 1948)[1] is a retired American race car driver. Haywood has won multiple events, inclu...

 

 

Inverse image of zero under a homomorphism For other uses, see Kernel (disambiguation). In mathematics, the kernel of a linear map, also known as the null space or nullspace, is the part (or linear subspace) of the domain which the map maps to the zero vector.[1] That is, given a linear map L : V → W between two vector spaces V and W, the kernel of L is the vector space of all elements v of V such that L(v) = 0, where 0 denotes the zero vector in W,[2] or more symbolica...

Ini adalah sebuah nama Indonesia yang tidak menggunakan nama keluarga. Nama Soekarnoputri adalah sebuah patronimik. Sukmawati Soekarnoputri Informasi pribadiLahirDiah Mutiara Sukmawati Sukarnoputri26 Oktober 1951 (umur 72)Jakarta, IndonesiaPartai politikPNIMSuami/istriMangkunegara IX ​ ​(m. 1974; c. 1984)​Anak3, termasuk GPH Paundrakarna Sukmaputra JiwanegaraOrang tuaSoekarnoFatmawatiPekerjaanSenimanpolitikuspengusahaSunting kotak info ...

 

 

American football player (born 1984) For similarly-named American football players, see Joe Thomas (wide receiver) and Joe Thomas (linebacker). American football player Joe ThomasThomas with the Browns in 2015Munich RavensPosition:Offensive line coachPersonal informationBorn: (1984-12-04) December 4, 1984 (age 39)Brookfield, Wisconsin, U.S.Height:6 ft 6 in (1.98 m)Weight:312 lb (142 kg)Career informationHigh school:Brookfield Central (Brookfield, Wisconsin)Colleg...

 

 

この記事は検証可能な参考文献や出典が全く示されていないか、不十分です。出典を追加して記事の信頼性向上にご協力ください。(このテンプレートの使い方)出典検索?: コルク – ニュース · 書籍 · スカラー · CiNii · J-STAGE · NDL · dlib.jp · ジャパンサーチ · TWL(2017年4月) コルクを打ち抜いて作った瓶の栓 コルク(木栓、�...

Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada Oktober 2022. Geologi fisik adalah salah satu cabang ilmu geologi yang mempelajari Bumi dari sifat fisiknya. Ruang lingkup geologi fisik meliputi bahan pembentuk Bumi, atmosfer Bumi, hidrosfer serta proses-proses yang menerima pengaruh dari energi surya dan gravitas...

 

 

土库曼斯坦总统土库曼斯坦国徽土库曼斯坦总统旗現任谢尔达尔·别尔德穆哈梅多夫自2022年3月19日官邸阿什哈巴德总统府(Oguzkhan Presidential Palace)機關所在地阿什哈巴德任命者直接选举任期7年,可连选连任首任萨帕尔穆拉特·尼亚佐夫设立1991年10月27日 土库曼斯坦土库曼斯坦政府与政治 国家政府 土库曼斯坦宪法 国旗 国徽 国歌 立法機關(英语:National Council of Turkmenistan) ...

 

 

土库曼斯坦总统土库曼斯坦国徽土库曼斯坦总统旗現任谢尔达尔·别尔德穆哈梅多夫自2022年3月19日官邸阿什哈巴德总统府(Oguzkhan Presidential Palace)機關所在地阿什哈巴德任命者直接选举任期7年,可连选连任首任萨帕尔穆拉特·尼亚佐夫设立1991年10月27日 土库曼斯坦土库曼斯坦政府与政治 国家政府 土库曼斯坦宪法 国旗 国徽 国歌 立法機關(英语:National Council of Turkmenistan) ...

Spanish television series (2001–2023) This article is about the Spanish TV series. For Argentinian series, see Cuéntame cómo pasó (Argentina). Cuéntame cómo pasóAlso known as Cuéntame Remember When Genre Historical drama Comedy drama Created byMiguel Ángel Bernardeau [es]Starring Imanol Arias Ana Duato Ricardo Gómez María Galiana Pablo Rivero Irene Visedo Narrated byCarlos HipólitoOpening themeCuéntame [es]Country of originSpainOriginal languageSpanishN...

 

 

 本表是動態列表,或許永遠不會完結。歡迎您參考可靠來源來查漏補缺。 潛伏於中華民國國軍中的中共間諜列表收錄根據公開資料來源,曾潛伏於中華民國國軍、被中國共產黨聲稱或承認,或者遭中華民國政府調查審判,為中華人民共和國和中國人民解放軍進行間諜行為的人物。以下列表以現今可查知時間為準,正確的間諜活動或洩漏機密時間可能早於或晚於以下所歸�...

 

 

Singkatan stasiun ini bukan berarti Rupiah. Artikel ini bukan mengenai Stasiun Indramayu. Stasiun Indralaya K02 Peron Stasiun Indralaya dengan KA KertalayaLokasiJalan Raya Palembang-PrabumulihTanjung Pering, Indralaya, Ogan Ilir, Sumatera Selatan 30813IndonesiaKoordinat3°12′43″S 104°38′17″E / 3.21194°S 104.63806°E / -3.21194; 104.63806Koordinat: 3°12′43″S 104°38′17″E / 3.21194°S 104.63806°E / -3.21194; 104.63806Ketinggian...

موزوراس الإحداثيات 35°32′00″N 24°09′00″E / 35.53333333°N 24.15°E / 35.53333333; 24.15   تقسيم إداري  البلد اليونان[1]  عدد السكان  عدد السكان 186 (2021)949 (2001)466 (1991)134 (2011)  رمز جيونيمز 256660  تعديل مصدري - تعديل   موزوراس (باليونانية: Μουζουράς)‏ هي مدينة في خانية في اليون�...

 

 

هيركوليس     الإحداثيات 38°01′02″N 122°17′19″W / 38.017222222222°N 122.28861111111°W / 38.017222222222; -122.28861111111   [1] تاريخ التأسيس 1900  تقسيم إداري  البلد الولايات المتحدة[2][3]  التقسيم الأعلى مقاطعة كونترا كوستا  خصائص جغرافية  المساحة 51.757738 كيلومتر مربع47.08...