this method makes more sense to be in HuffNode

This commit is contained in:
Vincent Batts 2012-11-08 14:21:05 -05:00
parent a2c7846134
commit 8e5f856c09
2 changed files with 39 additions and 33 deletions

View file

@ -122,7 +122,7 @@ public class HuffNode {
* search down the tree, for the node, * search down the tree, for the node,
* at address of String bits, on HuffNode trie * at address of String bits, on HuffNode trie
*/ */
public HuffNode findByBits(HuffNode trie, String bits) { private HuffNode findByBits(HuffNode trie, String bits) {
if (bits.length() == 0) { if (bits.length() == 0) {
return trie; return trie;
} }
@ -138,6 +138,40 @@ public class HuffNode {
return null; return null;
} }
public String getBitPath(Object need) {
return getBitPath(this, need);
}
/**
* get a String of the bits, that map to Object need
*/
private String getBitPath(HuffNode trie, Object need) {
HuffNode left = trie.getLeft();
HuffNode right = trie.getRight();
if (left != null && left.getValue() != null) {
if (need.equals(left.getValue())) {
return "0";
}
}
if (right != null && right.getValue() != null) {
if (need.equals(right.getValue())) {
return "1";
}
}
if (left != null) {
String leftPath = getBitPath(left, need);
if (leftPath.length() > 0) {
return "0" + leftPath;
}
}
if (right != null) {
String rightPath = getBitPath(right, need);
if (rightPath.length() > 0) {
return "1" + rightPath;
}
}
return "";
}
/** /**
* pretty information * pretty information
*/ */

View file

@ -655,13 +655,13 @@ public class PathTree {
baos.write(nodeSize); baos.write(nodeSize);
} }
StringBuffer bits = new StringBuffer(); StringBuffer bits = new StringBuffer();
String endNodeLocation = findHuffPath(stringParent, HuffNode.END_NODE); String endNodeLocationBitPath = stringParent.getBitPath(HuffNode.END_NODE);
for (PathNode pn : pathNodes) { for (PathNode pn : pathNodes) {
for (NodePair np : pn.getChildren()) { for (NodePair np : pn.getChildren()) {
bits.append(findHuffPath(stringParent, np.getName())); bits.append(stringParent.getBitPath(np.getName()));
bits.append(findHuffPath(pathNodeParent, np.getConnection())); bits.append(pathNodeParent.getBitPath(np.getConnection()));
} }
bits.append(endNodeLocation); bits.append(endNodeLocationBitPath);
while (bits.length() >= 8) { while (bits.length() >= 8) {
int next = 0; int next = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -724,34 +724,6 @@ public class PathTree {
return result; return result;
} }
private String findHuffPath(HuffNode trie, Object need) {
HuffNode left = trie.getLeft();
HuffNode right = trie.getRight();
if (left != null && left.getValue() != null) {
if (need.equals(left.getValue())) {
return "0";
}
}
if (right != null && right.getValue() != null) {
if (need.equals(right.getValue())) {
return "1";
}
}
if (left != null) {
String leftPath = findHuffPath(left, need);
if (leftPath.length() > 0) {
return "0" + leftPath;
}
}
if (right != null) {
String rightPath = findHuffPath(right, need);
if (rightPath.length() > 0) {
return "1" + rightPath;
}
}
return "";
}
/** /**
* given a tokenized URL path, build out the PathNode parent, * given a tokenized URL path, build out the PathNode parent,
* and append endMarker to terminal nodes. * and append endMarker to terminal nodes.