字典树(前缀树)的实现(添加、删除、查找、查找前缀)

//字典树(前缀树)的实现(添加、删除、查找、查找前缀)
public class DicTree{
	public  TrieNode root; //定义根节点

	//字典树构造函数初始化
	public DicTree()
	{
		root=new TrieNode();
	}
	//字典树节点的定义
	 class TrieNode{

		public int path;
		public int end;
		public TrieNode[]map;
         //构造函数实现初始化
		public TrieNode()
		{
			 path=0;
			 end=0;
			 map=new TrieNode[26];
		}

	}

	//(1)添加
	public  void insert(String word)
	{
       if(word==null)
       {
       	 return;
       }
       //字符串转数组
       char[]chs=word.toCharArray();
       TrieNode node=root;
       int index=0;
       for(int i=0;i<chs.length;i++)
       {
       	   index=chs[i]-'a';
       	   if(node.map[index]==null)
       	   {
       	   	  node.map[index]=new TrieNode();
       	   }
       	   node=node.map[index];
       	   node.path++;
       }
       node.end++;
	}
   
    //(2)删除
    public  void delete(String word)
    {  
    	//字典树中是否含有word单词
       if(search(word))
       {
       	 //字符串转数组
       char[]chs=word.toCharArray();
       TrieNode node=root;
       int index=0;
       for(int i=0;i<chs.length;i++)
       {
       	   index=chs[i]-'a';
       	   if(node.map[index].path--==1)
       	   {
       	   	  node.map[index]=null;
       	   	  return ;
       	   }
       	   node=node.map[index];
       	   
       }
       node.end--;
       } 
    }
    
    //(3)查找
    public  boolean  search(String word)
    {
         if(word==null)
         {
         	return false;
         } 
        //字符串转数组
       char[]chs=word.toCharArray();
       TrieNode node=root;
       int index=0;
       for(int i=0;i<chs.length;i++)
       {
       	   index=chs[i]-'a';
       	   if(node.map[index]==null)
       	   {
       	   	 
       	   	  return  false;
       	   }
       	   node=node.map[index];
       	   
       }
         return node.end!=0;
    }
  
    //(4)查找前缀
    public  int PreSearch(String pre)
    {
        if(pre==null)
        {
        	return 0;
        }

    	char[]chs=pre.toCharArray();
    	int index=0;
    	TrieNode node=root;
    	for(int i=0;i<chs.length;i++)
    	{
             index=chs[i]-'a';
             if(node.map[index]==null)
             {
             	return 0;
             }
             node=node.map[index];
    	}
       return node.path;
    }

	public static void main(String[]args)
	{
		//System.out.println("Hello");
		DicTree dicTree=new DicTree();
		String str1="abc";
		String str2="abcd";
		String str3="abd";
		String str4="b";
		String str5="bcd";
		String str6="efg";
		String str7="hik";

		dicTree.insert(str1);
		dicTree.insert(str2);
		dicTree.insert(str3);
		dicTree.insert(str4);
		dicTree.insert(str5);
		dicTree.insert(str6);
		dicTree.insert(str7);

		System.out.println(dicTree.search(str3));
        dicTree.delete(str3);
        System.out.println(dicTree.search(str3));

        System.out.println(dicTree.PreSearch(str3));
        System.out.println(dicTree.PreSearch(str1));


      
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值