using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { 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; } } } } } public void Preorder(Węzeł Root) { if (Root != null) { Console.Write(Root.wartość + " "); Preorder(Root.lewy); Preorder(Root.prawy); } } public void Inorder(Węzeł Root) { if (Root != null) { Inorder(Root.lewy); Console.Write(Root.wartość + " "); Inorder(Root.prawy); } } public void Postorder(Węzeł Root) { if (Root != null) { Postorder(Root.lewy); Postorder(Root.prawy); Console.Write(Root.wartość + " "); } } } class Program { static void Main(string[] args) { Drzewo d = new Drzewo(); 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); Console.WriteLine("Inorder: "); d.Inorder(d.Rodzic()); Console.WriteLine(" "); Console.WriteLine(); Console.WriteLine("Preorder: "); d.Preorder(d.Rodzic()); Console.WriteLine(" "); Console.WriteLine(); Console.WriteLine("Postorder: "); d.Postorder(d.Rodzic()); Console.WriteLine(" "); Console.ReadLine(); } } }