首页 > 其他 > 详细

递归和嵌套循环的区别

时间:2016-01-08 18:30:32      阅读:405      评论:0      收藏:0      [点我收藏+]

递归和嵌套循环的区别

亲,不要误以为自己调用自己就等于递归了!

技术分享

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TreeSearch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //查询前折叠所有
            trv.CollapseAll();

            //关键字
            string keyStr = txtKey.Text.Trim();

            //遍历根节点
            foreach (TreeNode tn in trv.Nodes)
            {
                CheckNode(tn, keyStr);
                FindChildNode(tn, keyStr);
            }
        }

        /// <summary>
        /// 遍历子节点
        /// </summary>
        /// <param name="tnParent">当前节点</param>
        /// <param name="keyStr">关键字</param>
        void FindChildNode(TreeNode tnParent, string keyStr)
        {
            foreach (TreeNode tn in tnParent.Nodes)
            {
                CheckNode(tn, keyStr);

                //递归检查当前节点的子节点
                FindChildNode(tn, keyStr);
            }
        }

        /// <summary>
        /// 展示符合条件的节点
        /// </summary>
        /// <param name="tn"></param>
        void DisplayNode(TreeNode tn)
        {
            if (tn.Parent != null)
            {
                tn.Parent.Expand();
                DisplayNode(tn.Parent);
            }
        }

        /// <summary>
        /// 检测当前节点
        /// </summary>
        /// <param name="tn"></param>
        /// <param name="keyStr"></param>
        void CheckNode(TreeNode tn, string keyStr)
        {
            //包含关键字,变红并展示节点
            if (tn.Text.Contains(keyStr))
            {
                tn.ForeColor = Color.Red;
                DisplayNode(tn);
            }
            else
            {
                //代替初始化的操作方法
                tn.ForeColor = Color.Black;
            }
        }


        //从低往高加
        public int sum23(int param)
        {
            int sum = 0;
            for (int i = 0; i < param; i++)
            {
                sum += 1;
            }
            return sum;
        }

        //从高往第加
        public int sum1(int param)
        {
            int sum = 0;
            for (int i = param; i >= 0; i--)
            {
                sum += i;
            }
            return sum;
        }


        public int sum(int param)
        {
            if (param == 0) { return 0; }
            else
            {
               return  param += sum(param-1);
            }
        }

        public long Fac(int n)
        {
            if (n == 0)
            {
                return 1;
            }
            else
            {   //用for循环来不断的叠乘
                long value = 1;
                for (int i = n; i > 0; i--)
                {
                    value *= 1;
                }
                return value;
            }

        }


    }
}

 

递归和嵌套循环的区别

原文:http://www.cnblogs.com/mc67/p/5114008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!