Advertisement
Danielos168

kolos21y22y2

Jan 23rd, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. Zadanie2 Y2
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Zad2
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             DrzewoBST D = new DrzewoBST();
  15.             /*
  16.             D.DodajElement(4);
  17.             D.DodajElement(1);
  18.             D.DodajElement(9);
  19.             D.DodajElement(3);
  20.             D.DodajElement(0);
  21.             D.DodajElement(8);
  22.             D.DodajElement(2);
  23.             */
  24.             D.DodajElement(3);
  25.             D.DodajElement(2);
  26.             D.DodajElement(1);
  27.             //D.DodajElement(4);
  28.             //D.DodajElement(5);
  29.             Console.WriteLine(D.Różnica());
  30.  
  31.             D.Wyświetl();
  32.  
  33.             Console.ReadKey();
  34.         }
  35.  
  36.         class DrzewoBST
  37.         {
  38.             Element Korzeń;
  39.  
  40.             public void DodajElement(int i)
  41.             {
  42.                 if (Korzeń == null)
  43.                     Korzeń = new Element(i);
  44.                 else
  45.                 {
  46.                     Element e = Korzeń;
  47.                     while(e != null)
  48.                     {
  49.                         if (i < e.war)
  50.                         {
  51.                             if (e.lewy == null)
  52.                             {
  53.                                 e.lewy = new Element(i);
  54.                                 return;
  55.                             }
  56.                             else
  57.                                 e = e.lewy;
  58.                         }
  59.                         else
  60.                         {
  61.                             if (e.prawy == null)
  62.                             {
  63.                                 e.prawy = new Element(i);
  64.                                 return;
  65.                             }
  66.                             else
  67.                                 e = e.prawy;
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.             public void Wyświetl()
  73.             {
  74.                 Wyświetl(Korzeń);
  75.             }
  76.  
  77.             void Wyświetl(Element e)
  78.             {
  79.                 Console.Write($"{e.war} ");
  80.                 if (e.lewy != null)
  81.                     Wyświetl(e.lewy);
  82.                 if (e.prawy != null)
  83.                     Wyświetl(e.prawy);
  84.             }
  85.  
  86.             public int Różnica()
  87.             {
  88.                 int max = Max(Korzeń);
  89.                 int min = Min(Korzeń);
  90.                 if (czyJedenLiść(Korzeń))
  91.                     return max;
  92.                 else
  93.                     return max - min;
  94.             }
  95.  
  96.             int Max(Element e)
  97.             {
  98.                 int wysokość = 0;
  99.                 if (e == null)
  100.                     return 0;
  101.                 if (e.lewy != null)
  102.                     wysokość = 1 + Max(e.lewy);
  103.                 if (e.prawy != null)
  104.                     wysokość = Math.Max(wysokość, 1 + Max(e.prawy));
  105.                 return wysokość;
  106.             }
  107.  
  108.             int Min(Element e)
  109.             {
  110.                 int wysokość = 0;
  111.                 if (e == null)
  112.                     return 0;
  113.                 if (e.lewy != null && e.prawy != null)
  114.                     wysokość = Math.Min(1 + Min(e.lewy), 1 + Min(e.prawy));
  115.                 else if (e.lewy != null)
  116.                     wysokość = 1 + Min(e.lewy);
  117.                 else if (e.prawy != null)
  118.                     wysokość = 1 + Min(e.prawy);
  119.                 return wysokość;
  120.             }
  121.  
  122.             bool czyJedenLiść(Element e)
  123.             {
  124.                 if (e.lewy != null && e.prawy != null)
  125.                     return false;
  126.                 else if (e.lewy != null)
  127.                     return czyJedenLiść(e.lewy);
  128.                 else if (e.prawy != null)
  129.                     return czyJedenLiść(e.prawy);
  130.                 else
  131.                     return true;
  132.             }
  133.         }
  134.  
  135.         class Element
  136.         {
  137.             public int war;
  138.             public Element lewy;
  139.             public Element prawy;
  140.  
  141.             public Element(int war)
  142.             {
  143.                 this.war = war;
  144.             }
  145.         }
  146.     }
  147. }
  148. zadanie 1 y2
  149. namespace Zad1
  150. {
  151.     class Program
  152.     {
  153.         static void Main(string[] args)
  154.         {
  155.             Kopiec K1 = new Kopiec();
  156.             K1.Dodaj(1);
  157.             K1.Dodaj(3);
  158.             K1.Dodaj(5);
  159.             K1.Dodaj(1);
  160.             K1.Dodaj(7);
  161.             K1.Dodaj(2);
  162.             K1.Dodaj(9);
  163.             K1.Dodaj(12);
  164.             K1.Dodaj(0);
  165.  
  166.             K1.wyświetl();
  167.  
  168.             if(K1.sprawdzCzyKopiecMinimalny())
  169.                 Console.WriteLine("Kopiec minimalny");
  170.             else
  171.                 Console.WriteLine("To sie nie wyświetli");
  172.  
  173.             Console.ReadKey();
  174.         }
  175.  
  176.         class Kopiec
  177.         {
  178.             int wielkość = 0;
  179.             int[] tablica = new int[30];
  180.  
  181.             public Kopiec()
  182.             {
  183.             }
  184.  
  185.             public void Dodaj(int war)
  186.             {
  187.                 if (wielkość + 1 < tablica.Length)
  188.                 {
  189.                     tablica[wielkość] = war;
  190.                     wielkość++;
  191.                 }
  192.                 buduj();
  193.             }
  194.  
  195.             public void napraw(int i)
  196.             {
  197.                 int d1 = i * 3 + 1;
  198.                 int d2 = i * 3 + 2;
  199.                 int d3 = i * 3 + 3;
  200.                 int naj = i;
  201.                 if (d1 < wielkość && tablica[d1] < tablica[naj])
  202.                 {
  203.                     naj = d1;
  204.                 }
  205.                 if (d2 < wielkość && tablica[d2] < tablica[naj])
  206.                 {
  207.                     naj = d2;
  208.                 }
  209.                 if (d3 < wielkość && tablica[d3] < tablica[naj])
  210.                 {
  211.                     naj = d3;
  212.                 }
  213.                 if (naj != i)
  214.                 {
  215.                     int tmp = tablica[i];
  216.                     tablica[i] = tablica[naj];
  217.                     tablica[naj] = tmp;
  218.                     napraw(naj);
  219.                 }
  220.             }
  221.  
  222.             public void buduj()
  223.             {
  224.                 for (int i = wielkość / 2; i >= 0; i--)
  225.                 {
  226.                     napraw(i);
  227.                 }
  228.             }
  229.  
  230.             public void wyświetl()
  231.             {
  232.                 for (int i = 0; i < wielkość; i++)
  233.                 {
  234.                     Console.Write(tablica[i] + " ");
  235.                 }
  236.                 Console.WriteLine();
  237.             }
  238.  
  239.             public bool sprawdzCzyKopiecMinimalny()
  240.             {
  241.                 for (int i = wielkość / 2; i >= 0; i--)
  242.                 {
  243.                     int d1 = i * 3 + 1;
  244.                     int d2 = i * 3 + 2;
  245.                     int d3 = i * 3 + 3;
  246.                     if (d1 < wielkość && tablica[d1] < tablica[i])
  247.                     {
  248.                         return false;
  249.                     }
  250.                     if (d2 < wielkość && tablica[d2] < tablica[i])
  251.                     {
  252.                         return false;
  253.                     }
  254.                     if (d3 < wielkość && tablica[d3] < tablica[i])
  255.                     {
  256.                         return false;
  257.                     }
  258.                 }
  259.                 return true;
  260.             }
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement