Facebook
From AISD, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp10
  8. {
  9.     public class DrzewoBST
  10.     {
  11.         BST root = null;
  12.         BST tmp;
  13.         int dlugosc = 0;
  14.  
  15.         public void Push(int liczba)
  16.         {
  17.             BST nowy = new BST();
  18.             nowy.wartosc = liczba;
  19.             tmp = root;
  20.             if (root == null) root = nowy;
  21.             else{
  22.                 tmp = root;
  23.                 if (nowy.wartosc < tmp.wartosc)
  24.                 {
  25.                     while (tmp.lewe != null)
  26.                     {
  27.                         tmp = tmp.lewe;
  28.                     }
  29.                     tmp.lewe = nowy;
  30.                     dlugosc++;
  31.                 }
  32.                 if (nowy.wartosc > tmp.wartosc)
  33.                 {
  34.                     while (tmp.prawe != null)
  35.                     {
  36.                         tmp = tmp.prawe;
  37.                     }
  38.                     tmp.prawe = nowy;
  39.                     dlugosc++;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         public void Pop(int liczba)
  45.         {
  46.             tmp = root;
  47.             while(tmp != null)
  48.             {
  49.                 tmp = tmp.lewe;
  50.                 if (root.wartosc == liczba)
  51.                 {
  52.                     Console.WriteLine("FOUND");
  53.                     tmp = null;
  54.                 }
  55.             }
  56.         }
  57.  
  58.  
  59.         public void Lenght()
  60.         {
  61.             Console.WriteLine("Ilosc: " + dlugosc);
  62.         }
  63.  
  64.         public void Show()
  65.         {
  66.             Console.WriteLine("--------------------");
  67.             Pokaz(root);
  68.         }
  69.  
  70.         public void Pokaz(BST Root)
  71.         {
  72.             if (Root != null)
  73.             {
  74.                 Console.WriteLine(Root.wartosc);
  75.                 Pokaz(Root.lewe);
  76.                 Pokaz(Root.prawe);
  77.             }
  78.         }
  79.     }
  80.  
  81.     public class BST
  82.     {
  83.         public BST lewe;
  84.         public BST prawe;
  85.         public int wartosc;
  86.     }
  87.  
  88.     class Program
  89.     {
  90.         static void Main(string[] args)
  91.         {
  92.             DrzewoBST Drzewo = new DrzewoBST();
  93.             Drzewo.Push(8);
  94.             Drzewo.Push(5);
  95.             Drzewo.Push(12);
  96.             Drzewo.Push(7);
  97.             Drzewo.Push(4);
  98.             Drzewo.Push(15);
  99.             Drzewo.Push(11);
  100.             Drzewo.Show();
  101.             Drzewo.Pop(4);
  102.             Drzewo.Show();
  103.             Console.ReadLine();
  104.         }
  105.     }
  106. }
  107.