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 */ //准备数组,代替从数据库中检 ...
随机推荐
- Hbase伪分布式
其实我就是要让数据存储在hdfs上而已........ 多配置点东西就好了,在hbase-site.xml中加入: <configuration> <property> < ...
- php之面向对象
<?php declare(encoding='UTF-8'); class Site{ /*成员变量*/ var $url; var $title = "gunduzi" ...
- 如何刪除GitHub中的repository
如何刪除一github中的repository,這本該是個非常簡單的操作,可一開始搜的時候,有不少文章比較含糊.這裡就記錄下來吧. 1.訪問https://github.com/settings/pr ...
- this web application instance has been stopped already解决办法
重启tomcat的时候出错 Illegal access: this web application instance has been stopped already. Could not loa ...
- jQuery实现的美观的倒计时实例代码
<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name=&q ...
- iOS 版本更新检查
#pragma mark ---------------------------------- #pragma mark -- 检测版本更新 -(void)onCheckVersion { NSDic ...
- Java中的GC操作及相关概念
一.GC Roots Tracing的基本思路:通过一系列名为"GC Roots"的对象作为起始点,从这些节点开始向下搜索,搜索所经过的路径称为引用链(Reference Chai ...
- java中的==和!=
java中一般很少用到==和!=,除了用于和null比较,如: if(null==o){ } //或者 if(null!=o){ } 其他地方比较一律用equals(); 建议:写完代码后在整个项目中 ...
- 嵌入式X86运行linux及QtEmbedded+触摸屏(X86PC104+Xlinux+QtE+触摸屏解决办法)
嵌入式X86运行linux及QtEmbedded+触摸屏(X86PC104+Xlinux+QtE+触摸屏解决办法) QQ:5724308 邮箱:sankye@163.com
- visual studio 中快捷键的使用
我在使用编辑器的过程中是比较喜欢使用快捷键的,因为这样可以在操作中更加便捷 ①ctrl+k,ctrl+d,代码重排 ②ctrl+k,k就是ctrl键加连续两次k键,添加书签,然后通过ctrl+k,ct ...