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 */ //准备数组,代替从数据库中检 ...
随机推荐
- CE_现金账户转账汇入汇出交易(案例)(未完成)
2014-07-15 BaoXinjian 一.摘要 二.案例 通过 Oracle Payments 结算事务处理 通过付款模板 事物处理子类型 已验证 -> 正在结算中 ->
- HTML番外整理
经过一周的个人项目与一周的团体项目,我学到了一些有用的内容,特分享如下: 一.视频 1.对在线视频的添加 在各大网站打开一个视频,在下方的分享找到HTML代码,然后复制HTML代码到自己的代码中. 2 ...
- git push失败的解决办法(2)
错误一:Cannot rebase: You have unstaged changes 解决办法: Cannot rebase: You have unstaged changes. 那说明有修改过 ...
- 数据库还原总提示空间不够,磁盘卷 'D:\' 上的可用空间不足,无法创建数据库
从数据库上备份下来bak格式的数据库文件之后,在本地数据库欢迎的时候总是提示空间不够. 这种情况一般在从64位电脑上面备份的数据库文件,还原到32位的sqlsever上面. System.Data.S ...
- 对CSS中的Position属性的一些深入探讨
转:http://www.cnblogs.com/coffeedeveloper/p/3145790.html Position属性 Position的属性值共有四个static.relative.a ...
- Windows2008防火墙封ip
http://www.bitscn.com/os/windows/201411/406212.html
- Hololens开发笔记之Gaze凝视射线
凝视是HoloLens首要输入方式,形式功能类似于桌面系统的光标,用于选择操作全息对象.然而在Unity中并没有明确的Gaze API或者组件. 概念上来说,Gaze是通过用户头部两眼之间发出一条向前 ...
- js封装 与 js高级用法 问题集合
1. 什么是自执行的匿名函数? 它是指形如这样的函数: (function {// code})(); 2. 疑问 为什么(function {// code})();可以被执行, 而function ...
- JS工作积累
/* * YYYY-MM-DD类型的字符串日期比较 * */ String.prototype.compareAsDate=function(dateStr){//prototype原型方法 var ...
- Bootstrap学习的点点滴滴
1)网站:http://www.bootcss.com/http://v3.bootcss.com/getting-started/#downloadhttps://github.com/twbs/b ...