特里树

实现 trie 数据结构trie数据结构的strider讲解class node{node [] node = new node[26];boolean flag;public node(){}public boolean contains

特里树

实现 trie 数据结构

trie数据结构的strider讲解

class node{
    node [] node = new node[26];
    boolean flag;
    public node(){

    }
    public boolean containskey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, node n){
        node[c-'a']  = n;
    }
    public node get(char c){
        return node[c-'a'];
    }
    public void setflag() {
        this.flag = true;
    }
    public boolean getflag(){
        return this.flag;
    }
}

class trie {
    node root;
    public trie() {
        root = new node();
    }
    //will take tc : o(len) of the word
    public void insert(string word) {
        node node  = root;
        for(int i =0;i<word.length if node.put node node.setflag take tc : o of the word public boolean search for i="0;i&lt;word.length();i++){" return false node.getflag prefix startswith true your trie object will be instantiated and called as such: obj="new" obj.insert param_2="obj.search(word);" param_3="obj.startswith(prefix);"><h2>
  
  
  trie数据结构二
</h2>

<p>奋斗者的解释,以便更好理解<br></p>

<pre class="brush:php;toolbar:false">import java.util.* ;
import java.io.*; 

class node {
    node node[] = new node[26];
    int endwith = 0;// will keep track of no. of words ending with this word
    int countprefix=0;// will keep track of no. of words starting with this word
    public node(){

    }
    public boolean containskey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, node n){
        node[c-'a'] = n;
    }
    public node get(char c){
        return node[c-'a'];
    }
    public void incrementcountprefix() {
        ++this.countprefix;
    }
    public void decrementcountprefix(){
        --this.countprefix;
    }
    public void incrementendwith(){
        ++this.endwith;
    }
    public void deleteendwith(){
        --this.endwith;
    }
    public int getcountprefix(){
        return this.countprefix;
    }
    public int getendwith(){
        return this.endwith;
    }


}
public class trie {
    node root;
    public trie() {
        // write your code here.
        root = new node();
    }

    public void insert(string word) {
        node node = root;
        for(int i =0;i<word.length if node.put node node.incrementcountprefix node.incrementendwith public int countwordsequalto word write your code here. for i="0;i&lt;word.length();i++){" else return node.getendwith will tell how many strings are with given countwordsstartingwith node.getcountprefix it starting void erase node.decrementcountprefix node.deleteendwith><h2>
  
  
  完整字符串
</h2>



<pre class="brush:php;toolbar:false">// tc : o(n*l)

import java.util.* ;
import java.io.*; 

class node{
    node[] node = new node[26];
    boolean flag;
    public node(){}
    public void put(char c , node n){
        node[c-'a'] = n;
    }
    public boolean containskey(char c){
        return node[c-'a']!=null;
    }
    public node get(char c){
        return node[c-'a'];
    }
    public void setend(){
        this.flag = true;
    }
    public boolean isend(){
        return this.flag;
    }
}

class trie{
    node root;
    public trie(){
        root = new node();
    }
    public boolean checkifprefixpresent(string s){
      node node = root;
      boolean flag= true;
      for(int i = 0;i<s.length char c="s.charat(i);" if return false node="node.get(c);" flag="flag" node.isend this will check the substring is also a string from list of strings line work here because any not present as in trie then s won be complete and we can only public void insert for i="0;i&lt;s.length();i++){" node.put new node.setend setting end current class solution static root completestring n all data structure : trie.insert out comeplete among s.length selection a.compareto b lexographically else s.compareto completestring.equals><h2>
  
  
  计算不同子串的个数
</h2>

<p>tc:在 <br> 中插入不同的唯一子字符串的 o(n^2)
trie数据结构<br></p>

<pre class="brush:php;toolbar:false">
import java.util.ArrayList;

public class Solution 
{
    Node root;
    static int count;
    public Solution(){
        root = new Node();
    }

    public static int countDistinctSubstrings(String s) 
    {
        count = 0;
        //    Write your code here.
        Solution sol = new Solution();
        for(int i =0;i




          

            
  

            
        

登录后复制

以上就是特里树的详细内容,更多请关注叮当号网其它相关文章!

文章来自互联网,只做分享使用。发布者:叮当,转转请注明出处:https://www.dingdanghao.com/article/659389.html

(0)
上一篇 2024-07-29 09:20
下一篇 2024-07-29 09:20

相关推荐

联系我们

在线咨询: QQ交谈

邮件:442814395@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信公众号