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 = ; 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 递归使用的更多相关文章
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 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 ...
- leetcode-101. 判断对称树 · Tree + 递归
题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 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 ...
- EasyUI Tree递归方式获取JSON
最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...
- leetcode-111. 二叉树最小深度 · Tree + 递归
题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...
- 如何采用easyui tree编写简单角色权限代码
首先每个管理员得对应一个角色: 而角色可以操作多个栏目,这种情况下我们可以采用tree多选的方式: 在页面上js代码: $('#Permission').dialog({ title: '栏目权限', ...
- 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 ...
- PHP无限级分类实现(递归+非递归)
<?php /** * Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: 上午12:00 */ //准备数组,代替从数据库中检 ...
随机推荐
- OAF_VO系列3 - Binding Style绑定方式
在OAF VO开发中,Binding Style主要用于对VO的where clause做动态传值,总共有三种方式 1. Oracle Named 2. Oracle Posi ...
- centos7编译安装nginx1.8
安装pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz tar -zxvf pcre-8. ...
- android:gravity 和 android:layout_Gravity属性
LinearLayout有两个非常相似的属性: android:gravity 与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中 ...
- Eclipse下快速打开本地文件插件EasyExplorer(转)
EasyExplorer 是一个类似于 Windows Explorer的Eclipse插件,它可以帮助你在不退出Eclipse的环境下浏览本地文件系统,类似的插件也有很多,但是本人喜欢使用这个版本 ...
- wireshark1.8捕获无线网卡的数据包——找不到无线网卡!
问题说明:奇怪的是,我线网卡明明有的,是interl的型号,可是wireshark总是找不到,如下: 奇了怪了,没有!原来是如下的: 实际上这块无线网卡是存在的,只不过由于兼容或驱动的原因无法显示型号 ...
- C++学习18 派生类的析构函数
和构造函数类似,析构函数也是不能被继承的. 创建派生类对象时,构造函数的调用顺序和继承顺序相同,先执行基类构造函数,然后再执行派生类的构造函数.但是对于析构函数,调用顺序恰好相反,即先执行派生类的析构 ...
- ppm与mg/m3转换
http://wenku.baidu.com/link?url=PY0Nb09VWmBDZgLvMhaHYGJyaC6YcdYAI5XTPRkxMpDHPrs3dNxskYkKmp2F0E6Sl2H5 ...
- Ext TreeGrid提交修改过的数据
本打算将整个treestore的数据提交到服务器,但找来找去没有找到好的方法,在翻api的时候发现了getUpdatedRecords()方法,拿来一试,试出此方法可以拿到被修改过的record so ...
- [ASP.NET]动态绑定树控件:
public void BindTree(TreeView tview, TreeNode tn_main, string parentId,string sql) { TreeNode tn=nul ...
- Js获取当前日期时间及时间相关操作
Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...