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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } class Węzeł { public int wartość; public Węzeł lewy; public Węzeł prawy; } class Drzewo { public Węzeł root; public Drzewo() { root = null; } public Węzeł Rodzic() { return root; } public void Push(int id) { Węzeł nowy = new Węzeł(); nowy.wartość = id; if (root == null) root = nowy; else { Węzeł tmp = root; Węzeł rodzic; while (true) { rodzic = tmp; if (id < tmp.wartość) { tmp = tmp.lewy; if (tmp == null) { rodzic.lewy = nowy; return; } } else { tmp = tmp.prawy; if (tmp == null) { rodzic.prawy = nowy; return; } } } } } String text = ""; public String Preorder(Węzeł Root) { if (Root != null) { text += Root.wartość + " "; Preorder(Root.lewy); Preorder(Root.prawy); } return text; } public String Inorder(Węzeł Root) { if (Root != null) { Inorder(Root.lewy); text += Root.wartość + " "; Inorder(Root.prawy); } return text; } public String Postorder(Węzeł Root) { if (Root != null) { Postorder(Root.lewy); Postorder(Root.prawy); text += Root.wartość + " "; } return text; } public void generatedata(Drzewo d) { d.Push(10); d.Push(5); d.Push(1); d.Push(0); d.Push(2); d.Push(7); d.Push(8); d.Push(15); d.Push(12); d.Push(11); d.Push(20); } } private void button1_Click(object sender, EventArgs e) { Drzewo d = new Drzewo(); d.generatedata(d); textBox1.Text += "INORDER -> "; String text = d.Inorder(d.Rodzic()); textBox1.Text += text; textBox1.Text += "rn"; } private void button2_Click(object sender, EventArgs e) { Drzewo d = new Drzewo(); d.generatedata(d); textBox1.Text += "PREORDER -> "; String text = d.Preorder(d.Rodzic()); textBox1.Text += text; textBox1.Text += "rn"; } private void button3_Click(object sender, EventArgs e) { Drzewo d = new Drzewo(); d.generatedata(d); textBox1.Text += "POSTORDER -> "; String text = d.Postorder(d.Rodzic()); textBox1.Text += text; textBox1.Text += "rn"; } } }