日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

5 判断数据是否存在_Trie树实现:100亿URL中判断某个URL是否存在

發布時間:2025/4/16 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 5 判断数据是否存在_Trie树实现:100亿URL中判断某个URL是否存在 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前在頭條看到一篇《如何在100億URL中判斷某一個URL是否存在》,里面介紹的是使用布隆算法bloomfilter,我們也知道bloomfilter只能保證URL不存在。

在這里介紹一個解法,使用Trie數,這也是評論里有人提到的。Trie樹,又叫字典樹、前綴樹(Prefix Tree)、單詞查找樹 或 鍵樹,是一種多叉樹結構,如下圖:

圖片來源于維基

Trie樹的基本性質:

  • 根節點不包含字符,除根節點外的每一個子節點都包含一個字符;
  • 從根節點到某一個節點,路徑上經過的字符連接起來,為該節點對應的字符串;
  • 每個節點的所有子節點包含的字符互不相同;
  • 具體實現,如下:

    package com.practice.algorithm;import com.google.common.util.concurrent.*;import java.util.HashMap;import java.util.Map;import java.util.Random;import java.util.concurrent.*;public class Trie { public static void main(String[] args) throws Exception { TrieNode root = new TrieNode(); ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("pool_%d").setDaemon(true).build(); ExecutorService executor = new ThreadPoolExecutor(10,10 * 4, 30, TimeUnit.SECONDS,new ArrayBlockingQueue(1024),factory); ListeningExecutorService service = MoreExecutors.listeningDecorator(executor); CountDownLatch latch = new CountDownLatch(10); Long st = System.currentTimeMillis(); for(int i= 0;i < latch.getCount();i++) { long start = System.currentTimeMillis(); ListenableFuture task = service.submit(new Callable() { @Override public Boolean call() throws Exception { mockInsert(root); return true; } }); final int taskNo = i; Futures.addCallback(task, new FutureCallback() { @Override public void onSuccess(Boolean result) { System.out.println("task " + String.valueOf(taskNo) +" timeslap: "+ String.valueOf(System.currentTimeMillis()-start)); latch.countDown(); } @Override public void onFailure(Throwable t) { latch.countDown(); } }); } latch.await(); System.out.println("total timeslap: "+ String.valueOf(System.currentTimeMillis()-st)); System.out.println("============================ esult ==================================="); printTrie(root,""); } public static void printTrie(TrieNode node,String s) { if(node.leafCount > 0) { System.out.println("duplicate " + s + " count:" + String.valueOf(node.leafCount)); } if(node.children.isEmpty()) { return; } for (Map.Entry entry:node.children.entrySet()) { printTrie(entry.getValue(),s+entry.getKey()); } } public static void mockInsert(TrieNode root) { String[] prefixs = new String[] {"www.baidu.com

    總結

    以上是生活随笔為你收集整理的5 判断数据是否存在_Trie树实现:100亿URL中判断某个URL是否存在的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。