前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 特别遍历树

原 特别遍历树

作者头像
魂祭心
发布2018-05-17 17:37:33
7000
发布2018-05-17 17:37:33
举报
文章被收录于专栏:魂祭心
代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TreeTest
{
    class Program
    {
        static List<Connect<Letter>>  letter = new List<Connect<Letter>>();
        static void Main(string[] args)
        {
            Init();
            List<Connect<Letter>> tt = Find("A");
            Stack<Letter> st = new Stack<Letter>();
            Search(st, tt.ElementAt(0).Item1);
            List < Letter > s=st.Reverse<Letter>().ToList<Letter>();
            Console.WriteLine("******************************");
            s.ForEach((o) => { o.write(); });
        }
        /// <summary>
        /// 递归搜索树
        /// </summary>
        /// <param name="st"></param>
        /// <param name="t"></param>
        static void Search(Stack<Letter> st, Letter t)
        {
            List<Connect<Letter>> fs = Fathers(t);
            List<Connect<Letter>> cs = Children(t);
            if (fs.Count > 0)
            {//有父亲 
                //查看是否存在未读取的父亲
                bool allread = isAllRead(fs);
                if (!allread)
                {
                    foreach (Connect<Letter> tt in fs)
                    {//迭代父节点
                        if (tt.Item1.IsRead == false)
                        {
                            Search(st, tt.Item1);
                        }
                    }
                }
                else
                {//父亲已经全部读取
                    if (!st.Contains(t))
                    {
                        t.write();
                        st.Push(t);
                    }
                }
            }
            else
            {//无父亲 入栈
                if (!st.Contains(t))
                {
                    t.write();
                    st.Push(t);
                }
            }

            if (cs.Count > 0)
            {
                //有孩子 遍历孩子
                foreach (Connect<Letter> tt in cs)
                {//迭代父节点
                    if(tt.Item2.IsRead==false)
                    Search(st, tt.Item2);
                }
            }
            else
            {//无孩子 结束迭代
                return;
            }
        }
        static Boolean isAllRead(List<Connect<Letter>>  tt)
        {
            foreach (Connect<Letter> t in tt)
            {//迭代父节点
                if (t.Item1.IsRead == false)
                {
                    return false;
                }
            }
            return true;
        }
        static void Init()
        {
            Letter A = new Letter("A");
            Letter B = new Letter("B");
            Letter C = new Letter("C");
            Letter D = new Letter("D");
            Letter E = new Letter("E");
            Letter F = new Letter("F");
            Letter G = new Letter("G");
            Letter H = new Letter("H");
            Letter I = new Letter("I");
            Letter J = new Letter("J");
            Letter K = new Letter("K");
            Letter L = new Letter("L");
            Letter M = new Letter("M");
            Letter N = new Letter("N");
            Letter O = new Letter("O");
            Letter P = new Letter("P");
            Letter Q = new Letter("Q");
            Letter R = new Letter("R");


            letter.Add(new Connect<Letter>(A, B));
            letter.Add(new Connect<Letter>(C, B));
            letter.Add(new Connect<Letter>(B, D));
            letter.Add(new Connect<Letter>(I,H));
            letter.Add(new Connect<Letter>(J, H));
            letter.Add(new Connect<Letter>(H, D));
            letter.Add(new Connect<Letter>(F, E));
            letter.Add(new Connect<Letter>(G,E));
            letter.Add(new Connect<Letter>(E, D));
            letter.Add(new Connect<Letter>(D, K));

            letter.Add(new Connect<Letter>(K, L));
            letter.Add(new Connect<Letter>(L,M));
            letter.Add(new Connect<Letter>(L, N));
            letter.Add(new Connect<Letter>(K, P));
            letter.Add(new Connect<Letter>(P,Q));
            letter.Add(new Connect<Letter>(P, R));
        }
        /// <summary>
        /// 寻找起始节点
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        static List<Connect<Letter>> Find(string s)
        {
            List<Connect<Letter>> tt = new List<Connect<Letter>>();
            foreach (Connect<Letter> t in letter)
            {
                if (t.Item1.Word== s)
                {
                    tt.Add(t);
                }
            }
            return tt;
        }
        /// <summary>
        /// 寻找所有父节点链接
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        static List<Connect<Letter>> Fathers(Letter l)
        {
            List<Connect<Letter>> tt = new List<Connect<Letter>>();
            foreach (Connect<Letter> t in letter)
            {
                if (t.Item2 == l)
                {
                    tt.Add(t);
                }
            }
            return tt;
        }
       /// <summary>
       /// 寻找所有子节点链接
       /// </summary>
       /// <param name="l"></param>
       /// <returns></returns>
        static List<Connect<Letter>> Children(Letter l)
        {
            List<Connect<Letter>> tt = new List<Connect<Letter>>();
            foreach (Connect<Letter> t in letter)
            {
                if (t.Item1 == l)
                {
                    tt.Add(t);
                }
            }
            return tt;
        }
    }
    public class Letter
    {
        private string _word;
        private Boolean _isRead;
        public string Word
        {
            get { return _word; }
            set { _word = value; }
        }
        public Boolean IsRead
        {
            get { return _isRead; }
            set { _isRead = value; }
        }
        public Letter(string v)
        {
            this._word = v;
            _isRead = false;
        }

        public void write() { Console.WriteLine(_word); _isRead = true; }
    }
    class Connect<T> where T :class
    {
        public T Item1 { get; set; }
        public T Item2 { get; set; }
        public Connect(T t1, T t2)
        {
            Item1 = t1;
            Item2 = t2;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档