/** * Provides methods to manipulate the Trie.
* The Trie only saves the root Node of which every Trie-Operation starts. */ public class Trie { /** * The root Node of the Trie. */ private Node root; /** * creates new Trie. */ public Trie() { this.root = new Node(); } /** * adds new Student key with * points points to the Trie. * @param key Student to add * @param points points of Student * @return true if add was successful or
* false if Student already exists */ public boolean add(String key, Integer points) { Node iterator = this.root; for (int i = 0; i < key.length(); i++) { if (iterator.getChild(key.charAt(i)) == null) { new Node(key.charAt(i), iterator); } iterator = iterator.getChild(key.charAt(i)); } if (iterator.getPoints() == null) { iterator.setPoints(points); return true; } else { return false; } } /** * deletes Student key in the Trie. * @param key Student to delete * @return true if delete was successful or
* false> if Student does not exist */ public boolean delete(String key) { Node hlp = this.root.find(key); if (hlp == null) { return false; } if (hlp.getPoints() != null) { hlp.deleteMe(); return true; } else { return false; } } /** * changes points points of Student key * in the Trie. * @param key Student to change * @param points points of Student to change * @return true if change was successful or
* false if Student does not exist */ public boolean change(String key, Integer points) { Node hlp = this.root.find(key); if (hlp == null) { return false; } if (hlp.getPoints() != null) { hlp.setPoints(points); return true; } else { return false; } } /** * Getter for points of Student key in the Trie. * @param key Student to search for * @return points of Student or
* null if Student does not exist */ public Integer points(String key) { Node hlp = this.root.find(key); if (hlp != null) { return hlp.getPoints(); } return null; } /** * Method to print the hole Trie. * @return string of the Trie in the requested format */ public String toString() { return this.root.toString(); } }