将 trie class 方法转换为 trie 节点方法
Transform trie class method to trie node method
我的问题是如何将这个方法从 trie class 转换为不使用节点作为参数的节点 class 的方法,我知道我必须树上的第一个方法是这样的:
public void remove(String key)
{
root = root.remove(key, 0);
}
但是不知道如何将方法转化为节点class。
这是我想在不使用根作为方法参数的情况下,将树的方法转化为节点方法:
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;
}
我的问题是如何将这个方法从 trie class 转换为不使用节点作为参数的节点 class 的方法,我知道我必须树上的第一个方法是这样的:
public void remove(String key)
{
root = root.remove(key, 0);
}
但是不知道如何将方法转化为节点class。
这是我想在不使用根作为方法参数的情况下,将树的方法转化为节点方法:
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;
}