Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace KopiecWezel
- {
- class Kopiec
- {
- Wezel korzen = null;
- Kopiec(int p_wartosc)
- {
- this.korzen = new Wezel(p_wartosc);
- }
- Kopiec(int[] tab)
- {
- this.korzen = new Wezel(tab);
- }
- //analogicznie w domu : dzieckoL, dodajdzieckoP, dodajwartosc(int), dodaj wartosc(int[]) , wywolac na korzeniu te metody
- ///***PRACA DOMOWA***///
- public Kopiec dodajDzieckoL(int p_wartosc)
- {
- Wezel dziecko = new Wezel(p_wartosc);
- korzen.dzieckoL = dziecko;
- korzen.rodzic = this.korzen;
- return this;
- }
- public Kopiec dodajDzieckoP(int p_wartosc)
- {
- Wezel dziecko = new Wezel(p_wartosc);
- korzen.dzieckoP = dziecko;
- korzen.rodzic = this.korzen;
- return this;
- }
- Kopiec dodajWartosc(int p_wartosc)
- {
- Wezel dziecko = new Wezel(p_wartosc);
- Wezel tmp = this.korzen;
- while (true)
- {
- if (tmp.wartosc < dziecko.wartosc)
- {
- if (korzen.dzieckoP == null)
- {
- tmp.dodajDzieckoP(dziecko.wartosc);
- break;
- }
- else
- tmp = tmp.dzieckoP;
- }
- else
- if (korzen.dzieckoL == null)
- {
- tmp.dodajDzieckoL(dziecko.wartosc);
- break;
- }
- else
- tmp = tmp.dzieckoL;
- }
- return this;
- }
- public void dodajWartosc(int[] tab)
- {
- if (tab == null)
- {
- return;
- }
- for (int i = 0; i < tab.Length; i++)
- {
- this.dodajWartosc(tab[i]);
- }
- }
- ///***PRACA DOMOWA***///
- int usunNajmniejszaWartosc()
- {
- Wezel tmp = this.korzen;
- //if korzen != null dopiero to wszystko
- while (tmp.dzieckoL != null) //szuakmy najbardziej na lewo bo jest najmniejsza wartoลฤ
- {
- tmp = tmp.dzieckoL;
- }
- if (tmp.rodzic != null)
- {
- if (tmp.dzieckoP != null)
- {
- tmp.rodzic.dzieckoL = tmp.dzieckoP;
- tmp.dzieckoP.rodzic = tmp.rodzic;
- }
- else
- {
- tmp.rodzic.dzieckoL = null;
- }
- }
- else
- {
- if (tmp.dzieckoP != null)
- {
- this.korzen = tmp.dzieckoP;
- }
- else
- {
- this.korzen = null;
- }
- }
- return tmp.wartosc;
- }
- int[] sortujRosnaco()
- {
- List<int> lista = new List<int>();
- while (this.korzen != null)
- {
- lista.Add(this.usunNajmniejszaWartosc());
- }
- return lista.ToArray();
- }
- int[] sortujMalejaco()
- {
- List<int> lista = this.sortujRosnaco().ToList();
- lista.Reverse();
- return lista.ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement