.
樹(TREE)是資料結構中常用的儲存方式之一
屬於 Linked List的延伸,特徵為只有一個根(ROOT) 且不具循環性 (Cycle)
在樹中若要從root尋找特定node,一定只存在一條路徑(path)。
每個node只會有一個parent(父節點)。
以下為最簡單的 樹狀結構建立 ,此程式只有基本的連結,尚無加入階層架構
如需轉為二元樹 ALV樹 等,再以此程式做延伸
CLASS NTree
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace Tree_test
- {
-
- delegate void TreeVisitor<T>(T nodeData);
-
- class NTree< T >
- {
- public T data;
- private LinkedList<NTree<T>> children;
- int member_count = 0;
-
- public NTree(T data)
- {
- this.data = data;
- children = new LinkedList<NTree<T>>();
- }
-
- public void AddChild(T data)
- {
- children.AddFirst(new NTree<T>(data));
- member_count++;
- }
-
- public int get_tree_member()
- {
- return member_count;
- }
-
- public NTree<T> GetChild(int i)
- {
- foreach (NTree<T> n in children)
- if (--i == 0)
- return n;
- return null;
- }
-
- public void DeleteChild()
- {
- if (member_count == 0)
- {
- MessageBox.Show("Tree have no leaves.");
- }
- else
- {
- member_count--;
- children.RemoveLast();
- }
- }
-
- public void Traverse(NTree<T> node, TreeVisitor<T> visitor)
- {
- visitor(node.data);
- foreach (NTree<T> kid in node.children)
- Traverse(kid, visitor);
- }
- }
- }
CLASS MAIN
- 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 Tree_test
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- NTree<String> _tree = new NTree<String>("ROOT");
- int width_pos = 10, height_pos = 100;
- int _last;
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- renew_data();
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- _tree.AddChild(textBox1.Text);
- renew_data();
- }
-
- private void renew_data()
- {
- //刪除前一次資料
- for (int i = 1; i <= _tree.get_tree_member()-1; i++)
- {
- this.Controls.RemoveByKey(i.ToString());
- this.Controls.RemoveByKey(i.ToString()+"P");
- }
-
- //更新頁面
- width_pos = 10;
- height_pos = 100;
- for (int i = 1; i <= _tree.get_tree_member(); i++)
- {
- NTree<String> result = _tree.GetChild(i);
- TextBox txt_data = new TextBox();
- PictureBox pic = new PictureBox();
- pic.Size = new Size(61,32);
- txt_data.Size = new Size(100, 10);
- if ((i-1) % 4 == 0) { height_pos += 40; width_pos = 10; }
- txt_data.Location = new Point(width_pos, height_pos);
- pic.Location = new Point(width_pos += 100, height_pos);
- pic.Name = i.ToString()+"P";
- pic.Image = Properties.Resources.arrow;
- txt_data.Text = result.data.ToString();
- txt_data.Name = i.ToString();
- _last = i ;
- Controls.Add(txt_data);
- Controls.Add(pic);
- width_pos += 100;
- }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- _tree.DeleteChild();
- Controls.RemoveByKey(_last.ToString()); //delete last index
- Controls.RemoveByKey(_last.ToString()+"P");
- _last--;
- }
- }
- }
執行結果
加入點一
加入點2
加入6個點
刪除點1 (刪除一律從尾端開始)
刪除點2
相關資料結構