(currentNode, charIndex = 0)
| 30 | */ |
| 31 | deleteWord(word) { |
| 32 | const depthFirstDelete = (currentNode, charIndex = 0) => { |
| 33 | if (charIndex >= word.length) { |
| 34 | // Return if we're trying to delete the character that is out of word's scope. |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | const character = word[charIndex]; |
| 39 | const nextNode = currentNode.getChild(character); |
| 40 | |
| 41 | if (nextNode == null) { |
| 42 | // Return if we're trying to delete a word that has not been added to the Trie. |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | // Go deeper. |
| 47 | depthFirstDelete(nextNode, charIndex + 1); |
| 48 | |
| 49 | // Since we're going to delete a word let's un-mark its last character isCompleteWord flag. |
| 50 | if (charIndex === (word.length - 1)) { |
| 51 | nextNode.isCompleteWord = false; |
| 52 | } |
| 53 | |
| 54 | // childNode is deleted only if: |
| 55 | // - childNode has NO children |
| 56 | // - childNode.isCompleteWord === false |
| 57 | currentNode.removeChild(character); |
| 58 | }; |
| 59 | |
| 60 | // Start depth-first deletion from the head node. |
| 61 | depthFirstDelete(this.head); |
nothing calls this directly
no test coverage detected