/** * Provides methods to manipulate single Nodes.
* A Node represents a char in the Trie and provides a children-array * that saves following chars.
The Node also saves the parent Node. * if a character is the last one of a string that is set it contains points. *

i.e. if "hans" is set with 5 points, points * 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 ch. * @param ch character of the Node */ private Node(char ch) { this.points = null; this.parent = null; this.ch = ch; } /** * Constructor for Node chs with known parent * Node parent. * @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 ch 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 child with character ch * 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 Node of child * or null, if child does not exist */ public Node getChild(char ch) { return this.children[convertChar(ch)]; } /** * Searches the subtree for a string key. * @param key string to search for * @return Node of found key or null */ 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 points of Node. */ public void deleteMe() { this.points = null; cleanup(); } /** * Searches for children. * @return true, if child was found or
* false, 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.
* Recursive self-calling every child of the Node. * @return string 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 points of Node */ public Integer getPoints() { return this.points; } }