Advertisement
Adik28

Klasa kopiec zrobiona

Nov 24th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace KopiecWezel
  8. {
  9.     class Kopiec
  10.     {
  11.         Wezel korzen = null;
  12.  
  13.         Kopiec(int p_wartosc)
  14.         {
  15.             this.korzen = new Wezel(p_wartosc);
  16.         }
  17.  
  18.         Kopiec(int[] tab)
  19.         {
  20.             this.korzen = new Wezel(tab);
  21.         }
  22.  
  23.         //analogicznie  w domu : dzieckoL, dodajdzieckoP, dodajwartosc(int), dodaj wartosc(int[]) , wywolac na          korzeniu te metody
  24.         ///***PRACA DOMOWA***///
  25.  
  26.         public Kopiec dodajDzieckoL(int p_wartosc)
  27.         {
  28.             Wezel dziecko = new Wezel(p_wartosc);
  29.             korzen.dzieckoL = dziecko;
  30.             korzen.rodzic = this.korzen;
  31.             return this;
  32.         }
  33.  
  34.         public Kopiec dodajDzieckoP(int p_wartosc)
  35.         {
  36.             Wezel dziecko = new Wezel(p_wartosc);
  37.             korzen.dzieckoP = dziecko;
  38.             korzen.rodzic = this.korzen;
  39.             return this;
  40.         }
  41.  
  42.         Kopiec dodajWartosc(int p_wartosc)
  43.         {
  44.             Wezel dziecko = new Wezel(p_wartosc);
  45.             Wezel tmp = this.korzen;
  46.             while (true)
  47.             {
  48.                 if (tmp.wartosc < dziecko.wartosc)
  49.                 {
  50.                     if (korzen.dzieckoP == null)
  51.                     {
  52.                         tmp.dodajDzieckoP(dziecko.wartosc);
  53.                         break;
  54.                     }
  55.                     else
  56.                         tmp = tmp.dzieckoP;
  57.                 }
  58.                 else
  59.                     if (korzen.dzieckoL == null)
  60.                     {
  61.                         tmp.dodajDzieckoL(dziecko.wartosc);
  62.                         break;
  63.                     }
  64.                     else
  65.                         tmp = tmp.dzieckoL;
  66.             }
  67.             return this;
  68.  
  69.         }
  70.  
  71.         public void dodajWartosc(int[] tab)
  72.         {
  73.             if (tab == null)
  74.             {
  75.                 return;
  76.             }
  77.  
  78.             for (int i = 0; i < tab.Length; i++)
  79.             {
  80.                 this.dodajWartosc(tab[i]);
  81.             }
  82.  
  83.         }
  84.         ///***PRACA DOMOWA***///
  85.  
  86.         int usunNajmniejszaWartosc()
  87.         {
  88.             Wezel tmp = this.korzen;
  89.  
  90.             //if korzen != null dopiero to wszystko
  91.  
  92.             while (tmp.dzieckoL != null)  //szuakmy najbardziej na lewo bo jest najmniejsza wartoล›ฤ‡
  93.             {
  94.                 tmp = tmp.dzieckoL;
  95.             }
  96.  
  97.             if (tmp.rodzic != null)
  98.             {
  99.                 if (tmp.dzieckoP != null)
  100.                 {
  101.                     tmp.rodzic.dzieckoL = tmp.dzieckoP;
  102.                     tmp.dzieckoP.rodzic = tmp.rodzic;
  103.                 }
  104.                 else
  105.                 {
  106.                     tmp.rodzic.dzieckoL = null;
  107.                 }
  108.             }
  109.             else
  110.             {
  111.                 if (tmp.dzieckoP != null)
  112.                 {
  113.                     this.korzen = tmp.dzieckoP;
  114.                 }
  115.                 else
  116.                 {
  117.                     this.korzen = null;
  118.                 }
  119.             }
  120.  
  121.             return tmp.wartosc;
  122.         }
  123.  
  124.         int[] sortujRosnaco()
  125.         {
  126.             List<int> lista = new List<int>();
  127.             while (this.korzen != null)
  128.             {
  129.                 lista.Add(this.usunNajmniejszaWartosc());
  130.             }
  131.             return lista.ToArray();
  132.         }
  133.  
  134.         int[] sortujMalejaco()
  135.         {
  136.             List<int> lista = this.sortujRosnaco().ToList();
  137.             lista.Reverse();
  138.             return lista.ToArray();
  139.         }
  140.  
  141.  
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement