Trie树基本操作(Java)

目录

结点类

插入

删除

前缀串搜索

完整代码及测试主函数


结点类

包含key和value及一个子节点表,初始化只需要传入key并创建子结点表。

private static class Node{
        char key;
        String value;
        Map<Character, Node> children;
        Node(char key){
            this.key=key;
            this.children = new HashMap<>();
        }
    }

插入

遍历待插入字符串的每个字符,用curr指向root。如果curr子结点key不包含该字符,则插入新结点,并将curr指向该节点,继续遍历下个字符。遍历结束后,将curr的value设为待插入字符串。

public void insert(String value){
        Node cur=root;
        for (char c:value.toCharArray()){
            if (!cur.children.containsKey(c))
                cur.children.put(c,new Node(c));
            cur=cur.children.get(c);
        }
        cur.value=value;
    }

删除

遍历待删除字符串,如果遇到value值不为空或有两个及以上子结点的结点,则将res指向该结点,并记录下一个key,遍历结束之后删除res对应key的子结点。

public void delete(String value){
        Node res = root,cur=root;
        char key = value.charAt(0);
        int flag=0;
        for (char c:value.toCharArray()){
            if (flag==1)key=c;flag=0;
            cur=cur.children.get(c);
            if (cur.children.size()>=2||cur.value!=null){
                if (cur.value!=null)if (cur.value.compareTo(value)==0)break;
                res=cur;
                flag=1;
            }
        }
        res.children.remove(key);
    }

前缀串搜索

遍历前缀至最后一个字符,对该字符对应结点本身及其子树进行遍历,value不为空即输出。

public int dfs(Node root,int flag){
        if(root == null) return 0;
        if (root.value!=null){System.out.println(root.value);flag=1;}
        for(Map.Entry<Character, Node> entry : root.children.entrySet()){
            dfs(root.children.get(entry.getKey()),flag);
        }
        return flag;
    }
    public void getValue(String prefix){
        Node cur=root;
        for (char c:prefix.toCharArray()){
            cur=cur.children.get(c);
        }
        int flag=dfs(cur,0);
        if (flag==0)System.out.println("Nothing was found.");
    }

完整代码及测试主函数

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Trie {
    public static void main(String[] args) throws FileNotFoundException {
        Trie a=new Trie();
        Scanner sc = new Scanner(new File("dictionary.txt"));
        while (sc.hasNext()){
            a.insert(sc.nextLine());
        }
        a.delete("aahed");
        a.insert("aaa");
        a.getValue("aa");
        a.getValue("system");
        a.getValue("systemm");
    }
    private static class Node{
        char key;
        String value;
        Map<Character, Node> children;
        Node(char key){
            this.key=key;
            this.children = new HashMap<>();
        }
    }
    private final Node root;
    public Trie(){this.root=new Node(' ');}
    public void insert(String value){
        Node cur=root;
        for (char c:value.toCharArray()){
            if (!cur.children.containsKey(c))
                cur.children.put(c,new Node(c));
            cur=cur.children.get(c);
        }
        cur.value=value;
    }
    public void delete(String value){
        Node res = root,cur=root;
        char key = value.charAt(0);
        int flag=0;
        for (char c:value.toCharArray()){
            if (flag==1)key=c;flag=0;
            cur=cur.children.get(c);
            if (cur.children.size()>=2||cur.value!=null){
                if (cur.value!=null)if (cur.value.compareTo(value)==0)break;
                res=cur;
                flag=1;
            }
        }
        res.children.remove(key);
    }
    public int dfs(Node root,int flag){
        if(root == null) return 0;
        if (root.value!=null){System.out.println(root.value);flag=1;}
        for(Map.Entry<Character, Node> entry : root.children.entrySet()){
            dfs(root.children.get(entry.getKey()),flag);
        }
        return flag;
    }
    public void getValue(String prefix){
        Node cur=root;
        for (char c:prefix.toCharArray()){
            cur=cur.children.get(c);
        }
        int flag=dfs(cur,0);
        if (flag==0)System.out.println("Nothing was found.");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值