当前位置: 代码迷 >> C# >> 需要把字符串用Hashtable生成树状结构,也许Dictionary,Json也可以
  详细解决方案

需要把字符串用Hashtable生成树状结构,也许Dictionary,Json也可以

热度:80   发布时间:2016-05-05 04:24:59.0
需要把字符串用Hashtable生成树状结构,或者Dictionary,Json也可以
本帖最后由 u010479137 于 2015-03-26 17:45:52 编辑
需要把字符串用Hashtable生成下面的树状结构,或者Dictionary,Json也可以,必须是键值型的数据结构,因为这样方便我获取数据。


字符串如下:

阿爸
阿鼻
阿的平
阿弟
阿非利加洲
阿富汗人
哀辞
哀的美敦
哀歌
爱国人士
爱国心
爱国主义
爱好者



树状结构如下:


  爸
    end
  鼻
    end
  的
    平
      end
  弟
    end
  非
    利
      加
        洲
          end
  富
    汗
      人
        end

  辞
    end
  的
    美
      敦
        end
  歌
    end

  国
    人
      士
        end
    心
      end
    主
      义
        end
  好
    者
      end
------解决思路----------------------
给你写了一个demo和简单测试。自己定义一个 CTree 即可。
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var ss = @"
阿爸
阿鼻
阿的平
阿弟
阿非利加洲
阿富汗人
哀辞
哀的美敦
哀歌
爱国人士
爱国心
爱国主义
爱好者";
            var tree = new CTree();
            tree.Add(ss.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
            Console.Write(tree.ToString());
            Console.WriteLine(new string('-', 70));
            Console.WriteLine("查找“阿里”=\r\n{0}", tree.Search("阿里"));
            Console.WriteLine(new string('-', 70));
            Console.WriteLine("查找“阿富汗”=\r\n{0}", tree.Search("阿富汗"));
            Console.ReadKey();
        }
    }

    public class CTree
    {
        public Dictionary<char, CTree> Nodes = new Dictionary<char, CTree>();

        public void Add(string[] arr)
        {
            foreach (var s in arr)
                Add(s);
        }

        private void Add(string s)
        {
            CTree current = this;
            foreach (var c in s)
                current = current.Add(c);
        }

        private CTree Add(char c)
        {
            CTree t;
            if (this.Nodes.TryGetValue(c, out t))
                return t;

            t = new CTree();
            this.Nodes.Add(c, t);
            return t;
        }

        override public string ToString()
        {
            var sb = new StringBuilder();
            ToString(0, sb);
            return sb.ToString();
        }

        private const char prefix = '.';

        private void ToString(int p, StringBuilder sb)
        {
            if (Nodes.Count == 0)
            {
                sb.AppendLine(new string(prefix, p) + "end");
                return;
            }

            foreach (var k in this.Nodes.Keys)
            {
                sb.AppendLine(new string(prefix, p) + k);
                this.Nodes[k].ToString(p + 1, sb);
            }
        }

        public CTree Search(string str)
        {
            return Search(str, 0);
        }

        private CTree Search(string str, int index)
        {
            if (index >= str.Length)
                return this;

            CTree ret;
            if (this.Nodes.TryGetValue(str[index], out ret))
                return ret.Search(str, index + 1);

            return null;
        }
    }
}
  相关解决方案