## Octree Algorithms

The principle of the octree algorithm is to sequentially read in the
image. Every color is then stored in an octree of depth 8 (every leaf
at depth 8 represents a distinct color). A limit of K (in this case K
= 256) leaves is placed on the tree. Insertion of a color in the tree
can result in two outcomes.
- If there are less than K leaves the the color is filtered down the
tree until either it reaches some leaf node that has an associated
representative color or it reaches the leaf node representing its
unique color.

- If there are greater than K leaves in the tree some set of leaves
in the tree must be merged (their representative colors averaged)
together and a new representative color stored in their parent.

Gervautz & Purgathofer[1] offer 2 possible criteria to be used in the
selection of leaves to be merged.
- Reducible nodes that have the largest depth in the tree should be
chosen first. They represent colors that lie closest together.

- If there is more than one group of leaves at the maximum depth the
algorithm could:

- Merge the leaves that represent the fewest number of pixels. This
will help keep the error small

- Reduce the leaves that represent the most pixels. In this case
large areas will be uniformly filled in a slightly wrong color while
maintaining detailed shadings.

Once the entire image has been processed in this manner the color map
consists of the representative colors of the leaf nodes in the
tree. The index of the color map is then stored at that leaf, and the
process of quantizing the image is simply filtering each color down
the tree until a leaf is hit.
Because a limit is placed on the number of leaves in the tree this
algorithm has a modest memory complexity, O(K), compared to the median
cut and popularity algorithms. The time complexity is more
unclear. Gervautz & Purgathofer[1] site the search phase as being O(N)
where N is the number of pixels in the image. This is clearly best
case behavior. The average case needs to address the complexity of the
merging algorithm.