pulling out more of the tree objects
This commit is contained in:
parent
f788c73a8e
commit
7664a903cc
3 changed files with 118 additions and 4 deletions
|
@ -5,21 +5,21 @@ package com.hashbangbash.trie;
|
||||||
* an external incrementor to the node id
|
* an external incrementor to the node id
|
||||||
*/
|
*/
|
||||||
public class NodeContext {
|
public class NodeContext {
|
||||||
private long huffNodeId = 0;
|
private long nodeId = 0;
|
||||||
|
|
||||||
public NodeContext() {
|
public NodeContext() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeContext(long startId) {
|
public NodeContext(long startId) {
|
||||||
this.huffNodeId = startId;
|
this.nodeId = startId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return this.huffNodeId;
|
return this.nodeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long nextId() {
|
public long nextId() {
|
||||||
return this.huffNodeId++;
|
return this.nodeId++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
src/main/java/com/hashbangbash/trie/NodePair.java
Normal file
28
src/main/java/com/hashbangbash/trie/NodePair.java
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package com.hashbangbash.trie;
|
||||||
|
|
||||||
|
public class NodePair {
|
||||||
|
private String name;
|
||||||
|
private PathNode connection;
|
||||||
|
|
||||||
|
public NodePair(String name, PathNode connection) {
|
||||||
|
this.name = name;
|
||||||
|
this.connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathNode getConnection() {
|
||||||
|
return this.connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnection(PathNode connection) {
|
||||||
|
this.connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Name: " + name + ", Connection: " + connection.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
86
src/main/java/com/hashbangbash/trie/PathNode.java
Normal file
86
src/main/java/com/hashbangbash/trie/PathNode.java
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package com.hashbangbash.trie;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PathNode {
|
||||||
|
private long id = 0;
|
||||||
|
private List<NodePair> children = new ArrayList<NodePair>();
|
||||||
|
private List<PathNode> parents = new ArrayList<PathNode>();
|
||||||
|
private NodeContext ctx = null;
|
||||||
|
|
||||||
|
public PathNode() {
|
||||||
|
this(new NodeContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathNode(NodeContext ctx) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
this.id = this.ctx.nextId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addChild(NodePair cp) {
|
||||||
|
this.children.add(cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addParent(PathNode cp) {
|
||||||
|
if (!parents.contains(cp)) {
|
||||||
|
this.parents.add(cp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<NodePair> getChildren() {
|
||||||
|
return this.children;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<PathNode> getParents() {
|
||||||
|
return this.parents;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setParents(List<PathNode> parents) {
|
||||||
|
this.parents = parents;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addParents(List<PathNode> parents) {
|
||||||
|
for (PathNode pn : parents) {
|
||||||
|
addParent(pn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* same number of children with the same names for child nodes
|
||||||
|
*/
|
||||||
|
boolean isEquivalentTo(PathNode that) {
|
||||||
|
if (this.getChildren().size() != that.getChildren().size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (NodePair thisnp : this.getChildren()) {
|
||||||
|
boolean found = false;
|
||||||
|
for (NodePair thatnp : that.getChildren()) {
|
||||||
|
if (thisnp.getName().equals(thatnp.getName())) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
String parentList = "";
|
||||||
|
for (PathNode parent : parents) {
|
||||||
|
parentList += ": " + parent.getId();
|
||||||
|
}
|
||||||
|
parentList += "";
|
||||||
|
return "ID: " + id + ", Parents" + parentList + ", Children: " + children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue