Radix Trees

How IntMap Works

Tikhon Jelvis

Graphs

Functional Graphs

FGL

Data.IntMap

intmap-haddock.png

PATRICIA

Practical Algorithm to Retrieve Information Coded in Alphanumeric

Other Uses

Merging

okasaki.png

Prefix Operations

  1. all keys, sorted
  2. read by prefix
  3. update by prefix

two-keys.png

How IntMap Works

Tries

Retrieval

Sorry, your browser does not support SVG.

lookup “b”

Sorry, your browser does not support SVG.

b = 1

lookup “bab”

Sorry, your browser does not support SVG.

b

lookup “bab”

Sorry, your browser does not support SVG.

b → a

lookup “bab”

Sorry, your browser does not support SVG.

b → a → b = 7

Prefixes

every key starting with “ba”

b

Sorry, your browser does not support SVG.

b → a

Sorry, your browser does not support SVG.

b → a

Sorry, your browser does not support SVG.

Tries

retrieve key character by character

Binary Trie

Bit-by-Bit

Sorry, your browser does not support SVG.

Paths

Sorry, your browser does not support SVG.

two keys: 00011, 00001

Path Compression

Sorry, your browser does not support SVG.

two keys: 00011, 00001

Path Compression

Sorry, your browser does not support SVG.

Path Compression

Sorry, your browser does not support SVG.

fully compressed radix tree

Data.IntMap

binary trie with path compression

Data.IntMap

  • Leaf (path, key, value)
  • Branch (path, children)
  • Empty

Data.IntMap

type Prefix = Int
type Mask = Int

data IntMap a = 
    Branch !Prefix 
           !Mask 
           !(IntMap a) !(IntMap a)
  | Leaf !Prefix a
  | Empty

lookup

lookup _ Empty = Nothing
lookup k (Leaf k' v)
  | k == k'   = Just v
  | otherwise = Nothing

lookup

lookup k (Branch prefix control l r)
  | getPrefix k control /= prefix = Nothing
  | k .&. control == 0            = lookup k l
  | otherwise                     = lookup k r
getPrefix k control =
  key .&. complement ((control `shiftL` 1) - 1)

Beyond IntMap

Binary Trie

  • binary: “bit-by-bit”
  • binary: “two children”

Span

Sorry, your browser does not support SVG.

n bits = 2ⁿ children

Space vs Height

branching-tradeoff.png

from ART paper

Adaptive Radix Tree

art.png

Adaptive Radix Tree

256 children (byte at a time)

art-nodes.png

Nodes

art-nodes-details.png

Adaptive Radix Tree

data ART a = Empty
           | Leaf !Key a
           | Node !Mask !Prefix !(Children a)

Nodes

type Chunk    = Word8
type Chunks   = UArray Chunk Chunk
type Values a = Array Chunk a
type Size     = Word8

data Children a =
  N4   !Size !Chunks !Values
| N16  !Size !Chunks !Values
| N48  !Size !Chunks !Values
| N256 !Size !(Array Chunk (Maybe a))

Persistent Adaptive Radix Trees?

Recap

Tries

  • sorted keys
  • prefix operations
  • merging

Data.IntMap

  • binary trie
  • path compression

Beyond IntMap

  • different spans
  • adaptive radix trees

Questions?

Created by Tikhon Jelvis.