using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace TreeTest{ class Program { static List> letter = new List >(); static void Main(string[] args) { Init(); List > tt = Find("A"); Stack st = new Stack (); Search(st, tt.ElementAt(0).Item1); List < Letter > s=st.Reverse ().ToList (); Console.WriteLine("******************************"); s.ForEach((o) => { o.write(); }); } /// /// 递归搜索树 /// /// /// static void Search(Stackst, Letter t) { List > fs = Fathers(t); List > cs = Children(t); if (fs.Count > 0) {//有父亲 //查看是否存在未读取的父亲 bool allread = isAllRead(fs); if (!allread) { foreach (Connect 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 tt in cs) {//迭代父节点 if(tt.Item2.IsRead==false) Search(st, tt.Item2); } } else {//无孩子 结束迭代 return; } } static Boolean isAllRead(List > tt) { foreach (Connect 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 (A, B)); letter.Add(new Connect (C, B)); letter.Add(new Connect (B, D)); letter.Add(new Connect (I,H)); letter.Add(new Connect (J, H)); letter.Add(new Connect (H, D)); letter.Add(new Connect (F, E)); letter.Add(new Connect (G,E)); letter.Add(new Connect (E, D)); letter.Add(new Connect (D, K)); letter.Add(new Connect (K, L)); letter.Add(new Connect (L,M)); letter.Add(new Connect (L, N)); letter.Add(new Connect (K, P)); letter.Add(new Connect (P,Q)); letter.Add(new Connect (P, R)); } /// /// 寻找起始节点 /// /// ///static List > Find(string s) { List > tt = new List >(); foreach (Connect t in letter) { if (t.Item1.Word== s) { tt.Add(t); } } return tt; } /// /// 寻找所有父节点链接 /// /// ///static List > Fathers(Letter l) { List > tt = new List >(); foreach (Connect t in letter) { if (t.Item2 == l) { tt.Add(t); } } return tt; } /// /// 寻找所有子节点链接 /// /// ///static List > Children(Letter l) { List > tt = new List >(); foreach (Connect 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 where T :class { public T Item1 { get; set; } public T Item2 { get; set; } public Connect(T t1, T t2) { Item1 = t1; Item2 = t2; } }}