my question is how to transform this method from the trie class into a method of the node class that doesn't use the node as a parameter, I know that I have to make a first method on the tree like this:
public void remove(String key)
{
root = root.remove(key, 0);
}
But I don't know how to make the transformation of the method into the node class.
This is the method of the tree that I want to transform into a node method without using root as parameter of the method:
static TrieNode remove(TrieNode root, String key, int depth)
{
// If tree is empty
if (root == null)
return null;
// If last character of key is being processed
if (depth == key.length()) {
// This node is no more end of word after
// removal of given key
if (root.isEndOfWord)
root.isEndOfWord = false;
// If given is not prefix of any other word
if (isEmpty(root)) {
root = null;
}
return root;
}
// If not last character, recur for the child
// obtained using ASCII value
int index = key.charAt(depth) - 'a';
root.children[index] =
remove(root.children[index], key, depth + 1);
// If root does not have any child (its only child got
// deleted), and it is not end of another word.
if (isEmpty(root) && root.isEndOfWord == false){
root = null;
}
return root;
}
public TrieNode remove(String key, int depth)
{
// If last character of key is being processed
if (depth == key.length()) {
// This node is no more end of word after
// removal of given key
if (this.isEndOfWord)
this.isEndOfWord = false;
// If given is not prefix of any other word
if (isEmpty(this)) {
return null;
}
return this;
}
// If not last character, recur for the child
// obtained using ASCII value
int index = key.charAt(depth) - 'a';
this.children[index] =
this.children[index].remove(key, depth + 1);
// If root does not have any child (its only child got
// deleted), and it is not end of another word.
if (isEmpty(this) && this.isEndOfWord == false){
return null;
}
return this;
}