using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp4 { class Wezel { public int wartosc; public Wezel lewy; public Wezel prawy; public void pokaz() { Console.Write("["); Console.Write(wartosc); Console.Write("]"); } } class Tree { public Wezel korzen; public Tree() { korzen = null; } public Wezel ZwrocKorzen() { return korzen; } public void Wstaw(int id) { Wezel newWezel = new Wezel(); newWezel.wartosc = id; if (korzen == null) korzen = newWezel; else { Wezel obecny = korzen; Wezel rodzic; while (true) { rodzic = obecny; if (id < obecny.wartosc) { obecny = obecny.lewy; if (obecny == null) { rodzic.lewy = newWezel; return; } } else { obecny = obecny.prawy; if (obecny == null) { rodzic.prawy = newWezel; return; } } } } } public void Preorder(Wezel Root) { if (Root != null) { Console.Write(Root.wartosc + " "); Preorder(Root.lewy); Preorder(Root.prawy); } } public void Inorder(Wezel Root) { if (Root != null) { Inorder(Root.lewy); Console.Write(Root.wartosc + " "); Inorder(Root.prawy); } } public void Postorder(Wezel Root) { if (Root != null) { Postorder(Root.lewy); Postorder(Root.prawy); Console.Write(Root.wartosc + " "); } } } class Program { static void Main(string[] args) { Tree Test = new Tree(); Test.Wstaw(14); Test.Wstaw(33); Test.Wstaw(67); Test.Wstaw(43); Test.Wstaw(32); Test.Wstaw(88); Test.Wstaw(43); Test.Wstaw(32); Test.Wstaw(12); Test.Wstaw(32); Test.Wstaw(54); Test.Wstaw(32); Test.Wstaw(65); Test.Wstaw(13); Console.WriteLine("Inorder : "); Test.Inorder(Test.ZwrocKorzen()); Console.WriteLine(" "); Console.WriteLine(); Console.WriteLine("Preorder : "); Test.Preorder(Test.ZwrocKorzen()); Console.WriteLine(" "); Console.WriteLine(); Console.WriteLine("Postorder : "); Test.Postorder(Test.ZwrocKorzen()); Console.WriteLine(" "); Console.ReadLine(); } } }