Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zadanie2 Y2
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Zad2
- {
- class Program
- {
- static void Main(string[] args)
- {
- DrzewoBST D = new DrzewoBST();
- /*
- D.DodajElement(4);
- D.DodajElement(1);
- D.DodajElement(9);
- D.DodajElement(3);
- D.DodajElement(0);
- D.DodajElement(8);
- D.DodajElement(2);
- */
- D.DodajElement(3);
- D.DodajElement(2);
- D.DodajElement(1);
- //D.DodajElement(4);
- //D.DodajElement(5);
- Console.WriteLine(D.Różnica());
- D.Wyświetl();
- Console.ReadKey();
- }
- class DrzewoBST
- {
- Element Korzeń;
- public void DodajElement(int i)
- {
- if (Korzeń == null)
- Korzeń = new Element(i);
- else
- {
- Element e = Korzeń;
- while(e != null)
- {
- if (i < e.war)
- {
- if (e.lewy == null)
- {
- e.lewy = new Element(i);
- return;
- }
- else
- e = e.lewy;
- }
- else
- {
- if (e.prawy == null)
- {
- e.prawy = new Element(i);
- return;
- }
- else
- e = e.prawy;
- }
- }
- }
- }
- public void Wyświetl()
- {
- Wyświetl(Korzeń);
- }
- void Wyświetl(Element e)
- {
- Console.Write($"{e.war} ");
- if (e.lewy != null)
- Wyświetl(e.lewy);
- if (e.prawy != null)
- Wyświetl(e.prawy);
- }
- public int Różnica()
- {
- int max = Max(Korzeń);
- int min = Min(Korzeń);
- if (czyJedenLiść(Korzeń))
- return max;
- else
- return max - min;
- }
- int Max(Element e)
- {
- int wysokość = 0;
- if (e == null)
- return 0;
- if (e.lewy != null)
- wysokość = 1 + Max(e.lewy);
- if (e.prawy != null)
- wysokość = Math.Max(wysokość, 1 + Max(e.prawy));
- return wysokość;
- }
- int Min(Element e)
- {
- int wysokość = 0;
- if (e == null)
- return 0;
- if (e.lewy != null && e.prawy != null)
- wysokość = Math.Min(1 + Min(e.lewy), 1 + Min(e.prawy));
- else if (e.lewy != null)
- wysokość = 1 + Min(e.lewy);
- else if (e.prawy != null)
- wysokość = 1 + Min(e.prawy);
- return wysokość;
- }
- bool czyJedenLiść(Element e)
- {
- if (e.lewy != null && e.prawy != null)
- return false;
- else if (e.lewy != null)
- return czyJedenLiść(e.lewy);
- else if (e.prawy != null)
- return czyJedenLiść(e.prawy);
- else
- return true;
- }
- }
- class Element
- {
- public int war;
- public Element lewy;
- public Element prawy;
- public Element(int war)
- {
- this.war = war;
- }
- }
- }
- }
- zadanie 1 y2
- namespace Zad1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Kopiec K1 = new Kopiec();
- K1.Dodaj(1);
- K1.Dodaj(3);
- K1.Dodaj(5);
- K1.Dodaj(1);
- K1.Dodaj(7);
- K1.Dodaj(2);
- K1.Dodaj(9);
- K1.Dodaj(12);
- K1.Dodaj(0);
- K1.wyświetl();
- if(K1.sprawdzCzyKopiecMinimalny())
- Console.WriteLine("Kopiec minimalny");
- else
- Console.WriteLine("To sie nie wyświetli");
- Console.ReadKey();
- }
- class Kopiec
- {
- int wielkość = 0;
- int[] tablica = new int[30];
- public Kopiec()
- {
- }
- public void Dodaj(int war)
- {
- if (wielkość + 1 < tablica.Length)
- {
- tablica[wielkość] = war;
- wielkość++;
- }
- buduj();
- }
- public void napraw(int i)
- {
- int d1 = i * 3 + 1;
- int d2 = i * 3 + 2;
- int d3 = i * 3 + 3;
- int naj = i;
- if (d1 < wielkość && tablica[d1] < tablica[naj])
- {
- naj = d1;
- }
- if (d2 < wielkość && tablica[d2] < tablica[naj])
- {
- naj = d2;
- }
- if (d3 < wielkość && tablica[d3] < tablica[naj])
- {
- naj = d3;
- }
- if (naj != i)
- {
- int tmp = tablica[i];
- tablica[i] = tablica[naj];
- tablica[naj] = tmp;
- napraw(naj);
- }
- }
- public void buduj()
- {
- for (int i = wielkość / 2; i >= 0; i--)
- {
- napraw(i);
- }
- }
- public void wyświetl()
- {
- for (int i = 0; i < wielkość; i++)
- {
- Console.Write(tablica[i] + " ");
- }
- Console.WriteLine();
- }
- public bool sprawdzCzyKopiecMinimalny()
- {
- for (int i = wielkość / 2; i >= 0; i--)
- {
- int d1 = i * 3 + 1;
- int d2 = i * 3 + 2;
- int d3 = i * 3 + 3;
- if (d1 < wielkość && tablica[d1] < tablica[i])
- {
- return false;
- }
- if (d2 < wielkość && tablica[d2] < tablica[i])
- {
- return false;
- }
- if (d3 < wielkość && tablica[d3] < tablica[i])
- {
- return false;
- }
- }
- return true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement