From 8e5f856c092b4a8887068254962767be9864f78b Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 8 Nov 2012 14:21:05 -0500 Subject: [PATCH] this method makes more sense to be in HuffNode --- src/main/java/com/redhat/trie/HuffNode.java | 36 ++++++++++++++++++++- src/main/java/com/redhat/trie/PathTree.java | 36 +++------------------ 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/redhat/trie/HuffNode.java b/src/main/java/com/redhat/trie/HuffNode.java index 43b83ab..9f089d4 100644 --- a/src/main/java/com/redhat/trie/HuffNode.java +++ b/src/main/java/com/redhat/trie/HuffNode.java @@ -122,7 +122,7 @@ public class HuffNode { * search down the tree, for the node, * 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) { return trie; } @@ -138,6 +138,40 @@ public class HuffNode { 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 */ diff --git a/src/main/java/com/redhat/trie/PathTree.java b/src/main/java/com/redhat/trie/PathTree.java index c1a3c7d..5476cec 100644 --- a/src/main/java/com/redhat/trie/PathTree.java +++ b/src/main/java/com/redhat/trie/PathTree.java @@ -655,13 +655,13 @@ public class PathTree { baos.write(nodeSize); } StringBuffer bits = new StringBuffer(); - String endNodeLocation = findHuffPath(stringParent, HuffNode.END_NODE); + String endNodeLocationBitPath = stringParent.getBitPath(HuffNode.END_NODE); for (PathNode pn : pathNodes) { for (NodePair np : pn.getChildren()) { - bits.append(findHuffPath(stringParent, np.getName())); - bits.append(findHuffPath(pathNodeParent, np.getConnection())); + bits.append(stringParent.getBitPath(np.getName())); + bits.append(pathNodeParent.getBitPath(np.getConnection())); } - bits.append(endNodeLocation); + bits.append(endNodeLocationBitPath); while (bits.length() >= 8) { int next = 0; for (int i = 0; i < 8; i++) { @@ -724,34 +724,6 @@ public class PathTree { 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, * and append endMarker to terminal nodes.