递归树 查找路径

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp3
{
    public class ThreeNode
    {
        public ThreeNode()
        {
            ChildList = new List<ThreeNode>();
            ParentList = new List<ThreeNode>();
            lujing = new List<string>();
        }
        public double value;
        public List<string> lujing;
        public string Sequence;
        public List<ThreeNode> ChildList;
        public List<ThreeNode> ParentList;
        public double[] Probablity;
        public bool IsLeaf;



        public  List<string> gellist()
        {
            List<string> newlist = new List<string>();
            if (this.ChildList.Count > 0)
            {
                foreach (var item in this.ChildList)
                {
                    newlist.AddRange(MultList(this.Sequence.ToString(), item.gellist()));
                }
            }
            else
            {
                newlist.Add(this.Sequence.ToString());
            }
            this.lujing = newlist;
            return newlist;
        }

        public  List<string> MultList(string sequence, List<string> list)
        {
            List<string> newlist = new List<string>();
            if (list.Count > 0)
            {
                foreach (var item in list)
                {
                    newlist.Add(sequence + "," + item);
                }
            }
  
            return newlist;
        }
    }
    class Program
    {
     
        public List<string> AddList(List<string>list1,List<string> list2)
        {
            List<string> newlist = new List<string>();
            newlist.AddRange(list1);
            newlist.AddRange(list2);
            return newlist;
        }
        
      
        static void Main(string[] args)
        {
            ThreeNode root = new ThreeNode() { Sequence = "0" };
            var t11 = new ThreeNode() { Sequence = "11" };
            var t12 = new ThreeNode() { Sequence = "12" };
            var t13 = new ThreeNode() { Sequence = "13" };
            root.ChildList.Add(t11);
            root.ChildList.Add(t12);
            root.ChildList.Add(t13);

            var t21 = new ThreeNode() { Sequence = "21" };
            var t22 = new ThreeNode() { Sequence = "22" };
            var t23 = new ThreeNode() { Sequence = "23" };
            var t24 = new ThreeNode() { Sequence = "24" };
            t11.ChildList.Add(t21);
            t11.ChildList.Add(t22);
            t11.ChildList.Add(t23);

            t12.ChildList.Add(t22);
            t12.ChildList.Add(t23);
            t12.ChildList.Add(t24);

            t13.ChildList.Add(t22);
            t13.ChildList.Add(t23);
            t13.ChildList.Add(t24);

            var t31 = new ThreeNode() { Sequence = "31" };
            var t32 = new ThreeNode() { Sequence = "32" };
            var t33 = new ThreeNode() { Sequence = "33" };
            var t34 = new ThreeNode() { Sequence = "34" };
            var t35 = new ThreeNode() { Sequence = "35" };

            t21.ChildList.Add(t31);
            t21.ChildList.Add(t32);
            t21.ChildList.Add(t33);

            t22.ChildList.Add(t32);
            t22.ChildList.Add(t33);
            t22.ChildList.Add(t34);

            t23.ChildList.Add(t33);
            t23.ChildList.Add(t34);
            t23.ChildList.Add(t35);

            t24.ChildList.Add(t33);
            t24.ChildList.Add(t34);
            t24.ChildList.Add(t35);

            root.gellist();
          
            Console.WriteLine("Hello World!");

         
            Console.Read();
        }
    }
}

 

posted on 2019-01-29 10:47  听哥哥的话  阅读(229)  评论(0编辑  收藏  举报

导航