java - overriding iterator? -
In all honesty, I've spent a few days trying to understand it, but have come up very much Are less The goal is to see if a triangular node is next, if that is what it is.
Where it is said:
Public Iterator & lt; String & gt; Iterator () {Return to new Tritrateter ()}} It says to implement its own Iterator I use hasNext () to use a position in array + 1 , In which things are shaped, and numbers or nodes and children are also compared, and few have come, my most recent attempt was on the pattern of something (below) but did not work.
hasNext () {this.children.hasChildren return); / * Does not work * / / the public class implements notification system & lt; String & gt; {Public Trivator () {} Next in Public Boolean () {Return false; } Public string next () {return tap; } Public Zero removal () {new unsupported exception (exception); }} Here is also the trieNode class:
public class TrieNode {protected char letter = ';; Secure TrieNode parentNode = null; Protected boolean full word = false; Protected Treeode [] Children = New Treeode [26]; Protected int suffix = 0; Public triode (four letter, triangle parent node) {this.letter = letter; This.parentNode = parentNode; } Children of Public Boolean () {int index = 0; While (index & lt; children.length) {if (children [index]! = Null) {return true; } Index ++; } return false; } Public triode nodefire letter (four f) {return child [ca-97]; } Public boolean is ENDOfWord () {return letter == '*'; }} plus adding and deleting is as follows:
public zero adwars (strings) {if (header (s) (s) are); Int index = 0; Trinoad current = root; Four [] letters = s.toCharArray (); While (index & lt; s.length ()) {trianode child = present. Children [letters [index] - 97]; If (child == faucet) {child = new tripod (letter [index], current); child.prefixes ++; numOfNodes ++; } Present = child; Index ++; If (index == s.length ()) {current.fullWord = true; numOfWords ++; }}} Public Zero Deletion Word (strings) {if (s.length () == 0) returns; If (size () == 0) return; If (! Howard (s)) return; Trinoad current = root; (Four f: S. Techarrere ()) {Children of Trianode = present. [SARATAT (CA) - 97]; If (child.prefixes == 1) {child = null; Return; } And (child. Prefix -; current = child;}} current.fullWord = false;} Hurd (strings) in public boolean {if (size () == 0) returned incorrect; four [] letters = s.toCharArray ( ); int l = letters.length; Turned to turn = root; for int i; (i = 0; i & lt; l; i ++) {return (current == faucet) to return; current = present. [Characters [i] - 97];} if (i == l & amp; current == faucet) wrong return; if (current! = Tap & amp;; current.fullWord) return false; return true;} Please do not change the current implementation of the tree Your attempt to implement
<< code> with this.children.has child (); will not work because children is an array TrieNode , so there is no in the child () method. is (child) method is a method TrieNode , not its children array. You need to keep track of the current node, the position in the current node, and all the characters so far seen hasNext () should be correct if there is another word, which means Is that the node is a complete word or if it has any child that has not yet got the full word (but, I think the nodes of the cards are always full words, so the latter test can only happen When the node has children that are not yet Have gone.) You will need to decide whether you should return the short words before long time (do you come back a word from next () As soon as you reach the full word node, or after the arrival of its children?) You will need to maintain the stack for the nodes, so that you have landed in a child node, you return to your previous position in the parent node. You can. This is not a trivial task. I recommend starting with a very small set of words (like "A", "A", and "A"), and before that That you walk on words or more of three letters, be completely debuged.
(You should also consider what you intend to do if the tree is modified, then you are going through it. Standard java.util The class updates a counter on each revision, and checks that it has not changed in the iterator. If it is, then they throw a concurrent correction concept . Telling Iterators Faster-fast Ierators Is.)
Comments
Post a Comment