当前位置:   article > 正文

Trie_trie数据结构特点

trie数据结构特点

今天照例,写了一发hihocoder,是关于Trie字典树的,于是顺便再复习下字典树吧。


Trie是一种树,非常实用的一种树,使用Trie还可以写AC自动机。在字符串处理上面有非常好的效率。

首先介绍下字典树的定义。

在使用C++的时候,可以这么定义字典树

  1. #define N 26 //字典树中的字母数量
  2. typedef struct node{
  3. int num; //用于计数
  4. char val; //表示这个节点代表的字符是什么
  5. int ch[N]; //儿子节点
  6. }
由于最近我开始深入学习Java了,所以也写一下Java的定义:

  1. class Trie{
  2. int num = 0;
  3. char val = '\0';
  4. Trie parent = null;
  5. ArrayList<Trie> ch = new ArrayList<Trie>();
  6. public Trie( char c, Trie p ) {
  7. this.val = c;
  8. this.parent = p;
  9. }
  10. public void add( Trie child ) {
  11. ch.add(child);
  12. }
  13. public int getValue() {
  14. return this.num;
  15. }
  16. public int getChildNum() {
  17. return ch.size();
  18. }
  19. }


因为Java没有结构体,所以要使用类,正好可以在里面把Trie要使用的一些方法顺便定义了。


当明确了字典树的定义之后,其实字典树就很直观了。就是一种特殊的树,其每一个节点代表一个字符,从根节点到该节点的路劲上的所有节点按顺序组合在一起就是一个字符串。而每一个节点的num值,就是代表以到这个节点的字符串为前缀的字符串有多少个。


大概结构就是这样...详细的关于树的内容....可以参考二叉树...

概念不难,关键在于灵活运用。


下面给出用python3写的Trie...

  1. class Trie(object):
  2. """docstring for Trie"""
  3. def __init__(self, c, p):
  4. self.num = 0
  5. self.val = c
  6. self.ch = []
  7. self.parent = p
  8. def add( self, child ):
  9. self.ch.append(child)
  10. def getValue( self ):
  11. return self.num
  12. def getChildSize( self ):
  13. return len( self.ch )
  14. root = Trie('$', -1)
  15. def Insert( string ):
  16. p = root
  17. for s in string:
  18. flag = False
  19. for child in p.ch:
  20. if child.val == s:
  21. p = child
  22. p.num += 1
  23. flag = True
  24. break
  25. if not flag:
  26. q = Trie( s, p )
  27. p.add(q)
  28. p = q
  29. p.num += 1
  30. #以字符串string为前缀的字符串个数
  31. def Query( string ):
  32. p = root
  33. for s in string:
  34. flag = False
  35. for child in p.ch:
  36. if child.val == s:
  37. p = child
  38. flag= True
  39. break
  40. if flag == False:
  41. return 0
  42. return p.getValue()
  43. n = int( input("please input n: ") )
  44. for i in range(n):
  45. string = input("please input string: ")
  46. Insert( string )
  47. m = int( input("please input m: ") )
  48. for i in range(m):
  49. string = input("please input query string: ")
  50. print ( Query(string) )



下面附上hihocoder-1014-Trie树的ac代码:

  1. import java.io.IOException;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.util.ArrayList;
  7. public class Main {
  8. public static void main(String[] args) throws IOException {
  9. // TODO Auto-generated method stub
  10. BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  11. BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( System.out ) );
  12. Trie root= new Trie('$', null);
  13. int n = Integer.parseInt( reader.readLine() );
  14. for( int i = 0; i < n; ++i ) {
  15. String str = reader.readLine();
  16. Trie p = root;
  17. int len = str.length();
  18. for( int j = 0; j < len; ++j ) {
  19. boolean flag = false;
  20. int cur = p.getChildNum();
  21. for( int k = 0; k < cur; ++k ) {
  22. Trie tmp = p.ch.get(k);
  23. if( tmp.val == str.charAt(j) ) {
  24. p = tmp;
  25. ++p.num;
  26. flag = true;
  27. break;
  28. }
  29. }
  30. if( !flag ) {
  31. Trie node = new Trie( str.charAt(j), p );
  32. p.add(node);
  33. p = node;
  34. ++p.num;
  35. }
  36. }
  37. }
  38. int m = Integer.parseInt( reader.readLine() );
  39. for( int i = 0; i < m; ++i ) {
  40. String str = reader.readLine();
  41. int ans = 0;
  42. Trie p = root;
  43. int len = str.length();
  44. for( int j = 0; j < len; ++j ) {
  45. boolean flag = false;
  46. int cur = p.getChildNum();
  47. for( int k = 0; k < cur; ++k ) {
  48. Trie tmp = p.ch.get(k);
  49. if( tmp.val == str.charAt(j) ) {
  50. p = tmp;
  51. ans = p.getValue();
  52. flag = true;
  53. break;
  54. }
  55. }
  56. if( !flag ) {
  57. ans = 0;
  58. break;
  59. }
  60. }
  61. writer.write(ans + "\r\n");
  62. writer.flush();
  63. }
  64. }
  65. }
  66. class Trie{
  67. int num = 0;
  68. char val = '\0';
  69. Trie parent = null;
  70. ArrayList<Trie> ch = new ArrayList<Trie>();
  71. public Trie( char c, Trie p ) {
  72. this.val = c;
  73. this.parent = p;
  74. }
  75. public void add( Trie child ) {
  76. ch.add(child);
  77. }
  78. public int getValue() {
  79. return this.num;
  80. }
  81. public int getChildNum() {
  82. return ch.size();
  83. }
  84. }
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号