Java - Deleting word in trie (pseudo code) -
I am trying to figure out how to remove words from the tree. I have an implementation that uses a one-dimensional array on the node that holds the next letter for a word. I understand how I can get rid of a whole word, but not in the big words, which contain small words so that the word "bat", "war", "like" and "any" end of word ) And excluding "Battery", "Battlefield", "Ask" and "Any":
root / \ a bat * -tery * / \ | Ns * -k * l-e * -f-i-e-l-d * | Y * -w-h-o * Below I have trie implemented in this way:
public class TrieNode {protected char letter = ';; Secure TrieNode parentNode = null; Protected boolean full word = false; Protected Treeode [] Children = New Treeode [26]; 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 Full Word; }} Public square tree implies Iterable & lt; String & gt; {Private INFNOD; Private Ink numOfWords; Private triode root = new triode ('', empty); Public trio () {} public zero adworld (strings) {if (hurds (s)) returns; Int index = 0; Triode Itter = Route; While (index of & lt; s.length ()) {iferator.children [s.charAt (index) - 97] == faucet) {iterator.children [s.charAt (index) - 97] = New TrieNode (.charAt (index), iterator); numOfNodes ++; } Iterator = iterator. Children [s.charAt (index) - 97]; Index ++; If (index == s.length ()) {iterator.fullWord = true; numOfWords ++; }}} // Delete a public zero on this issue (strings) {if (s.length () == 0) returns; // If you make the method to check for the empty tray (! (Header (s) else {TrieNode iterator = root; ind index = 0; while (index of & lt; s.length ()) {if (! iterator.children [index] = faucet) {/ * What will be required to be placed here (in pseudo code) for this * account}}}} public boolean password (strings) {trinoad current = root; while (current! = Null) {for (int i = 0; i & lt; s.length (); i ++) {if (current.letter == '') return false; // error here maybe and current = present. [I];} if (current.fullWord == F) Truth return; Second false return;} The details are false;} Public iterator & lt; string & gt; iterator () {back to new beginner (s)) / <
The last position in the tree -
Find the word you want to delete in the tree. It will be indicated by the full word to change the status of the full word.
- < p> Is there any more character after the word Are they Exit.
-
Is there any child in the last letter? Exit.
- Remove the character from the tree - the only option at this point is to remove the word after the word or after that there are no children - We can not find any word or any child, which indicates a new word
'4. While the current character is not a word full and does not have any child: pointer = four -1; Delete four; char = pointer
'5. Exit
Comments
Post a Comment