Skip to content

Commit 89035c3

Browse files
authored
updated PATRICIA_Trie
Removed the printKeys utility method that printed all keys in the trie.
1 parent 72e6c43 commit 89035c3

File tree

1 file changed

+2
-49
lines changed

1 file changed

+2
-49
lines changed

src/main/java/com/thealgorithms/datastructures/trees/PATRICIA_Trie.java

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.thealgorithms.datastructures.tries;
22

3-
import java.math.BigInteger;
4-
import java.util.Objects;
5-
import java.util.stream.Collectors;
63
import java.util.stream.IntStream;
74

85
/**
@@ -13,6 +10,8 @@
1310
* as keys, common in many networking and IP lookup contexts, and relies on
1411
* bitwise operations for efficiency.
1512
*
13+
* <p>Reference: <a href="https://en.wikipedia.org/wiki/Radix_tree">Wikipedia: Radix Tree (Patricia Trie)</a>
14+
*
1615
* <p>Key characteristics:
1716
* <ul>
1817
* <li>**Radix-2 Trie:** Works on the binary representation of integer keys.</li>
@@ -223,52 +222,6 @@ private PatriciaTrieNode insert(PatriciaTrieNode t, int element) {
223222
return t;
224223
}
225224

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-
272225
// --- Main Driver and Example Usage ---
273226

274227
public static void main(String[] args) {

0 commit comments

Comments
 (0)