Skip to content

Commit

Permalink
Implemented shrinkInternalNodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
antononcube committed Feb 3, 2017
1 parent 8e828e2 commit b1de8f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Java/TriesWithFrequencies/src/Experiments.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public static void basic() {
System.out.println( TrieFunctions.shrink( strie, ":" ) );
System.out.println();

System.out.println( "shrink internal nodes only trie:");
System.out.println( TrieFunctions.shrinkInternalNodes( strie, ":", 1.0 ) );
System.out.println();

List<String> sword = new ArrayList() {{ add("b"); add("a"); add("r"); }};
System.out.println("For " + sword );
System.out.println( "contains: " + TrieFunctions.contains( strie, sword ) );
Expand Down
30 changes: 21 additions & 9 deletions Java/TriesWithFrequencies/src/TrieFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ protected static Trie nodeProbabilitiesRec(Trie tr) {
/// Retrieval functions
///**************************************************************

//! @description Test is a trie object a leaf.
protected static boolean leafQ( Trie tr ) {
return tr.getChildren() == null || tr.getChildren().isEmpty();
}

//! @description Find the position of a given word (or part of it) in the trie.
//! @param tr a trie object
//! @param word a list of strings
Expand Down Expand Up @@ -588,29 +593,36 @@ protected static List< Pair<String, Double> > leafProbabilitiesRec( Trie tr, int
//! @description Shrinks a trie by finding prefixes.
//! @param tr a trie object
public static Trie shrink(Trie tr) {
return shrinkRec(tr, "", -1, 0);
return shrinkRec(tr, "", -1, false, 0);
}

//! @description Shrinks a trie by finding prefixes.
//! @param tr a trie object
//! @param delimiter a delimiter to be used when strings are joined
public static Trie shrink(Trie tr, String delimiter) {
return shrinkRec(tr, delimiter, -1,0);
return shrinkRec(tr, delimiter, -1, false, 0);
}

//! @description Shrinks a trie by finding prefixes.
//! @param tr a trie object
//! @param delimiter a delimiter to be used when strings are joined
public static Trie shrinkByThreshold(Trie tr, String delimiter, double threshold ) {
return shrinkRec(tr, delimiter, threshold,0);
return shrinkRec(tr, delimiter, threshold, false, 0);
}

//! @description Shrinks a trie by finding prefixes.
//! @param tr a trie object
//! @param delimiter a delimiter to be used when strings are joined
public static Trie shrinkInternalNodes(Trie tr, String delimiter, double threshold ) {
return shrinkRec(tr, delimiter, threshold, true, 0);
}

//! @description Shrinking recursive function.
//! @param tr a trie object
//! @param delimiter a delimiter for the concatenation of the node keys
//! @param threshold if negative automatic shrinking test is applied
//! @param n recursion level
protected static Trie shrinkRec(Trie tr, String delimiter, double threshold, int n) {
protected static Trie shrinkRec(Trie tr, String delimiter, double threshold, boolean internalOnly, int n) {
Trie trRes = new Trie();
Boolean rootQ = ((n == 0) && tr.getKey().equals(""));

Expand All @@ -630,11 +642,11 @@ protected static Trie shrinkRec(Trie tr, String delimiter, double threshold, int
shrinkQ = arr.get(0).getValue() >= threshold;
}

if ( shrinkQ ) {
if ( shrinkQ && (!internalOnly || internalOnly && !leafQ( arr.get(0) ) ) ) {
// Only one child and the current node does not make a complete match:
// proceed with recursion and join with result.

Trie chTr = shrinkRec(arr.get(0), delimiter, threshold, n + 1);
Trie chTr = shrinkRec(arr.get(0), delimiter, threshold, internalOnly, n + 1);

trRes.setKey(tr.getKey() + delimiter + chTr.getKey());
trRes.setValue(tr.getValue());
Expand All @@ -646,7 +658,7 @@ protected static Trie shrinkRec(Trie tr, String delimiter, double threshold, int
} else {
// Only one child but the current node makes a complete match.

Trie chTr = shrinkRec(arr.get(0), delimiter, threshold,n + 1);
Trie chTr = shrinkRec(arr.get(0), delimiter, threshold, internalOnly, n + 1);

trRes.setKey(tr.getKey());
trRes.setValue(tr.getValue());
Expand All @@ -658,10 +670,10 @@ protected static Trie shrinkRec(Trie tr, String delimiter, double threshold, int

} else {
// No shrinking at this node. Proceed with recursion.
Map<String, Trie> recChildren = new HashMap<>();
Map<String, Trie> recChildren = new HashMap<String, Trie>();

for (Trie chTr : tr.getChildren().values()) {
Trie nTr = shrinkRec(chTr, delimiter, threshold, n + 1);
Trie nTr = shrinkRec(chTr, delimiter, threshold, internalOnly, n + 1);
recChildren.put(nTr.getKey(), nTr);
}

Expand Down

0 comments on commit b1de8f8

Please sign in to comment.