Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Drzewo
- {
- class Drzewo
- {
- public Węzeł korzeń;
- public void WypiszPoziomami()
- {
- Queue kolejka = new Queue();
- Queue tmp = new Queue();
- kolejka.Enqueue(korzeń);
- int poziom = 0;
- Console.Write("Poziom: Węzły");
- Console.Write('\n');
- while (kolejka.Count != 0)
- {
- Console.Write(poziom + ": ");
- while (kolejka.Count != 0)
- {
- Węzeł temporary = (Węzeł)kolejka.Dequeue();
- Console.Write(temporary.wartosc + " ");
- for (int j = 0; j < temporary.dzieci.Count; j++)
- {
- tmp.Enqueue(temporary.dzieci[j]);
- }
- }
- Console.Write('\n');
- while(tmp.Count != 0)
- {
- kolejka.Enqueue(tmp.Dequeue());
- }
- poziom++;
- }
- }
- public void WypiszPoziomamiUproszczone()
- {
- Queue kolejka = new Queue();
- kolejka.Enqueue(korzeń);
- while(kolejka.Count != 0)
- {
- Węzeł wypisywany = (Węzeł)kolejka.Dequeue();
- Węzeł doPorownania = wypisywany;
- Console.Write(wypisywany.wartosc + " ");
- }
- }
- }
- class Węzeł
- {
- public string wartosc;
- public ArrayList dzieci;
- public Węzeł(string wart)
- {
- wartosc = wart;
- dzieci = new ArrayList();
- }
- }
- class Program
- {
- public static void Dodaj(Węzeł k, Węzeł w)
- {
- k.dzieci.Add(w);
- }
- static void Main(string[] args)
- {
- Drzewo drzewko = new Drzewo();
- Węzeł A = new Węzeł("A");
- Węzeł B = new Węzeł("B");
- Węzeł C = new Węzeł("C");
- Węzeł D = new Węzeł("D");
- Węzeł E = new Węzeł("E");
- Węzeł F = new Węzeł("F");
- drzewko.korzeń = A;
- Dodaj(A, B);
- Dodaj(A, C);
- Dodaj(B, D);
- Dodaj(B, E);
- Dodaj(D, F);
- drzewko.WypiszPoziomami();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement