C# 学习笔记:Trie树

问题描述:

#1014 : Trie树

方法1:

本方法参考了hihoCoder problem 1014:Trie Tree文档的C++版本。

using System;


namespace TrieProject
{
    public class Program
    {
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            TreeNode root = new TreeNode();
            while (n > 0)
            {
                TrieTreeController.BuildTrieTree(root, Console.ReadLine());
                n -= 1;
            }
            n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                Console.WriteLine(TrieTreeController.GetNodeCount(root, Console.ReadLine()));
                n -= 1;
            }
        }
    }
    public class TreeNode
    {
        public TreeNode()
        {
            this.size = 0;
            this.children = new TreeNode[26];
        }

        public int size;
        public TreeNode[] children;

        public int AddChild(char key)
        {
            int count = (int)key - 97;
            if (children[count] == null)
                children[count] = new TreeNode();
            return count;
        }
    }
    public static class TrieTreeController
    {
        public static void BuildTrieTree(TreeNode root, string word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                root = root.children[root.AddChild(word[i])];
                root.size++;
            }
        }
        public static int GetNodeCount(TreeNode root, string word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                int count = (int)word[i] - 97;
                if (root.children[count] == null)
                    return 0;
                else
                    root = root.children[count];
            }
            return root.size;
        }
    }
}

方法2:

using System;
using System.Collections.Generic;

namespace TrieProject
{
    public class Program
    {
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            TreeNode root = new TreeNode('\0');
            while (n > 0)
            {
                TreeNode.BuildTrieTree(root, Console.ReadLine());
                n -= 1;
            }
            n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                Console.WriteLine(TreeNode.GetNodeCount(root, Console.ReadLine()));
                n -= 1;
            }
        }
    }
    public class TreeNode
    {
        public TreeNode(char key)
        {
            this.key = key;
            this.size = 0;
            this.children = new List<TreeNode>();
        }

        char key;
        int size;
        IList<TreeNode> children;

        public char Key
        {
            get { return key; }
            set { key = value; }
        }
        public int Size
        {
            get { return size; }
        }
        public int Count
        {
            get { return this.children.Count; }
        }
        public IList<TreeNode> Children
        {
            get { return this.children; }
        }
        public TreeNode this[int index]
        {
            get { return this.children[index]; }
            set { this.children[index] = value; }
        }

        public TreeNode AddChild(char key)
        {
            TreeNode node = new TreeNode(key);
            this.children.Add(node);
            int i = Count - 2;
            while (i >= 0 && children[i].key > node.key)
            {
                children[i + 1] = children[i];
                i--;
            }
            children[i + 1] = node;
            return node;
        }

        private static string getTailString(string word)
        {
            return word.Length > 1 ? word.Substring(1) : null;
        }
        public static void BuildTrieTree(TreeNode root, string word)
        {
            if (!string.IsNullOrEmpty(word))
            {
                char target = word[0];
                bool found = false;
                for (int i = 0; i < root.Count; i++)
                {
                    if (root[i].Key == target)
                    {
                        found = true;
                        root[i].size++;
                        BuildTrieTree(root[i], getTailString(word));
                        break;
                    }
                }
                if (!found)
                {
                    var node = root.AddChild(target);
                    node.size++;
                    BuildTrieTree(node, getTailString(word));
                }
            }
        }

        public static int GetNodeCount(TreeNode root, string word)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(word))
            {
                char target = word[0];
                for (int i = 0; i < root.Count; i++)
                {
                    if (root[i].Key == target)
                    {
                        result += GetNodeCount(root[i], getTailString(word));
                    }
                    if (root[i].Key >= target)
                    {
                        break;
                    }
                }
            }
            else
            {
                result = root.Size;
            }
            return result;
        }
    }
}

    原文作者:Trie树
    原文地址: https://blog.csdn.net/lsj308/article/details/39938825
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞