前缀树的各种操作

前缀树(Trie)

  • 前缀树,又称字典树。它是一棵 N 叉树。前缀树一般用于存储、查找字符串。

前缀树的操作

  • 运用前缀树可以进行增删查等操作

  • 可以利用字符串的公共前缀来减少查询时间,最大限度地减少不必要的字符串比较,查询效率比哈希树高。
    • public class TrieNode{
          public int end;
          public int pass;
          public TrieNode[] nexts;
          
          public TrieNode(){
              this.end = 0;
              this.pass = 0;
              this.nexts = new TrieNode[26];
          }
          
          public static class Trie{
              public TireNode root;
              public Trie(){
                  root = new TrieNode();
              }
          }
          
          public void insert(String word){
              if(word ==null){
                  return;
              }
              
              TrieNode node = root;
              int index;
              char[]chars = word.toCharArray();
              for(int i = 0;i<chars.length;i++){
                  index = chars[i] ='a';
                  if(node.nexts[index] == null){
                      node.nexts[index] = new TireNode();
                  }
                  node = node.nexts[index];
                  node.pass++;
              }
              node.end++;
          }
      }

    • 查询特定的字符串

    • public class TrieNode{
          public int end;
          public int pass;
          public TrieNode[] nexts;
          
          public TrieNode(){
              this.end = 0;
              this.pass = 0;
              this.nexts = new TrieNode[26];
          }
          
          public static class Trie{
              public TireNode root;
              public Trie(){
                  root = new TrieNode();
              }
          }
          public int search(String word){
              if(word == null){
                  return 0;
              }
              TireNode node = root;
              int index;
              char[]chars = word.toCharArray();
              for(int i = 0;i<chars.length;i++){
                  index = chars[i] - 'a';
                  if(node.nexts[index] == null){
                      return 0;
                  }
                  node = node.nexts[index];
              }
              return node.end;
          }
      }

    • 查询以......前缀的字符串

    • public class TrieNode{
          public int end;
          public int pass;
          public TrieNode[] nexts;
          
          public TrieNode(){
              this.end = 0;
              this.pass = 0;
              this.nexts = new TrieNode[26];
          }
          
          public static class Trie{
              public TireNode root;
              public Trie(){
                  root = new TrieNode();
              }
          }
          public int prefixNumber(String preWord){
              if(preWord == null){
                  return 0;
              }
              char[]chars = preWord.toCharArray();
              TireNode node = root;
              int index;
              for(int i = 0;i<chars.length;i++){
                  index = chars[i] -'a';
                  if(node.nexts[index] == null){
                      return 0;
                  }
                  node = node.nexts[index];
                 
              }
              return node.pass;
          }
      }

    • 删除字符串

    • public class TrieNode{
          public int end;
          public int pass;
          public TrieNode[] nexts;
          
          public TrieNode(){
              this.end = 0;
              this.pass = 0;
              this.nexts = new TrieNode[26];
          }
          
          public static class Trie{
              public TireNode root;
              public Trie(){
                  root = new TrieNode();
              }
          }
          public void delete(String word){
              if(word == null){
                  return;
              }
              char[]chars = word.toCharArray();
              int index;
              TireNode node = root;
              for(int i = 0;i<chars.length;i++){
                  index = chars[i]-'a';
                  if(--node.nexts[index].pass == 0){
                      return;
                  }
                  node = node.nexts[index];
              }
              node.end--;
          }
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值