Patricia Tree

Active
Target
Pivot
idle
Initializing...
Time Complexity
Best
O(k)O(k)
Average
O(k)O(k)
Worst
O(k)O(k)
Space Complexity
O(n)O(n)
StabilityFALSE
In-placeFALSE

Explanation

Patricia Tree (Compressed Trie)

A Patricia Tree (Practical Algorithm to Retrieve Information Coded in Alphanumeric) is a specialized dictionary based on the trie that is extremely space-efficient. It compresses a trie by merging nodes that have only one child with their parents.

Key Features

  • Space Efficiency: By compressing paths with single children, it significantly reduces the number of nodes compared to a standard Trie.
  • Prefix Matching: Like a Trie, it is excellent for prefix-based searches and autocomplete features.
  • Edge Labels: Instead of storing a single character per edge, a Patricia Tree stores strings (sequences of characters) on its edges.

How it works

When inserting a word, if the algorithm encounters a node where the existing prefix matches the word being inserted, it either:

  1. Continues down if the match is complete.
  2. Splits the node if only a part of the prefix matches.

Pseudo Code

1procedure insert(word)
2 node = root
3 find common prefix among children
4 if common prefix exists:
5 if exact match:
6 recurse into child
7 else:
8 split child node at common prefix
9 add new child for remaining word
10 else:
11 add new leaf node
12end procedure
Sponsor Ad