View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.trie;
18  
19  import java.util.Map;
20  
21  import org.apache.commons.collections4.Trie;
22  import org.apache.commons.collections4.trie.analyzer.StringKeyAnalyzer;
23  
24  /**
25   * Implements a PATRICIA Trie (Practical Algorithm to Retrieve Information
26   * Coded in Alphanumeric).
27   * <p>
28   * A PATRICIA {@link Trie} is a compressed
29   * {@link Trie}. Instead of storing
30   * all data at the edges of the {@link Trie}
31   * (and having empty internal nodes), PATRICIA stores data in every node.
32   * This allows for very efficient traversal, insert, delete, predecessor,
33   * successor, prefix, range, and {@link #select(Object)}
34   * operations. All operations are performed at worst in O(K) time, where K
35   * is the number of bits in the largest item in the tree. In practice,
36   * operations actually take O(A(K)) time, where A(K) is the average number of
37   * bits of all items in the tree.
38   * </p>
39   * <p>
40   * Most importantly, PATRICIA requires very few comparisons to keys while
41   * doing any operation. While performing a lookup, each comparison (at most
42   * K of them, described above) will perform a single bit comparison against
43   * the given key, instead of comparing the entire key to another key.
44   * </p>
45   * <p>
46   * The {@link Trie} can return operations in
47   * lexicographical order using the 'prefixMap', 'submap', or 'iterator' methods.
48   * The {@link Trie} can also
49   * scan for items that are 'bitwise' (using an XOR metric) by the 'select' method.
50   * Bitwise closeness is determined by the {@link KeyAnalyzer} returning true or
51   * false for a bit being set or not in a given key.
52   * </p>
53   * <p>
54   * This PATRICIA {@link Trie} supports both variable
55   * length &amp; fixed length keys. Some methods, such as {@link org.apache.commons.collections4.Trie#prefixMap(Object)}
56   * are suited only to variable length keys.
57   * </p>
58   *
59   * @param <V> The type of the values in this map
60   * @see <a href="https://en.wikipedia.org/wiki/Radix_tree">Radix Tree</a>
61   * @see <a href="https://users.monash.edu/~lloyd/tildeAlgDS/Tree/PATRICIA/">PATRICIA</a>
62   * @see <a href="https://www.imperialviolet.org/binary/critbit.pdf">Crit-Bit Tree</a>
63   * @since 4.0
64   */
65  public class PatriciaTrie<V> extends AbstractPatriciaTrie<String, V> {
66  
67      private static final long serialVersionUID = 4446367780901817838L;
68  
69      /**
70       * Constructs a new instance.
71       */
72      public PatriciaTrie() {
73          super(StringKeyAnalyzer.INSTANCE);
74      }
75  
76      /**
77       * Constructs a new instance.
78       *
79       * @param map mappings to be stored in this map.
80       */
81      public PatriciaTrie(final Map<? extends String, ? extends V> map) {
82          super(StringKeyAnalyzer.INSTANCE, map);
83      }
84  
85  }