递归树

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Collections.Generic;
  12. using System.Data.SqlClient;
  13. public partial class Default2 : System.Web.UI.Page
  14. {
  15.     IList<Category> list = new List<Category>();
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!this.IsPostBack)
  19.         {
  20.             SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=coll");
  21.             SqlCommand cmd = new SqlCommand("select * from category", con);
  22.             con.Open();
  23.             SqlDataReader reader = cmd.ExecuteReader();
  24.             while (reader.Read())
  25.             {
  26.                 Category c = new Category();
  27.                 c.CategoryID = reader.GetInt32(0);
  28.                 c.ParentID = reader.GetInt32(1);
  29.                 c.Layer = reader.GetInt32(2);
  30.                 c.Name = reader.GetString(3);
  31.                 list.Add(c);
  32.             }
  33.             reader.Close();
  34.             cmd.Dispose();
  35.             con.Close();
  36.             this.AddTreeNode(0, null);
  37.         }
  38.     }
  39.     private void AddTreeNode(int parentId, TreeNode node)
  40.     {
  41.         List<Category> categorylist = new List<Category>();
  42.         foreach (Category c in this.list)
  43.         {
  44.             if (c.ParentID == parentId)
  45.                 categorylist.Add(c);
  46.         }
  47.         if (categorylist.Count > 0)
  48.         {
  49.             foreach (Category cate in categorylist)
  50.             {
  51.                 TreeNode tNode = new TreeNode();
  52.                 tNode.Text = cate.Name;
  53.                 if (node == null)
  54.                 {
  55.                     this.TreeView1.Nodes.Add(tNode);
  56.                 }
  57.                 else
  58.                 {
  59.                     node.ChildNodes.Add(tNode);
  60.                 }
  61.                 this.AddTreeNode(cate.CategoryID, tNode);
  62.             }
  63.         }
  64.     }
  65. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
递归(Recursion Tree)是一种可视化递归算法的方式,它通过将递归算法转化为形结构来帮助我们更好地理解递归算法的执行过程和时间复杂度。 在递归算法中,每次递归调用都会产生一个新的子问题,直到达到基本情况,然后逐级返回结果。这样的递归过程可以用一棵来表示,其中每个节点代表一个递归调用,子节点代表对应的子问题,叶子节点代表基本情况的返回值。 举个例子,假设我们要求一个数组的所有子集,可以使用如下的递归算法: ``` void subsets(vector<int>& nums, int start, vector<int>& subset, vector<vector<int>>& res) { res.push_back(subset); for (int i = start; i < nums.size(); i++) { subset.push_back(nums[i]); subsets(nums, i+1, subset, res); subset.pop_back(); } } vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> res; vector<int> subset; subsets(nums, 0, subset, res); return res; } ``` 我们可以将递归过程可视化为下面的递归: ``` [] / | \ [1] [2] [3] / \ / \ | [1,2] [1,3] [2,3] [3,4] [1,2,3] / \ | [1,2,3] [1,3,4] [2,3,4] ``` 其中,[]代表一个子集,每个节点代表一次递归调用,左子节点代表将当前元素加入子集,右子节点代表不将当前元素加入子集。当递归到数组末尾时,就返回一个空子集。 通过递归,我们可以更好地理解递归算法的执行过程,并且可以根据递归来分析时间复杂度。在上面的例子中,递归的深度为数组的长度N,每个节点会被访问一次,因此时间复杂度为O(2^N)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值