MyNixOS website logo
Description

Efficient implementation of the implicit treap data structure.

Efficient implementation of the implicit treap data structure. Use this data structure if you want dynamic arrays with fast operations on segments.

treap

Treap: tree logo Hackage Build status MPL-2.0 license

Efficient implementation of the implicit treap data structure.

What does this package provide?

This package implements a tree-like data structure called implicit treap. This data structure implements interface similar to random-access arrays, but with fast (logarithmic time complexity) insert/delete/split/merge/take/drop/rotate operations. In addition, treap allows you to specify and measure values of any monoids on a segment, like a sum of elements or minimal element on some contiguous part of the array.

When to use this package?

Use this package when you want the following operations to be fast:

  1. Access elements by index.
  2. Insert elements by index.
  3. Delete elements by index.
  4. Calculate monoidal operation (like sum, product, min, etc.) of all elements between two indices.
  5. Call slicing operations like take or drop or split.

Below you can find the table of time complexity for all operations (where n is the size of the treap):

OperationTime complexityDescription
sizeO(1)Get number of elements in the treap
atO(log n)Access by index
insertO(log n)Insert by index
deleteO(log n)Delete by index
queryO(log n)Measure monoid on the segment
splitAtO(log n)Split treap by index into two treaps
mergeO(log n)Merge two treaps into a single one
takeO(log n)Take first i elements of the treap
dropO(log n)Drop first i elements of the treap
rotateO(log n)Put first i elements to the end

The package also comes with nice pretty-printing!

ghci> t = fromList [1..5] :: RTreap (Sum Int) Int
ghci> prettyPrint t
   5,15:2
      ╱╲
     ╱  ╲
    ╱    ╲
   ╱      ╲
1,1:1   3,12:4
          ╱╲
         ╱  ╲
        ╱    ╲
      1,3:3 1,5:5

Alternatives

If you don't need to calculate monoidal operations, you may alternatively use Seq from the containers package as it provides more extended interface but doesn't allow to measure monoidal values on segments.

Acknowledgement

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY.

Metadata

Version

0.0.0.0

License

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows