C#使用TreeView控件實(shí)現(xiàn)的二叉樹(shù)泛型節(jié)點(diǎn)類(lèi)及其方法
一、涉及到的知識(shí)點(diǎn)
1.TreeView控件
TreeView 控件在 C# 中主要用于顯示分層結(jié)構(gòu)的數(shù)據(jù)。這通常是一個(gè)文件系統(tǒng)的表示,但也可以是任何具有父子關(guān)系的數(shù)據(jù)集合。TreeView 控件在 Windows Forms 應(yīng)用程序中非常常見(jiàn),允許用戶通過(guò)點(diǎn)擊箭頭來(lái)展開(kāi)或折疊節(jié)點(diǎn),以查看或隱藏子節(jié)點(diǎn)。
在 C# Windows Forms 應(yīng)用程序中使用 TreeView 控件的基本步驟:
(1)添加 TreeView 控件到 Form:
在設(shè)計(jì)視圖中,從工具箱中拖動(dòng) TreeView 控件到 Form 上。
或者在代碼中,使用 Controls.Add 方法將 TreeView 添加到 Form。
(2)添加節(jié)點(diǎn):
使用 Nodes 屬性添加根節(jié)點(diǎn)。
使用 Nodes.Add 方法為根節(jié)點(diǎn)添加子節(jié)點(diǎn)。
也可以為子節(jié)點(diǎn)再添加子節(jié)點(diǎn),形成多級(jí)層次結(jié)構(gòu)。
(3)為節(jié)點(diǎn)添加文本和圖像:
使用 Text 屬性為節(jié)點(diǎn)設(shè)置文本。
使用 ImageIndex 和 SelectedImageIndex 屬性為節(jié)點(diǎn)設(shè)置圖像。這些屬性通常與 ImageList 控件結(jié)合使用,后者可以包含要在 TreeView 中顯示的圖像。
(4)事件處理:
AfterSelect:當(dāng)用戶選擇一個(gè)節(jié)點(diǎn)后觸發(fā)。
BeforeSelect:在用戶選擇一個(gè)節(jié)點(diǎn)之前觸發(fā),允許你取消選擇。
NodeMouseClick:當(dāng)用戶點(diǎn)擊一個(gè)節(jié)點(diǎn)時(shí)觸發(fā)。
其他事件,如 AfterExpand、BeforeExpand 等。
(5)自定義外觀和行為:
通過(guò)設(shè)置 TreeView 的屬性,如 LineColor、ExpandCollapseColor、ScrollAlwaysVisible 等,可以自定義其外觀和行為。
2.TreeView控件的應(yīng)用示例
該實(shí)例展示了如何在 Windows Forms 應(yīng)用程序中添加一個(gè) TreeView 控件并為其添加節(jié)點(diǎn):
// Form1.cs namespace _135_8 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { treeView1!.Nodes.Add("根節(jié)點(diǎn)1"); treeView1.Nodes[0].Nodes.Add("子節(jié)點(diǎn)1"); treeView1.Nodes[0].Nodes.Add("子節(jié)點(diǎn)2"); treeView1.Nodes.Add("根節(jié)點(diǎn)2"); } private void TreeView1_AfterSelect(object? sender, TreeViewEventArgs e) { MessageBox.Show("你選擇了節(jié)點(diǎn): " + e.Node!.Text); } } }
//Form1.Designer.cs namespace _135_8 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { treeView1 = new TreeView(); SuspendLayout(); // // treeView1 // treeView1.Dock = DockStyle.Fill; treeView1.Location = new Point(0, 0); treeView1.Name = "treeView1"; treeView1.Size = new Size(284, 181); treeView1.TabIndex = 0; treeView1.AfterSelect += TreeView1_AfterSelect; // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(284, 181); Controls.Add(treeView1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "Form1"; Load += Form1_Load; ResumeLayout(false); } #endregion private TreeView treeView1; } }
這個(gè)例子創(chuàng)建了一個(gè)簡(jiǎn)單的 TreeView,有兩個(gè)根節(jié)點(diǎn),其中一個(gè)根節(jié)點(diǎn)有兩個(gè)子節(jié)點(diǎn)。當(dāng)用戶選擇一個(gè)節(jié)點(diǎn)時(shí),會(huì)顯示一個(gè)消息框,顯示所選擇的節(jié)點(diǎn)的文本。
3.使用TreeView控件實(shí)現(xiàn)的二叉樹(shù)類(lèi)及其方法的可行性
在C#中,TreeView 控件通常用于展示層次結(jié)構(gòu)的數(shù)據(jù),但它并不直接支持二叉樹(shù)結(jié)構(gòu)。TreeView 的節(jié)點(diǎn)(TreeNode)類(lèi)并不限制其子節(jié)點(diǎn)的數(shù)量,這意味著你可以為每個(gè)節(jié)點(diǎn)添加任意數(shù)量的子節(jié)點(diǎn),這更適合于表示一般的樹(shù)形結(jié)構(gòu)而不是二叉樹(shù)。
然而,如果想用 TreeView 控件來(lái)展示二叉樹(shù),可以自定義一個(gè)二叉樹(shù)類(lèi),然后將其轉(zhuǎn)換為 TreeView 可以表示的形式。
二、使用TreeView控件實(shí)現(xiàn)的二叉樹(shù)類(lèi)及其方法
創(chuàng)建一個(gè)C#的Windows Forms應(yīng)用程序,并在這個(gè)應(yīng)用程序中包含一個(gè)BinaryTree類(lèi)和一個(gè)窗體(Form1),該窗體包含一個(gè)TreeView控件來(lái)顯示二叉樹(shù)的內(nèi)容。
下面是一個(gè)簡(jiǎn)化的示例程序,它包括了一個(gè)基本的BinaryTree類(lèi)和一個(gè)使用TreeView控件來(lái)顯示二叉樹(shù)的Windows Forms窗體。
1.首先,定義BinaryTree類(lèi)和BinaryTreeNode類(lèi):
/// <summary> /// 定義BinaryTreeNode<T>類(lèi) /// 泛型約束:可比較 /// </summary> public class BinaryTreeNode<T>(T value) where T : IComparable<T> { public T Value { get; set; } = value; public BinaryTreeNode<T>? Left { get; set; } = null; public BinaryTreeNode<T>? Right { get; set; } = null; } /// <summary> /// 定義BinaryTree<T>類(lèi) /// 泛型約束:可比較 /// </summary> public class BinaryTree<T> where T : IComparable<T> { private BinaryTreeNode<T>? _root; public BinaryTree() { _root = null; } /// <summary> /// 添加節(jié)點(diǎn)的Add(T value)方法 /// </summary> public void Add(T value) { _root = BinaryTree<T>.Add(value, _root!); } private static BinaryTreeNode<T> Add(T value, BinaryTreeNode<T> currentNode) { if (currentNode == null) { return new BinaryTreeNode<T>(value); } if (value.CompareTo(currentNode.Value) < 0) { currentNode.Left = BinaryTree<T>.Add(value, currentNode.Left!); } else if (value.CompareTo(currentNode.Value) > 0) { currentNode.Right = BinaryTree<T>.Add(value, currentNode.Right!); } return currentNode; } /// <summary> /// 將二叉樹(shù)轉(zhuǎn)換為 TreeView 控件的節(jié)點(diǎn) /// </summary> public void PopulateTreeView(TreeView treeView) { treeView.Nodes.Clear(); if (_root != null) { treeView.Nodes.Add(BinaryTree<T>.CreateTreeNode(_root)); } } private static TreeNode CreateTreeNode(BinaryTreeNode<T> node) { TreeNode treeNode = new(node.Value.ToString()); if (node.Left != null) { treeNode.Nodes.Add(BinaryTree<T>.CreateTreeNode(node.Left));// 遞歸添加左子樹(shù) } if (node.Right != null) { treeNode.Nodes.Add(BinaryTree<T>.CreateTreeNode(node.Right));// 遞歸添加右子樹(shù) } return treeNode; } }
2.接著,創(chuàng)建窗體Form1并添加一個(gè)TreeView控件:
public partial class Form1 : Form { private BinaryTree<int>? binaryTree; public Form1() { InitializeComponent(); //binaryTree = new BinaryTree<int>(); } private void Form1_Load(object sender, EventArgs e) { // 初始化二叉樹(shù)并添加節(jié)點(diǎn) binaryTree = new BinaryTree<int>(); binaryTree.Add(5); binaryTree.Add(3); binaryTree.Add(7); binaryTree.Add(2); binaryTree.Add(4); binaryTree.Add(6); binaryTree.Add(8); treeView1.Refresh(); // 將二叉樹(shù)轉(zhuǎn)換為 TreeView 控件的節(jié)點(diǎn)并顯示 binaryTree.PopulateTreeView(treeView1); } }
3.運(yùn)行結(jié)果
把上面兩個(gè)類(lèi)放在同一個(gè)命名空間下,運(yùn)行結(jié)果:
以上就是C#使用TreeView控件實(shí)現(xiàn)的二叉樹(shù)泛型節(jié)點(diǎn)類(lèi)及其方法的詳細(xì)內(nèi)容,更多關(guān)于C# TreeView二叉樹(shù)類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#中backgroundWorker類(lèi)的用法詳解
這篇文章主要介紹了C#使用backgroundWorker實(shí)現(xiàn)多線程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07C#根據(jù)年月日計(jì)算星期幾的函數(shù)小例子
這篇文章介紹了C#根據(jù)年月日計(jì)算星期幾的函數(shù)小例子,有需要的朋友可以參考一下2013-07-07C#中Backgroundworker與Thread的區(qū)別
本文主要介紹了C#中Backgroundworker與Thread的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C#使用linq計(jì)算執(zhí)行元素在列表中出現(xiàn)次數(shù)的方法
這篇文章主要介紹了C#使用linq計(jì)算執(zhí)行元素在列表中出現(xiàn)次數(shù)的方法,涉及C#使用linq擴(kuò)展進(jìn)行列表查詢(xún)的技巧,需要的朋友可以參考下2015-04-04C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動(dòng)管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時(shí)器進(jìn)行多線程的自動(dòng)管理,實(shí)例分析了C#使用timer定時(shí)器類(lèi)實(shí)現(xiàn)針對(duì)多線程的自動(dòng)管理功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#設(shè)置自定義文件圖標(biāo)實(shí)現(xiàn)雙擊啟動(dòng)(修改注冊(cè)表)
這篇文章介紹的是利用C#設(shè)置自定義文件圖標(biāo),然后實(shí)現(xiàn)雙擊啟動(dòng)的功能,文章給出了示例代碼,介紹的很詳細(xì),有需要的可以參考借鑒。2016-08-08Expression操作運(yùn)算符、表達(dá)式和操作方法總結(jié)
這篇文章詳細(xì)介紹了Expression操作運(yùn)算符、表達(dá)式和操作方法總結(jié),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01