Book Image

Haskell Data Analysis cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis cookbook

By: Nishant Shukla

Overview of this book

Step-by-step recipes filled with practical code samples and engaging examples demonstrate Haskell in practice, and then the concepts behind the code. This book shows functional developers and analysts how to leverage their existing knowledge of Haskell specifically for high-quality data analysis. A good understanding of data sets and functional programming is assumed.
Table of Contents (14 chapters)
13
Index

Using a self-balancing tree


An AVL tree is a balanced binary search tree. The heights of each subtree differ by at most one. On each insertion or deletion, the tree shifts around its nodes through a series of rotations to become balanced. A balanced tree ensures the height is minimized, which guarantees lookups and insertions to be within O(log n) time. In this recipe, we will use an AVL tree package directly, but self-balancing trees also exist within the Data.Set and Data.Map implementations.

Getting ready

We will be using the AvlTree package to use Data.Tree.AVL:

$ cabal install AvlTree

How to do it...

  1. Import the relevant AVL tree packages:

    import Data.Tree.AVL
    import Data.COrdering
  2. Set up an AVL tree from a list of values and read the minimum and maximum values from it:

    main = do
      let avl  = asTree fstCC [4,2,1,5,3,6]
      let min  = tryReadL avl
      let max  = tryReadR avl
      print min
      print max
  3. The minimum and maximum values are printed out as follows:

    $ runhaskell Main.hs
    
    Just 1
    Just 6
    

How...