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 = 0; i < dr.Rows.Count; i++)
            {
                if (Convert.ToInt32(dr.Rows[i]["pid"]) == 0)
                {

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 = 0; 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 = 0;
            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[0];
                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; }

}

时间: 2024-11-05 11:40:22

C# 最原始的tree 递归使用的相关文章

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

leetcode 100. Same Tree(递归)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 简单的递归. /** * Definition for a binary tree node. * struct TreeNode

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 inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a wh

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 inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a wh

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

42: Binary Tree Postorder Traversal

/************************************************************************/            /*       42:  Binary Tree Postorder Traversal                               */            /************************************************************************/

二叉树三种遍历(递归以及非递归实现)

package com.shiyeqiang.tree; import java.util.Stack; public class BiTree { public static void main(String[] args) { // 首先构造叶子节点 BiTree leafA1 = new BiTree(4); BiTree leafA2 = new BiTree(5); BiTree leafB1 = new BiTree(6); BiTree leafB2 = new BiTree(7)

如何采用easyui tree编写简单角色权限代码

首先每个管理员得对应一个角色: 而角色可以操作多个栏目,这种情况下我们可以采用tree多选的方式: 在页面上js代码: $('#Permission').dialog({ title: '栏目权限', closed: false }); $('#rtt').tree({ url: 'ashx/RoleService.ashx?action=RoleTree&Rid=' + raw.ID, method: 'get', animate: true, checkbox: true }); $('#R