C#使用TreeView控件實現(xiàn)的二叉樹泛型節(jié)點類及其方法
一、涉及到的知識點
1.TreeView控件
TreeView 控件在 C# 中主要用于顯示分層結(jié)構(gòu)的數(shù)據(jù)。這通常是一個文件系統(tǒng)的表示,但也可以是任何具有父子關(guān)系的數(shù)據(jù)集合。TreeView 控件在 Windows Forms 應(yīng)用程序中非常常見,允許用戶通過點擊箭頭來展開或折疊節(jié)點,以查看或隱藏子節(jié)點。
在 C# Windows Forms 應(yīng)用程序中使用 TreeView 控件的基本步驟:
(1)添加 TreeView 控件到 Form:
在設(shè)計視圖中,從工具箱中拖動 TreeView 控件到 Form 上。
或者在代碼中,使用 Controls.Add 方法將 TreeView 添加到 Form。
(2)添加節(jié)點:
使用 Nodes 屬性添加根節(jié)點。
使用 Nodes.Add 方法為根節(jié)點添加子節(jié)點。
也可以為子節(jié)點再添加子節(jié)點,形成多級層次結(jié)構(gòu)。
(3)為節(jié)點添加文本和圖像:
使用 Text 屬性為節(jié)點設(shè)置文本。
使用 ImageIndex 和 SelectedImageIndex 屬性為節(jié)點設(shè)置圖像。這些屬性通常與 ImageList 控件結(jié)合使用,后者可以包含要在 TreeView 中顯示的圖像。
(4)事件處理:
AfterSelect:當(dāng)用戶選擇一個節(jié)點后觸發(fā)。
BeforeSelect:在用戶選擇一個節(jié)點之前觸發(fā),允許你取消選擇。
NodeMouseClick:當(dāng)用戶點擊一個節(jié)點時觸發(fā)。
其他事件,如 AfterExpand、BeforeExpand 等。
(5)自定義外觀和行為:
通過設(shè)置 TreeView 的屬性,如 LineColor、ExpandCollapseColor、ScrollAlwaysVisible 等,可以自定義其外觀和行為。
2.TreeView控件的應(yīng)用示例
該實例展示了如何在 Windows Forms 應(yīng)用程序中添加一個 TreeView 控件并為其添加節(jié)點:
// 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é)點1"); treeView1.Nodes[0].Nodes.Add("子節(jié)點1"); treeView1.Nodes[0].Nodes.Add("子節(jié)點2"); treeView1.Nodes.Add("根節(jié)點2"); } private void TreeView1_AfterSelect(object? sender, TreeViewEventArgs e) { MessageBox.Show("你選擇了節(jié)點: " + 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; } }
這個例子創(chuàng)建了一個簡單的 TreeView,有兩個根節(jié)點,其中一個根節(jié)點有兩個子節(jié)點。當(dāng)用戶選擇一個節(jié)點時,會顯示一個消息框,顯示所選擇的節(jié)點的文本。
3.使用TreeView控件實現(xiàn)的二叉樹類及其方法的可行性
在C#中,TreeView 控件通常用于展示層次結(jié)構(gòu)的數(shù)據(jù),但它并不直接支持二叉樹結(jié)構(gòu)。TreeView 的節(jié)點(TreeNode)類并不限制其子節(jié)點的數(shù)量,這意味著你可以為每個節(jié)點添加任意數(shù)量的子節(jié)點,這更適合于表示一般的樹形結(jié)構(gòu)而不是二叉樹。
然而,如果想用 TreeView 控件來展示二叉樹,可以自定義一個二叉樹類,然后將其轉(zhuǎn)換為 TreeView 可以表示的形式。
二、使用TreeView控件實現(xiàn)的二叉樹類及其方法
創(chuàng)建一個C#的Windows Forms應(yīng)用程序,并在這個應(yīng)用程序中包含一個BinaryTree類和一個窗體(Form1),該窗體包含一個TreeView控件來顯示二叉樹的內(nèi)容。
下面是一個簡化的示例程序,它包括了一個基本的BinaryTree類和一個使用TreeView控件來顯示二叉樹的Windows Forms窗體。
1.首先,定義BinaryTree類和BinaryTreeNode類:
/// <summary> /// 定義BinaryTreeNode<T>類 /// 泛型約束:可比較 /// </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>類 /// 泛型約束:可比較 /// </summary> public class BinaryTree<T> where T : IComparable<T> { private BinaryTreeNode<T>? _root; public BinaryTree() { _root = null; } /// <summary> /// 添加節(jié)點的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> /// 將二叉樹轉(zhuǎn)換為 TreeView 控件的節(jié)點 /// </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));// 遞歸添加左子樹 } if (node.Right != null) { treeNode.Nodes.Add(BinaryTree<T>.CreateTreeNode(node.Right));// 遞歸添加右子樹 } return treeNode; } }
2.接著,創(chuàng)建窗體Form1并添加一個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) { // 初始化二叉樹并添加節(jié)點 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(); // 將二叉樹轉(zhuǎn)換為 TreeView 控件的節(jié)點并顯示 binaryTree.PopulateTreeView(treeView1); } }
3.運行結(jié)果
把上面兩個類放在同一個命名空間下,運行結(jié)果:
以上就是C#使用TreeView控件實現(xiàn)的二叉樹泛型節(jié)點類及其方法的詳細內(nèi)容,更多關(guān)于C# TreeView二叉樹類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#中Backgroundworker與Thread的區(qū)別
本文主要介紹了C#中Backgroundworker與Thread的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C#使用linq計算執(zhí)行元素在列表中出現(xiàn)次數(shù)的方法
這篇文章主要介紹了C#使用linq計算執(zhí)行元素在列表中出現(xiàn)次數(shù)的方法,涉及C#使用linq擴展進行列表查詢的技巧,需要的朋友可以參考下2015-04-04C#多線程學(xué)習(xí)之(五)使用定時器進行多線程的自動管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時器進行多線程的自動管理,實例分析了C#使用timer定時器類實現(xiàn)針對多線程的自動管理功能,非常具有實用價值,需要的朋友可以參考下2015-04-04C#設(shè)置自定義文件圖標實現(xiàn)雙擊啟動(修改注冊表)
這篇文章介紹的是利用C#設(shè)置自定義文件圖標,然后實現(xiàn)雙擊啟動的功能,文章給出了示例代碼,介紹的很詳細,有需要的可以參考借鑒。2016-08-08Expression操作運算符、表達式和操作方法總結(jié)
這篇文章詳細介紹了Expression操作運算符、表達式和操作方法總結(jié),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01