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. RabbitMQ介绍4 - 编程(C#客户端示例)

    C#终端的说明文档: http://www.rabbitmq.com/dotnet-api-guide.html 这里介绍使用RabbitMQ的几种典型场景. 1. 简单direct模式( http: ...

  2. KEIL编译错误总结:

    1 expected an identifier:#define宏定义常量后,如果再用前面定义的常量作为枚举常量就会报错,方法,去掉#define宏定义

  3. 计算webView的 高度 和自适应屏幕大小

    - (void)webViewDidFinishLoad:(UIWebView *)webView{ [webView stringByEvaluatingJavaScriptFromString: ...

  4. 关于Objective-C 对象release操作的一个小问题探讨

    来源:http://blog.csdn.net/duxinfeng2010/article/details/8757211 最近遇到这样一个问题,以前的时候并未注意:新建一个工程,然后添加一个类,文件 ...

  5. textarea文本换行和页面显示换行符

    在textarea里写的文本有换行,但是显示到页面后就不会自动换行,通过对数据分析发现textarea里的换行符是\n\r,然而HTML中的换行为<\br>解决办法有两种: 第一种:把文本 ...

  6. java生产随机字符串

    public static String getRandomString(int length) { //length表示生成字符串的长度 String base = "abcdefghij ...

  7. 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案

    找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...

  8. [HackerCup Round1 3] Winning at Sports (动态规划)

    题目链接:https://www.facebook.com/hackercup/problems.php?pid=688426044611322&round=344496159068801 题 ...

  9. router os

    http://www.oschina.net/news/47568/router-operation-system

  10. iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...