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 = ; i < dr.Rows.Count; i++)
            {
                if (Convert.ToInt32(dr.Rows[i]["pid"]) == )
                {                     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 = ; 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 = ;
            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[];
                return table;
            }
        }
    }     public class tree
    {
        public int id { get; set; }
        public string text { get; set; }
        public int pid { get; set; }
        public List<tree> child { get; set; }     }

C# 最原始的tree 递归使用的更多相关文章

  1. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  2. leetcode 226. Invert Binary Tree(递归)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  3. leetcode-101. 判断对称树 · Tree + 递归

    题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 2. 根节点不空,递归判断左右子树.如果左右孩子都空 ...

  4. LeetCode (226):Invert Binary Tree 递归实现

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  5. EasyUI Tree递归方式获取JSON

    最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...

  6. leetcode-111. 二叉树最小深度 · Tree + 递归

    题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...

  7. 如何采用easyui tree编写简单角色权限代码

    首先每个管理员得对应一个角色: 而角色可以操作多个栏目,这种情况下我们可以采用tree多选的方式: 在页面上js代码: $('#Permission').dialog({ title: '栏目权限', ...

  8. Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. PHP无限级分类实现(递归+非递归)

    <?php /** * Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: 上午12:00 */ //准备数组,代替从数据库中检 ...

随机推荐

  1. Topology的构建

    public class BlackListBolt extends BaseRichBolt{ private static Logger logger = Logger.getLogger(Bla ...

  2. zabbix安装,关闭SELinux

    一.缘由 在安装zabbix的时候,按照官网的Zabbix Manual一路跑下来,zabbix的dashboard提示:zabbix server is not running the inform ...

  3. Javascript生成GUID

    GUID(全球唯一标识)是微软使用的一个术语,由一个特定的算法,给某一个实体,如Word文档,创建一个唯一的标识,GUID值就是这个唯一的标识码.除了.Net有专门的方法生成外,JS也可以生成GUID ...

  4. PHP每日签到时怎么实现

    以淘宝网领取淘金币的签到系统为例:目标:第一天签到增加5个积分:第二天连续签到则增加8个积分:第三天连续签到,增加11个积分,第 四天连续签到,增加15个积分:第五天连续签到,增加19个积分:第六天连 ...

  5. How to get the date N days ago in Python

    from datetime import datetime, timedelta N = 2 date_N_days_ago = datetime.now() - timedelta(days=N) ...

  6. python metaclass 入门简介

    http://cizixs.com/2015/08/30/metaclass-in-python 动态类型也是类型 python 是一种动态类型语言,换句话说每个变量可以在程序里任何地方改变它的类型. ...

  7. sopcinfo路径改变,nios工程该怎么办?

    操作系统:Win7 64 bit 开发环境:Quartus II 14.0 (64-Bit)  + Nios II EDS 14.0 使用Quartus 时,有时候出于备份的考虑,或者从网上下载别人的 ...

  8. QTEmbedded VCN实现

    1 在QT在第三方插件里加入VNC 进入\src\plugins\gfxdrivers\vnc,qmake -------make--------- make install 把目标文件夹gfxdri ...

  9. lua绑定c++的时候常见得错误

    1 Error parsing reanslation unit 这种情况,首先来说你的python 2.7以及他的插件安装是完整的,最可能的原因就是自己写的ini文件,header路径错误,可以把这 ...

  10. 剑指Offer:面试题4——替换空格(java实现)

    问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...