194 lines
4.9 KiB
Java
194 lines
4.9 KiB
Java
/**
|
|
* Provides methods to manipulate single Nodes.<br />
|
|
* A Node represents a char in the Trie and provides a children-array
|
|
* that saves following chars. <br />The Node also saves the parent Node.
|
|
* if a character is the last one of a string that is set it contains points.
|
|
* <br /><br />i.e. if "hans" is set with 5 points, <code>points</code>
|
|
* of 's' is 5.
|
|
*/
|
|
public class Node {
|
|
|
|
/**
|
|
* the points of a Student.
|
|
*/
|
|
private Integer points;
|
|
/**
|
|
* Node array for children.
|
|
*/
|
|
private Node[] children = new Node[26];
|
|
/**
|
|
* Parent Node.
|
|
*/
|
|
private Node parent;
|
|
/**
|
|
* The Character of the Node.
|
|
*/
|
|
private char ch;
|
|
|
|
/**
|
|
* Constructor for root Node.
|
|
*/
|
|
public Node() {
|
|
this.points = null;
|
|
this.parent = null;
|
|
this.ch = '+';
|
|
}
|
|
|
|
/**
|
|
* Constructor for Node with character <code>ch</code>.
|
|
* @param ch character of the Node
|
|
*/
|
|
private Node(char ch) {
|
|
this.points = null;
|
|
this.parent = null;
|
|
this.ch = ch;
|
|
}
|
|
|
|
/**
|
|
* Constructor for Node <code>ch</code>s with known parent
|
|
* Node <code>parent</code>.
|
|
* @param ch character of the Node
|
|
* @param parent Parent Node of the Node
|
|
*/
|
|
public Node(char ch, Node parent) {
|
|
Node hlp = new Node(ch);
|
|
hlp.parent = parent;
|
|
parent.setChild(ch, hlp);
|
|
}
|
|
|
|
/**
|
|
* Converts a character <code>ch</code> into the index for
|
|
* the children array.
|
|
* @param ch character to convert
|
|
* @return index for the children array
|
|
*/
|
|
private int convertChar(char ch) {
|
|
return ch - 'a';
|
|
}
|
|
|
|
/**
|
|
* Sets <code>child</code> with character <code>ch</code>
|
|
* in children array.
|
|
* @param ch the child's character
|
|
* @param child the child Node
|
|
*/
|
|
private void setChild(char ch, Node child) {
|
|
this.children[convertChar(ch)] = child;
|
|
}
|
|
|
|
/**
|
|
* Child getter.
|
|
* @param ch character of child to get
|
|
* @return <code>Node</code> of child
|
|
* or <code>null</code>, if child does not exist
|
|
*/
|
|
public Node getChild(char ch) {
|
|
return this.children[convertChar(ch)];
|
|
}
|
|
|
|
/**
|
|
* Searches the subtree for a string <code>key</code>.
|
|
* @param key string to search for
|
|
* @return <code>Node</code> of found key or <code>null</code>
|
|
*/
|
|
public Node find(String key) {
|
|
int i = 1;
|
|
Node iterator = this.children[convertChar(key.charAt(0))];
|
|
while (iterator != null && i < key.length()) {
|
|
iterator = iterator.children[convertChar(key.charAt(i))];
|
|
i++;
|
|
}
|
|
if (i == key.length()) {
|
|
return iterator;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset <code>points</code> of Node.
|
|
*/
|
|
public void deleteMe() {
|
|
this.points = null;
|
|
cleanup();
|
|
}
|
|
|
|
/**
|
|
* Searches for children.
|
|
* @return <code>true</code>, if child was found or<br />
|
|
* <code>false</code>, if child not available
|
|
*/
|
|
private boolean hasChild() {
|
|
for (int i = 0; i < this.children.length; i++) {
|
|
if (this.children[i] != null) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Deletes unneeded Nodes after Student deletion.
|
|
*/
|
|
private void cleanup() {
|
|
if (hasChild()) {
|
|
return;
|
|
}
|
|
Node iterator = this.parent;
|
|
char isChild = this.ch;
|
|
while (iterator.points == null && iterator.parent != null) {
|
|
|
|
//delete edge
|
|
iterator.children[convertChar(isChild)] = null;
|
|
|
|
//if another child is set, the Node is still needed
|
|
if (iterator.hasChild()) {
|
|
return;
|
|
}
|
|
isChild = iterator.ch;
|
|
iterator = iterator.parent;
|
|
}
|
|
|
|
//delete child Node in root
|
|
iterator.children[convertChar(isChild)] = null;
|
|
|
|
}
|
|
|
|
/**
|
|
* Method to print a Node and his subtree.<br />
|
|
* Recursive self-calling every child of the Node.
|
|
* @return <code>string</code> of Node and subtree
|
|
*/
|
|
public String toString() {
|
|
System.out.print(this.ch);
|
|
if (this.points != null) {
|
|
System.out.print("[" + this.points + "]");
|
|
}
|
|
if (this.hasChild()) {
|
|
System.out.print("(");
|
|
for (int i = 0; i < this.children.length; i++) {
|
|
if (this.children[i] != null) {
|
|
this.children[i].toString();
|
|
}
|
|
}
|
|
System.out.print(")");
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/**
|
|
* Setter for points of Node.
|
|
* @param points to set
|
|
*/
|
|
public void setPoints(Integer points) {
|
|
this.points = points;
|
|
}
|
|
|
|
/**
|
|
* Getter for Points of Node.
|
|
* @return <code>points</code> of Node
|
|
*/
|
|
public Integer getPoints() {
|
|
return this.points;
|
|
}
|
|
} |