0%

前缀树

前缀树问题

  • 定义: 通过一组字符串构建的树叫做前缀树
  • 构成:
    1. 遍历每一个字符串, 从根节点判断, 是否有一条路径上的字符是字符串的字符
    2. 如果有, 复用这条路径, 没有, 新建一条路径
    3. 注意字符是在节点与节点的联线上而不是节点上
    4. 节点上记录这有多少个字符串是以上一条路径结尾的
    5. 节点上同时记录这有多少字符串经过这个节点
  • 代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    class TrieTree
    {
    public class TreeNode
    {
    public TreeNode()
    {
    this.path = 0;
    this.end = 0;
    this.nexts = new Dictionary<char, TreeNode>();
    }
    public int Path
    {
    get => this.path;
    set => this.path = value;
    }

    public int End
    {
    get => this.end;
    set => this.end = value;
    }

    public Dictionary<char, TreeNode> Nexts
    {
    get => this.nexts;
    private set => this.nexts = value;
    }

    private int path;
    private int end;
    private Dictionary<char, TreeNode> nexts;
    }
    public TreeNode Root
    {
    get => this.root;
    private set => this.root = value;
    }

    public TrieTree()
    {
    this.root = new TreeNode();
    }

    public void Insert(String word)
    {
    if (word == null)
    {
    return;
    }
    this.root.Path++;
    TreeNode node = this.root;
    foreach (char ch in word)
    {
    if (!node.Nexts.ContainsKey(ch))
    {
    node.Nexts[ch] = new TreeNode();
    }
    node = node.Nexts[ch];
    node.Path++;
    }
    node.End++;
    }

    public void Delete(String word)
    {
    if (this.Search(word) == 0)
    {
    return;
    }
    this.root.Path--;
    TreeNode node = this.root;
    foreach (char ch in word)
    {
    TreeNode next = node.Nexts[ch];
    if (next.Path - 1 > 0)
    {
    next.Path--;
    node = next;
    }
    else
    {
    node.Nexts.Remove(ch);
    break;
    }
    }
    }
    public int Search(String word)
    {
    if (word == null)
    {
    return 0;
    }
    TreeNode node = root;
    foreach (char ch in word)
    {
    if (!node.Nexts.ContainsKey(ch))
    {
    return 0;
    }
    node = node.Nexts[ch];
    }
    return node.End;
    }

    public int PreFixNumber(String word)
    {
    if (word == null)
    {
    return 0;
    }
    TreeNode node = root;
    foreach (char ch in word)
    {
    if (!node.Nexts.ContainsKey(ch))
    {
    return 0;
    }
    node = node.Nexts[ch];
    }
    return node.Path;
    }

    private TreeNode root;

    }