码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的生成

时间:2015-12-02 12:41:54      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:二叉树

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace ConsoleApplication4
{
    class Program
{
        static void Main(string[] args)
{
            personalInfo p1 = new personalInfo("1", "1");
            personalInfo p2 = new personalInfo("2", "2");
            personalInfo p3 = new personalInfo("3", "3");
            personalInfo p4 = new personalInfo("", "");
            personalInfo p31 = new personalInfo("31", "31");
            personalInfo p32 = new personalInfo("32", "32");
            personalInfo p33 = new personalInfo("", "");
            personalInfo p321 = new personalInfo("", "");
            personalInfo p311 = new personalInfo("", "");
            personalInfo p21 = new personalInfo("", "");
            personalInfo p11 = new personalInfo("11", "11");
            personalInfo p12 = new personalInfo("", "");
            personalInfo p111 = new personalInfo("", "");
            List<personalInfo> list = new System.Collections.Generic.List<personalInfo>();
            list.AddRange(new personalInfo[]{p1,p2,p3,p4,p31,p32,p33,p321,p311,p21,p11,p12,p111});
            JiaPu j = new JiaPu(list);
            note phead = null;
            int index = j.CreateTree(ref phead, 0, 13);
}
        public struct personalInfo
{
            public personalInfo(string name, string Id)
{
                this.name = name;
                this.Id = Id;
}
            public string name;
            public string Id; // Id is empty or null, that means, no the person.
}
        public class note
{
            public personalInfo data;
            public note lChild, rChild;
}
        public class JiaPu
{
            List<personalInfo> persons;
           // int index = 0;
            public JiaPu(List<personalInfo> persons)
{
                this.persons = persons;
}
            public int CreateTree(ref note nNode,int index, int length)
{
                if(index>=length)
{
                    nNode= null;
                    return index;
}
                if (string.IsNullOrEmpty(persons[index].Id))
{
                    nNode = null;
                    return index;
}
                nNode = new note();
nNode.data = persons[index];

                index = CreateTree(ref nNode.lChild, ++index, length);

                index = CreateTree(ref nNode.rChild, ++index, length);
                return index;
}
}

}
}


本文出自 “每天积累一点点” 博客,请务必保留此出处http://fyifei0558.blog.51cto.com/7249113/1718741

二叉树的生成

标签:二叉树

原文地址:http://fyifei0558.blog.51cto.com/7249113/1718741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!