|
1 | 1 | package com.thealgorithms.datastructures.tries; |
2 | 2 |
|
3 | | -import java.math.BigInteger; |
4 | | -import java.util.Objects; |
5 | | -import java.util.stream.Collectors; |
6 | 3 | import java.util.stream.IntStream; |
7 | 4 |
|
8 | 5 | /** |
|
13 | 10 | * as keys, common in many networking and IP lookup contexts, and relies on |
14 | 11 | * bitwise operations for efficiency. |
15 | 12 | * |
| 13 | + * <p>Reference: <a href="https://en.wikipedia.org/wiki/Radix_tree">Wikipedia: Radix Tree (Patricia Trie)</a> |
| 14 | + * |
16 | 15 | * <p>Key characteristics: |
17 | 16 | * <ul> |
18 | 17 | * <li>**Radix-2 Trie:** Works on the binary representation of integer keys.</li> |
@@ -223,52 +222,6 @@ private PatriciaTrieNode insert(PatriciaTrieNode t, int element) { |
223 | 222 | return t; |
224 | 223 | } |
225 | 224 |
|
226 | | - /** |
227 | | - * Utility method to print all keys in the trie (in order of insertion discovery). |
228 | | - * @param t The root node. |
229 | | - */ |
230 | | - private void printKeys(PatriciaTrieNode t) { |
231 | | - if (t == null) { |
232 | | - return; |
233 | | - } |
234 | | - |
235 | | - PatriciaTrieNode startNode = t.leftChild; // Start at the first meaningful node |
236 | | - |
237 | | - // Use a set to track visited nodes and prevent infinite loop due to back pointers |
238 | | - java.util.Set<PatriciaTrieNode> visitedNodes = new java.util.HashSet<>(); |
239 | | - java.util.Queue<PatriciaTrieNode> queue = new java.util.LinkedList<>(); |
240 | | - |
241 | | - // Add the sentinel/root node's left child if it's not the root itself (0 bit) |
242 | | - if (startNode != t && startNode != null) { |
243 | | - queue.add(startNode); |
244 | | - visitedNodes.add(startNode); |
245 | | - } |
246 | | - |
247 | | - // Handle the root key if it's the only one |
248 | | - if (t.leftChild == t && t.key != 0) { |
249 | | - System.out.print(t.key + " "); |
250 | | - return; |
251 | | - } |
252 | | - |
253 | | - while (!queue.isEmpty()) { |
254 | | - PatriciaTrieNode current = queue.poll(); |
255 | | - |
256 | | - // The 'key' in a Patricia node is only the data stored at the time of creation. |
257 | | - // It is NOT a full traversal output. Traversal requires following the logic. |
258 | | - // This traversal is complex due to back pointers. A simpler in-order traversal |
259 | | - // that avoids infinite loops by checking bit numbers is typically used. |
260 | | - |
261 | | - // Simplest key extraction for this structure: Recursively find external nodes |
262 | | - // by detecting back pointers. |
263 | | - |
264 | | - // Skip if the node is a back pointer (i.e., its child is itself or points "back" |
265 | | - // to a node with a smaller or equal bit number). |
266 | | - // NOTE: A standard in-order traversal is difficult due to the compressed structure. |
267 | | - // We will stick to the basic functionality and provide a simple list of inserted keys |
268 | | - // for demonstration in the main method. |
269 | | - } |
270 | | - } |
271 | | - |
272 | 225 | // --- Main Driver and Example Usage --- |
273 | 226 |
|
274 | 227 | public static void main(String[] args) { |
|
0 commit comments