zoukankan      html  css  js  c++  java
  • C# 最原始的tree 递归使用

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Reflection;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace EasyUITree
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            List<tree> listTreeAll = new List<tree>();

            tree treeAll = new tree();

            public tree BindNew(tree node)
            {
                DataTable dr = GetReader(node.id);
                tree n = new tree();
                for (int i = 0; i < dr.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dr.Rows[i]["pid"]) == 0)
                    {

                        n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                        n.text = dr.Rows[i]["text"].ToString();
                        n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                        n.child = GetChild(n);

                    }
                }
                return n;
            }
            public List<tree> GetChild(tree node)
            {
                DataTable dr = GetReader(node.id);
                List<tree> child = new List<tree>();
                for (int i = 0; i < dr.Rows.Count; i++)
                {

                    tree n = new tree();
                    n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                    n.text = dr.Rows[i]["text"].ToString();
                    n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                    child.Add(n);
                    DataTable dr1 = GetReader(n.id);
                    if (dr1 != null)
                    {
                        n.child = GetChild(n);
                    }


                }
                return child;
            }

            





            /// <summary>
            
    /// 测试
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            protected void Button1_Click(object sender, EventArgs e)
            {
                tree model = new tree();
                model.id = 0;
                BindNew(model);//递归

                List<tree> list = new List<tree>();
                list.Add(BindNew(model));




            }

            public DataTable GetReader(int pid)
            {
                string sql = " select * from t_tree where pid = " + pid + "  ";  //where pid = " + pid + "  ";
                string ConnectionString = "uid=sa;pwd=qazwsx;initial catalog=TestDBase;data source=DESKTOP-HKIRA54;Connect Timeout=900";
                using (SqlConnection con = new SqlConnection(ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand(sql, con);
                    con.Open();
                    DataSet ds = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(ds);

                    DataTable table = ds.Tables[0];
                    return table;
                }
            }
        }


        public class tree
        {
            public int id { getset; }
            public string text { getset; }
            public int pid { getset; }
            public List<tree> child { getset; }


        }
  • 相关阅读:
    BZOJ3509: [CodeChef] COUNTARI
    BZOJ3790: 神奇项链
    BZOJ3527: [Zjoi2014]力
    BZOJ2194: 快速傅立叶之二
    解题:BJOI 2006 狼抓兔子
    解题:SDOI 2017 数字表格
    解题:TJOI 2015 弦论
    解题:NOI 2016 优秀的拆分
    解题:AHOI2017/HNOI2017 礼物
    解题:洛谷2093 JZPFAR
  • 原文地址:https://www.cnblogs.com/dullbaby/p/5043675.html
Copyright © 2011-2022 走看看