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. listview--记录ListView滚动停止位置与设置显示位置

    在项目中经常使用到listView控件,当想记录滚动停止时的记录,当点击加载新的数据,从记录的位置开始显示的操作怎么实现尼?分为如下步骤 1.记录位置代码 //声明记录停止滚动时候,可见的位置 pri ...

  2. Android listview中使用checkbox

    最近比较忙碌,我也不知道忙的什么东西,打算写的博客写了一半,还没写完,今天先扯一扯项目中遇到的一个问题,一方面防止以后遇到这个问题忘记如何解决,另一方面希望可以提供给遇到同样问题的朋友一个思路.下面开 ...

  3. ylbtech-Unitity-cs:传递的字符串中数字字符的数目

    ylbtech-Unitity-cs:传递的字符串中数字字符的数目 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1, using System; namespace Functions ...

  4. iphone的手势与触摸编程学习笔记

    一.基本概念与理解:Cocoa Touch将触摸事件发送到正在处理的视图.触摸传达的信息包括: 触摸发生的位置 (当前位置.最近的历史位置) 触摸的阶段 (按下.移动.弹起) 轻击数量 (tapCou ...

  5. 关于Servlet中的HttpServletRequest和HttpServletResponse

    1.HttpServletRequest 方    法 说    明 getAttributeNames() 返回当前请求的所有属性的名字集合 getAttribute(String name) 返回 ...

  6. linux secureCRT utf-8编码显示

    secureCRT 会话选项-终端-外观-字符编码: 下拉选择UTF-8 关闭当前secureCRT,另开一个新的让设置生效,显示正常.

  7. sql执行返回值存储

    List<Map> list = SqlRunner.queryMapList(sql); if(list != null && !list.isEmpty()){ Has ...

  8. poj 2253 Frogger dijkstra算法实现

    点击打开链接 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21653   Accepted: 7042 D ...

  9. Hadoop2.6.0错误

    错误1: WARN hdfs.DFSClient: DataStreamer Exception org.apache.hadoop.ipc.RemoteException(java.io.IOExc ...

  10. git 如何恢复只是提交到本地的文件(或者commit)

    今天早上傻逼了,把四天的代码commit到了本地,然后fetch一下,然后就全没了,不过git还是挺强大的 参考:http://blog.163.com/jiams_wang/blog/static/ ...