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;
}
}
}