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 */ //准备数组,代替从数据库中检 ...
随机推荐
- maven命令行跳过test
mvn跳过测试的两种方法: -DskipTests -Dmaven.test.skip=true
- 基于jquery的表单校验插件 - rjboy的Validform使用体验
官方地址:http://validform.rjboy.cn/document.html 引用js后再加上以下css就可以使用了 .Validform_checktip{ margin-left:8p ...
- 转载__Android开源项目(二)
http://www.csdn.net/article/1970-01-01/2815145 GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用 ...
- error-iis-Service Unavailable
Service Unavailable Service Unavailable HTTP Error 503. The service is unavailable.
- 每个程序员都需要学习 JavaScript 的7个理由
最近在和招聘经理交流现在找一个好的程序员有多难的时候,我渐渐意识到了现在编程语言越来越倾重于JavaScript.Web开发人员尤其如此.所以,如果你是一个程序员,那么你应该去学习JavaScript ...
- SQL Server 2005中的分区表(一):什么是分区表?为什么要用分区表?如何创建分区表?(转)
如果你的数据库中某一个表中的数据满足以下几个条件,那么你就要考虑创建分区表了. 1.数据库中某个表中的数据很多.很多是什么概念?一万条?两万条?还是十万条.一百万条?这个,我觉得是仁者见仁.智者见 ...
- Oracle中常用操作
查看表中的字段名和类型 SELECT column_name,DATA_TYPE FROM cols WHERE TABLE_NAME=upper('tableName') ORDER BY COLU ...
- (转)一段如何調用Button.Click事件的故事
原文地址:http://helloouc.blog.163.com/blog/static/5530527120091050314590/ 一.前言 由于小朱与BillChung的启发,想写一个故事, ...
- Java-convert between INT and STRING
int -> String 三种写法 String s = 43 + ""; String s = String.valueOf(43); String s = Intege ...
- Appium+Maven+TestNG(ReportNG)环境搭建(详细过程)
最近群里经常有人会问到关于maven构建Appium测试项目以及使用testNG生成测试报告的问题,试着搭建了一下,下面是过程: jdk安装过程我这里就不说了 一.下载eclipse,推荐下载Ecli ...