getting the PathNode context wired together
This commit is contained in:
parent
1bb7b1083b
commit
20648a5dd3
3 changed files with 51 additions and 6 deletions
|
@ -17,6 +17,7 @@ import com.redhat.trie.Util;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Util util = new Util();
|
||||||
FileInputStream fis;
|
FileInputStream fis;
|
||||||
DataInputStream in;
|
DataInputStream in;
|
||||||
BufferedReader br;
|
BufferedReader br;
|
||||||
|
@ -51,7 +52,7 @@ public class App {
|
||||||
//System.out.println(contentList.toString());
|
//System.out.println(contentList.toString());
|
||||||
|
|
||||||
PathNode root = new PathNode();
|
PathNode root = new PathNode();
|
||||||
Util.makePathTree(contentList, root);
|
util.makePathTree(contentList, root);
|
||||||
|
|
||||||
Util.printTree(root, 0);
|
Util.printTree(root, 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,10 @@ public class PathNode {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NodeContext getContext() {
|
||||||
|
return this.ctx;
|
||||||
|
}
|
||||||
|
|
||||||
void addChild(NodePair cp) {
|
void addChild(NodePair cp) {
|
||||||
this.children.add(cp);
|
this.children.add(cp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,14 @@ import java.util.HashSet;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Util {
|
public class Util {
|
||||||
public static PathNode makePathTree(List<String> contents, PathNode parent) {
|
private NodeContext ctx;
|
||||||
PathNode endMarker = new PathNode();
|
|
||||||
|
public Util() {
|
||||||
|
this.ctx = new NodeContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathNode makePathTree(List<String> contents, PathNode parent) {
|
||||||
|
PathNode endMarker = new PathNode(this.ctx);
|
||||||
for (String path : contents) {
|
for (String path : contents) {
|
||||||
StringTokenizer st = new StringTokenizer(path, "/");
|
StringTokenizer st = new StringTokenizer(path, "/");
|
||||||
makePathForURL(st, parent, endMarker);
|
makePathForURL(st, parent, endMarker);
|
||||||
|
@ -98,7 +104,7 @@ public class Util {
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
private static void makePathForURL(StringTokenizer st, PathNode parent, PathNode endMarker) {
|
private void makePathForURL(StringTokenizer st, PathNode parent, PathNode endMarker) {
|
||||||
if (st.hasMoreTokens()) {
|
if (st.hasMoreTokens()) {
|
||||||
String childVal = st.nextToken();
|
String childVal = st.nextToken();
|
||||||
if (childVal.equals("")) {
|
if (childVal.equals("")) {
|
||||||
|
@ -116,7 +122,7 @@ public class Util {
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
PathNode next = null;
|
PathNode next = null;
|
||||||
if (st.hasMoreTokens()) {
|
if (st.hasMoreTokens()) {
|
||||||
next = new PathNode();
|
next = new PathNode(parent.getContext());
|
||||||
parent.addChild(new NodePair(childVal, next));
|
parent.addChild(new NodePair(childVal, next));
|
||||||
next.addParent(parent);
|
next.addParent(parent);
|
||||||
makePathForURL(st, next, endMarker);
|
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
|
// "equivalent" parents are merged
|
||||||
List<PathNode> parentResult = new ArrayList<PathNode>();
|
List<PathNode> parentResult = new ArrayList<PathNode>();
|
||||||
parentResult.addAll(location.getParents());
|
parentResult.addAll(location.getParents());
|
||||||
|
@ -225,5 +231,39 @@ public class Util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<PathNode> orderNodes(PathNode treeRoot) {
|
||||||
|
List<PathNode> result = new ArrayList<PathNode>();
|
||||||
|
|
||||||
|
// walk tree to make string map
|
||||||
|
Set<PathNode> 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<PathNode> getPathNodes(PathNode treeRoot) {
|
||||||
|
Set<PathNode> nodes = new HashSet<PathNode>();
|
||||||
|
nodes.add(treeRoot);
|
||||||
|
for (NodePair np : treeRoot.getChildren()) {
|
||||||
|
nodes.addAll(getPathNodes(np.getConnection()));
|
||||||
|
}
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue