lca-0.3: O(log n) persistent on-line lowest common ancestor calculation without preprocessing

Copyright(C) 2011-2015 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.LCA.Online.Naive

Description

Naive online calculation of the the lowest common ancestor in O(h)

Synopsis

Documentation

data Path a Source

An uncompressed Path with memoized length.

empty :: Path a Source

The empty Path

cons :: Int -> a -> Path a -> Path a Source

O(1) Invariant: most operations assume that the keys k are globally unique

Extend the path with a new node ID and value.

uncons :: Path a -> Maybe (Int, a, Path a) Source

O(1) Extract the node ID and value from the newest node on the Path.

view :: Path a -> View Path a Source

O(1) Extract the node ID and value from the newest node on the Path, slightly faster than uncons.

null :: Foldable t => forall a. t a -> Bool

Test whether the structure is empty. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.

length :: Foldable t => forall a. t a -> Int

Returns the size/length of a finite structure as an Int. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.

isAncestorOf :: Path a -> Path b -> Bool Source

O(h) xs isAncestorOf ys holds when xs is a prefix starting at the root of Path ys.

lca :: Path a -> Path b -> Path a Source

O(h) Compute the lowest common ancestor of two paths

keep :: Int -> Path a -> Path a Source

O(h - k) to keep k elements of Path of length h

drop :: Int -> Path a -> Path a Source

O(k) to drop k elements from a Path

traverseWithKey :: Applicative f => (Int -> a -> f b) -> Path a -> f (Path b) Source

Traverse a Path with access to the node IDs.

toList :: Path a -> [(Int, a)] Source

Convert a Path to a list of (ID, value) pairs.

fromList :: [(Int, a)] -> Path a Source

Build a Path from a list of (ID, value) pairs.

(~=) :: Path a -> Path b -> Bool infix 4 Source

O(1) Compare to see if two trees have the same leaf key