zoukankan      html  css  js  c++  java
  • 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹。

    深搜算法用到了递归。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace 文本文件 {
        //public partial class Form1 : Form {
        //    public Form1() {
        //        InitializeComponent();
        //    }
    
        //    private void Form1_Load(object sender, EventArgs e) {
        //        string path = "DelegetTestSpace";
        //        string[] dirs = Directory.GetDirectories(path);
        //        string[] files = Directory.GetFiles(path);
    
        //        TreeNode node = new TreeNode("DelegetTestSpace");
        //        tvContentOfTable.Nodes.Add(node);
        //        node.ImageIndex = 0;
        //        node.SelectedImageIndex = 0;
        //        DFS(path, node);
        //    }
        //    // 采用深度优先算法加入节点。
        //    private static void DFS(string path, TreeNode node) {
        //        string[] dirs = Directory.GetDirectories(path);
        //        string[] files = Directory.GetFiles(path);
    
        //        foreach (string dir in dirs) {
        //            TreeNode subnode = node.Nodes.Add(Path.GetFileName(dir));
        //            subnode.ImageIndex = 0;
        //            subnode.ImageIndex = 0;
        //            DFS(dir,subnode);             
        //        }
        //        foreach (string file in files) {
        //            TreeNode filenode = node.Nodes.Add(Path.GetFileName(file));
        //            filenode.ImageIndex =1;
        //            filenode.SelectedImageIndex = 1;
        //            // 这个tag里面包含了这个文件的完整路径。是这个节点的附加信息。
        //            filenode.Tag = file;
        //        }
        //    }
    
        //    private void tvContentOfTable_AfterSelect(object sender, TreeViewEventArgs e) {
        //        // 一般的,在EventArgs前面有内容,都是当前的信息。这里指的是点击的节点 古可以得到这个节点的附加信息。
        //        string tag;
                
        //        // 文件夹节点没有tag
        //        if (e.Node.Tag != null) {
        //            tag = e.Node.Tag.ToString();
        //            tbContent.Text = File.ReadAllText(tag, Encoding.Default);
        //        }
        //    }
        
        //}
    


    下面是宽搜,用到一个全局队列。

    public partial class Form1:Form {
                public Form1() {
                    InitializeComponent();
                }
                public struct dictionary {
                    public TreeNode node;
                    public string str;
                }
    
                Queue<dictionary> queue = new Queue<dictionary>();
                private void Form1_Load(object sender, EventArgs e) {
                    string path = "DelegetTestSpace";
                    dictionary dic = new dictionary();
                    dic.node = new TreeNode(path);
                    dic.str = path;
    
                    tvContentOfTable.Nodes.Add(dic.node);
                    BFS(dic);
                }
    
    
                private void BFS(dictionary dic) {
                    dictionary temp = new dictionary();
                    queue.Enqueue(dic);
    
                    while (true) {
                        if (queue.Count==0) {
                            break;
                        }
                        temp = queue.Dequeue();
                        
                        TreeNode node = temp.node;
                        string str = temp.str;
                        string[] dirs = Directory.GetDirectories(str);
                        string[] files = Directory.GetFiles(str);
    
                        //入队文件夹
                        foreach (string dir in dirs) {
                            string lastName = Path.GetFileName(dir);
                            TreeNode subNode = new TreeNode(lastName);
                            dictionary subDic = new dictionary();
                            subDic.node = subNode;
                            subDic.str = dir;
                            queue.Enqueue(subDic);
    
                            node.Nodes.Add(subNode);
                        }
    
                        //入队文件
                        foreach (string file in files) {
                            dictionary fileDic = new dictionary();
                            string lastName = Path.GetFileName(file);
                            fileDic.node = new TreeNode(lastName);
                            fileDic.node.Tag = file;
                            node.Nodes.Add(fileDic.node);
                        }
    
                    }
                    }
                private void tvContentOfTable_AfterSelect(object sender, TreeViewEventArgs e) {
                    //   一般的,在EventArgs前面有内容,都是当前的信息。这里指的是点击的节点 古可以得到这个节点的附加信息。
                    string tag;
    
                    // 文件夹节点没有tag
                    if (e.Node.Tag != null) {
                        tag = e.Node.Tag.ToString();
                        tbContent.Text = File.ReadAllText(tag, Encoding.Default);
                    }
                }

    其中,用到了一个imageList组件,用它来提供一个节点的图像。

    效果如图。


  • 相关阅读:
    UVA 10572 Black & White (状压DP)
    ZOJ 3466 The Hive II (插头DP,变形)
    POJ 3133 Manhattan Wiring (插头DP,轮廓线,经典)
    HDU 3377 Plan (插头DP,变形)
    HDU 1964 Pipes (插头DP,变形)
    FZU 1977 Pandora adventure (插头DP,常规)
    URAL 1519 Formula 1 (插头DP,常规)
    POJ 1739 Tony's Tour (插头DP,轮廓线DP)
    HDU 1693 Eat the Trees (插头DP)
    hihoCoder #1162 : 骨牌覆盖问题·三 (矩阵快速幂,DP)
  • 原文地址:https://www.cnblogs.com/likeFlyingFish/p/5345987.html
Copyright © 2011-2022 走看看