Tire implementation in Java

Source: Internet
Author: User

Tire implementation in Java
Trie, also known as word search tree or key tree, is a tree structure. A typical application is to count and sort a large number of strings (but not limited to strings), so it is often used by the search engine system for text word frequency statistics. It has the following advantages: minimizes unnecessary string comparisons and improves query efficiency than hash tables. It has three basic properties: the root node does not contain characters, and each node except the root node only contains one character. From the root node to a node, the character passing through the path is connected to the corresponding string of the node. All subnodes of each node contain different characters. The following figure shows the expression of Trie. Each side represents a character. If it ends, it is represented by an asterisk. In this Trie structure, we have the following strings, such as do, dork, and dorm, but there is no ba or sen in Trie, because at the end of a, and n, there is no ending symbol (asterisk ). With such a data structure, we can use it to store a dictionary. Is it very convenient to query and change the dictionary to see if there are corresponding words? We can also make smart prompts. We store words that have been searched by users in Trie. When users enter a word, we can automatically prompt. For example, when users enter ba, we will automatically prompt bat and baii. now we will discuss the implementation of Trie. First, we define an Abstract Trie, which stores a Node. There are two operations in this class: insert and query. The specific implementation is later. Implement the Node class: copy the code package com. yydcdut; import java. util. optional list; public class Node {char content; // contains the content in node boolean isEnd; // whether it is the end of a word int count; // The number of branches under the letter of this word in the descending list <Node> childList; // sublist/*** constructor * @ param c letter */public Node (char c) {childList = new shortlist <Node> (); isEnd = false; content = c; count = 0;}/*** traverses whether the letter exists in the member list of the node. * @ Param c the letter * @ return returns the next node if any. if no Node exists, null */public node subNode (char c) {if (childList! = Null) {for (Node eachChild: childList) {if (eachChild. content = c) {return eachChild ;}} return null ;}} copy the code implementation: copy the code package com. yydcdut; public class Main {private Node root; // root/*** constructor to generate the root */public Main () {root = new Node ('');} /*** insert a function to first determine whether the word exists (by using each letter). If not, check whether the letter exists in sequence, * if you have this subtitle, continue to judge the next one. If you do not have this letter, create a node object for this letter, put it in * into list of the last letter * @ param word the word to be inserted */public Void insert (String word) {// if it is found, return if (search (word) = true) return; Node current = root; for (int I = 0; I <word. length (); I ++) {Node child = current. subNode (word. charAt (I); if (child! = Null) {current = child;} else {current. childList. add (new Node (word. charAt (I); current = current. subNode (word. charAt (I);} // number of branches under a word + + current. count ++;} // end current in the last letter of the word. isEnd = true;}/*** search function to determine whether a word exists through the word * @ param word to be judged * @ return returns true, no false */public boolean search (String word) {Node current = root; for (int I = 0; I <word. length (); I ++) {if (current. subNode (word. c HarAt (I) = null) return false; else current = current. subNode (word. charAt (I);} // determines whether the letter of the word ends in the dictionary if (current. isEnd = true) return true; else return false;}/*** delete a function. First, determine whether the word exists. If the word does not exist, it jumps out and deletes it, the count of each word minus 1 * @ param word the word to be deleted */public void deleteWord (String word) {if (search (word) = false) return; node current = root; for (char c: word. toCharArray () {Node child = current. subNode (C); if (child. count = 1) {current. childList. remove (child); return;} else {child. count --; current = child;} current. isEnd = false;} public static void main (String [] args) {Main trie = new Main (); trie. insert ("ball"); trie. insert ("bils"); trie. insert ("sense"); System. out. println (trie. search ("bils"); System. out. println (trie. search ("ba"); trie. deleteWord ("bils"); System. out. println (trie. s Earch ("bils"); System. out. println (trie. search ("ball") ;}} time complexity analysis of the copied code: For insert, if the length of the inserted String is k, query each character, we can query up to 26 times (up to 26 letters) in the child Register list. Therefore, the complexity is O (26 * k) = O (k ). for search, the complexity is the same.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.