注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

分享,态度 ·~~

—— 十年太长,五年;如果可以回到五年前,你最想对那时候的自己说什么?

 
 
 

日志

 
 

字符串快速查找(C#版)  

2010-01-19 11:25:35|  分类: DotNet |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

BinaryTree.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BinaryTree
{
    public  class BinaryNode
    {
        private  int fChildNum;
        private object fData;
        private ArrayList fChildPointer;
        private const int fArrayListNum = 26;

        public BinaryNode()
        {
            fChildNum = 0;
            fChildPointer = new ArrayList(fArrayListNum);
            InitialChildPointer();
        }

       public BinaryNode(object Data)
       {
           fChildNum = 0;
           fChildPointer = new ArrayList(fArrayListNum);
           InitialChildPointer();
           fData = Data;
       }
        private void InitialChildPointer()
        {
            for (int i = 0; i < fArrayListNum; i++)
            {
                fChildPointer.Insert(i, null);
            }
        }

        public int ChildNum
        {
            get
            {
                return fChildNum;
            }

            set
            {
                fChildNum = value;
            }
        }

        public int ArrayListNum
        {
            get
            {
                return fArrayListNum;
            }
        }

        public object Data
        {
            get
            {
                return fData;
            }
            set
            {
                fData = value;
            }
        }
        public void AddCount()
        {
            fChildNum++;

        }

        public void LessCount()
        {
            fChildNum--;
        }

        public void ResetCount()
        {
            fChildNum = 0;
        }

        public BinaryNode this[int index]
        {
            get
            {
              return (BinaryNode)fChildPointer[index];
            }
        }

       public void InsertAt(object Info, int index)
       {
          if( index >=0 && index < fArrayListNum)
          {
              fChildPointer.Insert(index,new BinaryNode(Info));
          }
       }

        public void AddBinaryNodePointer(BinaryNode fNode)
        {
            fChildPointer.Add(fNode);
        }

        public void RemoveAt(int index)
        {
            fChildPointer.RemoveAt(index);
        }

    }
 
    public delegate void VisitFunc(BinaryNode Node); //定义对树节点访问的委托,作为用户传入自己编写的VisitFunction的接口


    class BinaryTree
    {
        private const int Size = 150;  //数组大小定义
        private BinaryNode[] DataArray = new BinaryNode[Size];  //数据数组,存放所有的树节点
        private int DataArrayCount = 0;  //数组访问时使用的计数器
        private BinaryNode TempNode = new BinaryNode(); 
        private BinaryNode fHeadNode = new BinaryNode();//头结点,作为树与外界的接口,它的子结点是树的顶节点
        private string InputString = "";  //输入到树节点的数据
        private Queue objQueue = new Queue(Size); //层序遍历生成数组时用到的队列
       
       
      
        #region 公共函数
        public BinaryTree()
        {
            for (int i = 0; i < Size; i++)
            {
                DataArray[i] = new BinaryNode();
            }
        }

       public void PrintExplaintion()
        {
            BinaryNode objNode = new BinaryNode();
            Console.WriteLine("在树的创建过程中将用到以下符号,请记住他们的意义:");
            Console.WriteLine("*  代表一个空结点");
            Console.WriteLine("&  代表结点创建完毕");
            Console.WriteLine("#  代表树创建结束");
            Console.WriteLine("在输入时请不要输入这些特殊字符");
            Console.WriteLine();
            Console.WriteLine("注意:");
            Console.WriteLine("一个数据占一行");
            Console.WriteLine("请在输入前仔细检查树的结构是否合理,\n本树每个节点最多可有{0}个子树,最大可有{1}个节点\n本树用层序遍历发开发,所以在输入时请注意顺序",objNode.ArrayListNum, Size);
            Console.WriteLine();
        }

        /// <summary>
        /// 把字符串插入到树中,每个字符占一个节点
        /// </summary>
        /// <param name="InsertString"></param>
       public void InsertStringByChar(string InsertString)
        {
          char[] DataArray = new char[50];
          DataArray = InsertString.ToCharArray();
          BinaryNode ParentNode = new BinaryNode();
          ParentNode = fHeadNode;
          int Index;
          for (int i = 0; i < DataArray.GetLength(0); i++)
          {
              Index = GetIndexFromChar(DataArray[i]);
              if(ChildSearch(ParentNode,DataArray[i],Index) == true)
              {
                  ParentNode.AddCount();
                  ParentNode = ParentNode[Index];
              }
              else
              {
                  ParentNode.AddCount();
                  ParentNode.InsertAt(DataArray[i], Index);
                  ParentNode = ParentNode[Index];
              }
          }


        }

       public bool SearchStringByChar(String SearchString)
        {
            char[] DataArray = new char[50];
            DataArray = SearchString.ToCharArray();
            BinaryNode ParentNode = new BinaryNode();
            ParentNode = fHeadNode;
            int Index;
            bool flag = false;
            for (int i = 0; i < DataArray.GetLength(0); i++)
            {
                Index = GetIndexFromChar(DataArray[i]);
                if (ChildSearch(ParentNode, DataArray[i], Index) == true)
                {
                    ParentNode.AddCount();
                    ParentNode = ParentNode[Index];
                    flag = true;
                }
                else
                    flag = false;
            }
            return flag;
           
        }

public void PreOrderCreateNewTree()  //先序遍历生成树
        {
            Stack objStack = new Stack(Size);
            BinaryNode ParentNode = new BinaryNode();

            objStack.Push(fHeadNode);
           
            while(objStack.Count != 0)
            {
                ParentNode = (BinaryNode)objStack.Peek();

               
                    for (int i = 0; i < ParentNode.ArrayListNum; ++i)
                    {
                        InputString = Console.ReadLine();

                        switch (InputString)
                        {
                            case "*":
                                ParentNode.AddBinaryNodePointer(null);

                                if (ParentNode.ChildNum >= ParentNode.ArrayListNum)
                                    goto LabelA;
                                break;
                            case "&":
                                if (ParentNode.ChildNum <= ParentNode.ArrayListNum)
                                {
                                    for (i = ParentNode.ChildNum; i < ParentNode.ArrayListNum; i++)
                                    {
                                        ParentNode.AddBinaryNodePointer(null);
                                    }
                                    objStack.Pop();
                                }
                              
                                break;
                            case "#":
                                return;
                            default:
                                if (ParentNode.ChildNum < ParentNode.ArrayListNum)
                                {
                                    TempNode = DataArray[DataArrayCount];
                                    ++DataArrayCount;
                                    TempNode.Data = InputString;
                                    ParentNode.AddBinaryNodePointer(TempNode);
                                    ParentNode.AddCount();
                                    objStack.Push(TempNode);
                                    goto LabelB;
                                }
                               
                                  
                                break;
                        }
                    LabelA: continue;
                    LabelB: break;
                       
                    }
               
               
            }
        }

       
       public void CreateNewTree()  //使用层序遍历创建新树

        {
          
            BinaryNode ParentNode = new BinaryNode();
            int i;
            Console.WriteLine("请输入数据创建顶结点");

            InputString = Console.ReadLine();
           
            switch (InputString)
            {
                case "*":
                    fHeadNode.AddBinaryNodePointer(null);
                  
                    break;
                case "&":
                    if (fHeadNode.ChildNum <= fHeadNode.ArrayListNum)
                        for ( i = fHeadNode.ChildNum; i < fHeadNode.ArrayListNum; i++)
                        {
                            fHeadNode.AddBinaryNodePointer(null);
                        }
                   
                        break;
                case "#":
                    return;
                default:
                    if (fHeadNode.ChildNum <= fHeadNode.ArrayListNum)
                    {
                       TempNode = DataArray[DataArrayCount];
                       ++DataArrayCount;
                       TempNode.Data = InputString;
                       fHeadNode.AddBinaryNodePointer(TempNode);
                       fHeadNode.AddCount();
                       objQueue.Enqueue(TempNode);
                    }
                   
                    break;
            }

            Console.WriteLine();
            Console.WriteLine("请输入数据创建其他节点");
            Console.WriteLine();

            while (objQueue.Count != 0)
            {
                ParentNode = (BinaryNode)objQueue.Peek();

                for (i = 0; i <= ParentNode.ArrayListNum ;i++ )
                {
                    InputString = Console.ReadLine();

                    switch (InputString)
                    {
                        case "*":
                            ParentNode.AddBinaryNodePointer(null);
                          
                            if (ParentNode.ChildNum >= ParentNode.ArrayListNum)
                                goto LabelA;
                            break;
                        case "&":
                            if (ParentNode.ChildNum <= ParentNode.ArrayListNum)
                                for (i = ParentNode.ChildNum; i < ParentNode.ArrayListNum; i++)
                                {
                                    ParentNode.AddBinaryNodePointer(null);
                                }
                            else
                                goto LabelB;
                            break;
                        case "#":
                            return;
                        default:
                            if (ParentNode.ChildNum < ParentNode.ArrayListNum)
                            {
                                TempNode = DataArray[DataArrayCount];
                                ++DataArrayCount;
                                TempNode.Data = InputString;
                                ParentNode.AddBinaryNodePointer(TempNode);
                                ParentNode.AddCount();
                                objQueue.Enqueue(TempNode);
                            }
                            else
                                goto LabelB;
                            break;
                    }
                LabelA: continue;
                LabelB: break;
                }
               
                objQueue.Dequeue();

            }
           

        }

       public  void PreTransverBinaryTreeHelpFunc(BinaryNode ParentNode,VisitFunc action)
        {
         
              

                int count = 0;
                for (int i = 0; i < ParentNode.ArrayListNum; i++)
                {
                    if (ParentNode.ChildNum != 0)
                    {
                        TempNode = ParentNode[i];

                        if (TempNode == null)
                            continue;

                        else
                        {
                            action(TempNode);
                            PreTransverBinaryTreeHelpFunc(TempNode,action);
                           
                            count++;
                            if (count >= ParentNode.ChildNum)
                                break;
                        }
                    }
                }

 


            }

       public void PrintData()
            {
                Console.WriteLine();
                for (int i = 0; i < DataArray.Length; i++)
                {
                    TempNode = DataArray[i];
                    if (TempNode.Data == null)
                        break;
                    Console.WriteLine("{0}", Convert.ToString(TempNode.Data));
                }
            }

        /// <summary>
        /// 搜索父结点中是否有含有相应数据的子结点
        /// </summary>
        /// <param name="ParentNode"></param>
        /// <param name="Info"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private bool ChildSearch(BinaryNode ParentNode,object Info,int index)
        {
            if (ParentNode[index] == null)
                return false;
            return System.Object.Equals(ParentNode[index].Data, Info);
           
        }

        private int GetIndexFromChar(char index)
        {
            if (index > 64 && index < 91)
                return index - 65;
            else if (index > 96 && index < 123)
                return index - 97;
            return -1;
        }
        #endregion

        #region 属性
            public BinaryNode TopNode
        {
            get
            {
                return fHeadNode;
            }
        }

     
            #endregion
    }

  
}
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace BinaryTree
{
    class Program
    {
     
        public static int a = 5;
        static void Main(string[] args)
        {

            int x = 0;
            int nu = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            BinaryTree objBinaryTree = new BinaryTree();
            objBinaryTree.InsertStringByChar("TestString");
            objBinaryTree.InsertStringByChar("TinaSianChao");
            objBinaryTree.InsertStringByChar("TestStriniegc");
            Console.WriteLine("\n{0}",objBinaryTree.SearchStringByChar("Tesa"));
            /*objBinaryTree.PrintExplaintion();
            objBinaryTree.PreOrderCreateNewTree();
            objBinaryTree.CreateNewTree();
            objBinaryTree.PrintData();
            Console.WriteLine();
            DoSomeThing objDoSomeThing = new DoSomeThing();
            VisitFunc objVisitFunc = new VisitFunc(objDoSomeThing.VisitBinaryNode);

            objBinaryTree.PreTransverBinaryTreeHelpFunc(objBinaryTree.TopNode,objVisitFunc);*/
            sw.Stop();

            Console.WriteLine("\n程序运行时间是:{0}", sw.ElapsedMilliseconds.ToString());
            Console.WriteLine("sum=" + x.ToString());
            Console.ReadLine();
        }

      
    }

    class DoSomeThing
    {
        public void VisitBinaryNode(BinaryNode Node)
        {
            Console.Write("{0}   ", Convert.ToString(Node.Data));
           // Console.WriteLine("^_^  !");
           /* BinaryNode TempNode = new BinaryNode();
            if(Node.ChildNum != 0)
            for (int i = 0; i < Node.ArrayListNum; i++)
            {
                TempNode = Node[i];
                if (TempNode != null)
                    Console.Write("{0}   ",Convert.ToString(TempNode.Data));
            }*/
        }

    }
}

【from 林海听松 http://hi.baidu.com/xianchao/blog/item/7fa3163bcd1da3ea15cecb85.html

  评论这张
 
阅读(4410)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017