# Data Structures


Common data structures you should know

## Lists

### Skip List

Technically a linked-list but looks like a tree if you squint.

## Hash

Use this all the time if you don't need to iterate through the data structure in order.
Probabilistic O(1) insertion.

#### Linear Probing

One way to handle collisions.

#### Double hashing

Another way to handle collisions.

#### Separate chaining

Another way to handle collisions. Each bucket is a pointer to a linked-list of values.

## Trees

### Red-Black

A red-black tree follows the following rules

1. Each node is either red or black.
2. The root is black. This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice versa, this rule has little effect on analysis.
3. All leaves (NIL) are black.
4. If a node is red, then both its children are black.
5. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.

#### Intuition

Consider any tree with a left and right child. From rule 4, we see that at most n/2 nodes are red. At least half will be black. Red nodes or levels are those interspersed between black levels. If the left child has $$\displaystyle m$$ levels then at most the right child can have $$\displaystyle 2m$$ levels.

Intuitively, this is more relaxed than AVL trees so they will have fewer operations for insert/delete but will be less balanced (i.e. longer search).
Red-black trees are used in in C++ (ordered_map, ordered_set) and Java (TreeMap, TreeSet).

#### Complexity

$$\displaystyle 2\log (n+1)$$ height.
$$\displaystyle O(\log n)$$ search, insert, delete

### B-tree

Wikipedia: B-tree
B-tree visualization
A very popular data structure for filesystems and memory indexes since the maximum size of a node in the B-tree can be configured to the size of a page of memory.

#### 2-3

A B-tree of order 3

### Treap

A tree and a heap. O(logn) with high probability. The main benefits are that it is very easy to implement.

#### Insertion

Insert using BST insert and then heapify using AVL rotations.

### Segment Trees

• This is a binary tree for holding segments.
• Leaves hold something called "elementary intervals" and internal nodes hold the union of elementary intervals of its children.
• In addition, each leaf or node v also holds intervals which span their segment, i.e. $$\displaystyle \{X \mid Int(v) \in X\}$$

## Spatial Data Structures

Extension of BST.

Simple to implement. See my implementation here. Requires a known range (region) of values.
O(log k) where k is the region divided by the distance between the two closest points (i.e. your grid is $$\displaystyle k \times k$$).