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 */ //准备数组,代替从数据库中检 ...
随机推荐
- BestCoder Round #84 Bellovin
Bellovin 题意: 给个中文链接:戳戳戳 题解: 这个题其实就是让你求每一位的最长公共子序列,之后输出就好了,求这个有2个算法,一个是n方,另一个nlogn,所以显然是nlogn的算法,其实这就 ...
- centos7编译安装nginx1.8
安装pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz tar -zxvf pcre-8. ...
- ylbtech-Unitity-cs:计算阶乘值
ylbtech-Unitity-cs:计算阶乘值 1.A,效果图返回顶部 1.B,源代码返回顶部 1.B.1, using System; namespace Functions { public ...
- Laravel 部署安装到虚拟主机的方法(折腾了一周,终于成功部署,原来是虚拟机不加载.env,谢谢莫回首http://lxl520.com/index.php/archives/88/!)
作者:莫回首链接:https://www.zhihu.com/question/35497879/answer/111241182来源:知乎著作权归作者所有,转载请联系作者获得授权. 序 lara ...
- git 冲突解决(转载)
gerrit是不会解决冲突的,如果两个人同时改了一个文件的同一行,就会冲突,你将会看到Review in Progress并且最下面会有Your change could not be merged ...
- Android学习笔记02
1.线性布局LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- http示例代码
//下载文件 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletEx ...
- oracle decode
decode()函数简介: 主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明): 使用方法: Select decode(columnname,值1,翻译值1,值2,翻译值2,.. ...
- [Flex] PopUpButton系列 —— 打开和关闭弹出菜单
<?xml version="1.0" encoding="utf-8"?><!--响应打开和关闭弹出菜单的例子 PopUpButtonOpe ...
- zookeeper 监控 —— 阿里taokeeper
TaoKeeper是一个围绕ZooKeeper做的监控与报表系统. 主要功能如下: 能够统计ZK集群连接数,Watcher数目 ,节点数等系列信息,并按一定规则进行一些聚合操作; 能够通过设置一些阈值 ...