Giter Club home page Giter Club logo

treap's Introduction

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):

Operation Time complexity Description
size O(1) Get number of elements in the treap
at O(log n) Access by index
insert O(log n) Insert by index
delete O(log n) Delete by index
query O(log n) Measure monoid on the segment
splitAt O(log n) Split treap by index into two treaps
merge O(log n) Merge two treaps into a single one
take O(log n) Take first i elements of the treap
drop O(log n) Drop first i elements of the treap
rotate O(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.

treap's People

Contributors

chris-martin avatar chshersh avatar sphaso avatar vrom911 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

chris-martin

treap's Issues

Implement property-based tests using `hedgehog`

This is a meta-issue to collect all possible laws for the Treap data structure:

Semigroup/Monoid laws

  • Associativity
  • Left/Right identity

Measured

  • measure (one x) ≡ x

IsList

  • Roundtrip IsList: toList . fromList ≡ id

Modifications

  • at i . insert i x ≡ Just x
  • delete i . insert i x ≡ id
  • size (insert i x t) ≡ size t + 1

Cuts

  • uncurry merge . splitAt i ≡ id
  • size (merge l r) ≡ size l + size r
  • Monoid homomorphism: measure (merge l r) ≡ measure l <> measure r
  • splitAt i t ≡ (take i t, drop i t)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.