Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace zad3
- {
- class Program
- {
- static void Main( string[] args )
- {
- Drzewo d = new Drzewo();
- d.glowa = new Element( 10 );
- d.glowa.DodajDzieci( new List<int>() { 2, 54, 6, 1, 65, 2 } );
- // Wypelnianie drzewa
- foreach( Element e in d.glowa.dzieci )
- {
- List<int> l = new List<int>();
- for( int i = 0; i < 3; i++ )
- l.Add( i + 1 );
- e.DodajDzieci( l );
- e.dzieci[ 0 ].DodajDzieci( new List<int>() { 100 } );
- e.dzieci[ 0 ].dzieci[ 0 ].DodajDzieci( new List<int>() { 200 } );
- }
- d.Wyswietl();
- Console.ReadKey();
- }
- class Drzewo
- {
- public Element glowa;
- public void Wyswietl()
- {
- if( glowa == null ) return;
- Queue<Element> q = new Queue<Element>();
- q.Enqueue( glowa );
- while( q.Count > 0 )
- {
- foreach( Element e in q.Peek().dzieci )
- q.Enqueue( e );
- Console.Write( q.Dequeue() + " " );
- }
- Console.WriteLine();
- }
- }
- class Element
- {
- public Element()
- {
- wartosc = 0;
- dzieci = new List<Element>();
- }
- public Element( int w )
- {
- wartosc = w;
- dzieci = new List<Element>();
- }
- public int wartosc;
- public List<Element> dzieci;
- public void DodajDzieci( List<int> lista )
- {
- foreach( int a in lista )
- dzieci.Add( new Element( a ) );
- }
- public override string ToString()
- {
- return wartosc.ToString();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment