diff --git a/src/main/java/com/hashbangbash/trie/App.java b/src/main/java/com/hashbangbash/trie/App.java index 0990c8a..f447639 100644 --- a/src/main/java/com/hashbangbash/trie/App.java +++ b/src/main/java/com/hashbangbash/trie/App.java @@ -17,6 +17,7 @@ import com.redhat.trie.Util; public class App { public static void main(String[] args) { + Util util = new Util(); FileInputStream fis; DataInputStream in; BufferedReader br; @@ -51,7 +52,7 @@ public class App { //System.out.println(contentList.toString()); PathNode root = new PathNode(); - Util.makePathTree(contentList, root); + util.makePathTree(contentList, root); Util.printTree(root, 0); } diff --git a/src/main/java/com/redhat/trie/PathNode.java b/src/main/java/com/redhat/trie/PathNode.java index 03a71c1..310f82f 100644 --- a/src/main/java/com/redhat/trie/PathNode.java +++ b/src/main/java/com/redhat/trie/PathNode.java @@ -38,6 +38,10 @@ public class PathNode { return this.id; } + public NodeContext getContext() { + return this.ctx; + } + void addChild(NodePair cp) { this.children.add(cp); } diff --git a/src/main/java/com/redhat/trie/Util.java b/src/main/java/com/redhat/trie/Util.java index f4e7cc6..499268d 100644 --- a/src/main/java/com/redhat/trie/Util.java +++ b/src/main/java/com/redhat/trie/Util.java @@ -31,8 +31,14 @@ import java.util.HashSet; * */ public class Util { - public static PathNode makePathTree(List contents, PathNode parent) { - PathNode endMarker = new PathNode(); + private NodeContext ctx; + + public Util() { + this.ctx = new NodeContext(); + } + + public PathNode makePathTree(List contents, PathNode parent) { + PathNode endMarker = new PathNode(this.ctx); for (String path : contents) { StringTokenizer st = new StringTokenizer(path, "/"); makePathForURL(st, parent, endMarker); @@ -98,7 +104,7 @@ public class Util { * given a tokenized URL path, build out the PathNode parent, * and append endMarker to terminal nodes. */ - private static void makePathForURL(StringTokenizer st, PathNode parent, PathNode endMarker) { + private void makePathForURL(StringTokenizer st, PathNode parent, PathNode endMarker) { if (st.hasMoreTokens()) { String childVal = st.nextToken(); if (childVal.equals("")) { @@ -116,7 +122,7 @@ public class Util { if (isNew) { PathNode next = null; if (st.hasMoreTokens()) { - next = new PathNode(); + next = new PathNode(parent.getContext()); parent.addChild(new NodePair(childVal, next)); next.addParent(parent); makePathForURL(st, next, endMarker); @@ -130,7 +136,7 @@ public class Util { } } - public static void condenseSubTreeNodes(PathNode location) { + public void condenseSubTreeNodes(PathNode location) { // "equivalent" parents are merged List parentResult = new ArrayList(); parentResult.addAll(location.getParents()); @@ -225,5 +231,39 @@ public class Util { } } + private List orderNodes(PathNode treeRoot) { + List result = new ArrayList(); + + // walk tree to make string map + Set nodes = getPathNodes(treeRoot); + for (PathNode pn : nodes) { + int count = pn.getParents().size(); + if (nodes.size() == 0) { + nodes.add(pn); + } + else { + int pos = result.size(); + for (int i = 0; i < result.size(); i++) { + if (count <= result.get(i).getParents().size()) { + pos = i; + break; + } + } + result.add(pos, pn); + } + } + return result; + } + + private Set getPathNodes(PathNode treeRoot) { + Set nodes = new HashSet(); + nodes.add(treeRoot); + for (NodePair np : treeRoot.getChildren()) { + nodes.addAll(getPathNodes(np.getConnection())); + } + return nodes; + } + + }