Facebook
From Reliable Porcupine, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Tree
  8. {
  9.  
  10.  
  11.  
  12.     class Wezel
  13.     {
  14.         public Wezel rodzic;
  15.         public Wezel lewe;
  16.         public Wezel prawe;
  17.         public int numer;
  18.         public int wartosc;
  19.     }
  20.     class Drzewo : Wezel
  21.     {
  22.         public
  23.         Wezel root = null;
  24.         int length = 0;
  25.  
  26.         public void push(int liczba)
  27.         {
  28.             Wezel nowy = new Wezel();
  29.             nowy.wartosc = liczba;
  30.             nowy.numer = length + 1;
  31.             if (length == 0)
  32.             {
  33.                 root = nowy;
  34.             }
  35.             else
  36.             {
  37.                 int tmp = nowy.numer;
  38.                 List<int> kierunek = new List<int>();
  39.                 while (tmp > 1)
  40.                 {
  41.                     kierunek.Add(tmp % 2);
  42.                     tmp /= 2;
  43.                 }
  44.                 Wezel rodzic = root;
  45.                 for (int i = kierunek.Count() - 1; i >= 1; i--)
  46.                 {
  47.                     if (kierunek[i] == 0)
  48.                     {
  49.                         rodzic = rodzic.lewe;
  50.                     }
  51.                     else
  52.                     {
  53.                         rodzic = rodzic.prawe;
  54.                     }
  55.                 }
  56.                 if (kierunek[0] == 0)
  57.                 {
  58.                     rodzic.lewe = nowy;
  59.                 }
  60.                 else
  61.                 {
  62.                     rodzic.prawe = nowy;
  63.                 }
  64.                 nowy.rodzic = rodzic;
  65.             }
  66.             length++;
  67.         }
  68.  
  69.         public void pop()
  70.         {
  71.             int wynik;
  72.             if (length < 1)
  73.             {
  74.                 Console.WriteLine("Brak drzewa");
  75.             }
  76.             else
  77.             {
  78.                 Wezel nowy = new Wezel();
  79.                 if (length == 1)
  80.                 {
  81.                     wynik = root.wartosc;
  82.                     Console.WriteLine("Usunięto Drzewo: " + wynik);
  83.                     root = null;
  84.                 }
  85.                 else
  86.                 {
  87.                     int tmp = length;
  88.                     List<int> kierunek = new List<int>();
  89.                     while (tmp > 1)
  90.                     {
  91.                         kierunek.Add(tmp % 2);
  92.                         tmp /= 2;
  93.                     }
  94.                     Wezel rodzic = root;
  95.                     for (int i = kierunek.Count() - 1; i >= 1; i--)
  96.                     {
  97.                         if (kierunek[i] == 0)
  98.                         {
  99.                             rodzic = rodzic.lewe;
  100.                         }
  101.                         else
  102.                         {
  103.                             rodzic = rodzic.prawe;
  104.                         }
  105.                     }
  106.                     if (kierunek[0] == 0)
  107.                     {
  108.                         wynik = rodzic.lewe.wartosc;
  109.                         Console.WriteLine("Usunięto lewe dziecko: " + wynik);
  110.                         rodzic.lewe = null;
  111.                     }
  112.                     else
  113.                     {
  114.                         wynik = rodzic.prawe.wartosc;
  115.                         Console.WriteLine("Usunięto prawe dziecko: " + wynik);
  116.                         rodzic.prawe = null;
  117.                     }
  118.                 }
  119.                 length--;
  120.                 Console.WriteLine();
  121.             }
  122.         }
  123.  
  124.         public int Length()
  125.         {
  126.             return length;
  127.         }
  128.         public void show()
  129.         {
  130.             if (length < 1)
  131.             {
  132.                 root = null;
  133.                 Console.WriteLine("Brak drzewa");
  134.             }
  135.             else
  136.             {
  137.                 int tmp;
  138.                 List<int> kierunek = new List<int>();
  139.                 Wezel rodzic = root;
  140.                 Console.WriteLine("Korzeń " + rodzic.wartosc);
  141.                 for (int i = 2; i <= length; i++)
  142.                 {
  143.                     rodzic = root;
  144.                     tmp = i;
  145.                     while (tmp > 1)
  146.                     {
  147.                         kierunek.Add(tmp % 2);
  148.                         tmp /= 2;
  149.                     }
  150.                     for (int j = kierunek.Count() - 1; j > 0; j--)
  151.                     {
  152.                         if (kierunek[j] == 0)
  153.                         {
  154.                             rodzic = rodzic.lewe;
  155.                         }
  156.                         else
  157.                         {
  158.                             rodzic = rodzic.prawe;
  159.                         }
  160.                     }
  161.                     if (kierunek[0] == 0)
  162.                     {
  163.                         Console.WriteLine("Lewe dziecko " + rodzic.lewe.wartosc + " Rodzic " + rodzic.wartosc);
  164.                     }
  165.                     else
  166.                     {
  167.                         Console.WriteLine("Prawe dziecko " + rodzic.prawe.wartosc + " Rodzic " + rodzic.wartosc);
  168.                     }
  169.                     kierunek.Clear();
  170.                 }
  171.                 Console.WriteLine();
  172.             }
  173.         }
  174.         public void show2()
  175.         {
  176.             if (length < 1)
  177.             {
  178.                 root = null;
  179.                 Console.WriteLine("Brak drzewa");
  180.             }
  181.             else
  182.             {
  183.                 int tmp;
  184.                 List<int> kierunek = new List<int>();
  185.                 Wezel rodzic = root;
  186.                 Console.WriteLine("Korzeń " + rodzic.wartosc);
  187.                 for (int i = 2; i <= length; i++)
  188.                 {
  189.                     rodzic = root;
  190.                     tmp = i;
  191.                     while (tmp > 1)
  192.                     {
  193.                         kierunek.Add(tmp % 2);
  194.                         tmp /= 2;
  195.                     }
  196.                     for (int j = kierunek.Count() - 1; j > 0; j--)
  197.                     {
  198.                         if (kierunek[j] == 0)
  199.                         {
  200.                             rodzic = rodzic.lewe;
  201.                         }
  202.                         else
  203.                         {
  204.                             rodzic = rodzic.prawe;
  205.                         }
  206.                     }
  207.                     if (kierunek[0] == 0)
  208.                     {
  209.                         Console.WriteLine("Lewe dziecko " + rodzic.lewe.wartosc);
  210.                     }
  211.                     kierunek.Clear();
  212.                 }
  213.             }
  214.             if (length < 1)
  215.             {
  216.                 root = null;
  217.                 Console.WriteLine("Brak drzewa");
  218.             }
  219.             else
  220.             {
  221.                 int tmp;
  222.                 List<int> kierunek = new List<int>();
  223.                 Wezel rodzic = root;
  224.                 for (int i = 2; i <= length; i++)
  225.                 {
  226.                     rodzic = root;
  227.                     tmp = i;
  228.                     while (tmp > 1)
  229.                     {
  230.                         kierunek.Add(tmp % 2);
  231.                         tmp /= 2;
  232.                     }
  233.                     for (int j = kierunek.Count() - 1; j > 0; j--)
  234.                     {
  235.                         if (kierunek[j] == 0)
  236.                         {
  237.                             rodzic = rodzic.lewe;
  238.                         }
  239.                         else
  240.                         {
  241.                             rodzic = rodzic.prawe;
  242.                         }
  243.                     }
  244.                     if (kierunek[0] != 0)
  245.                     {
  246.                         Console.WriteLine("Prawe dziecko " + rodzic.prawe.wartosc);
  247.                     }
  248.                     kierunek.Clear();
  249.                 }
  250.                 Console.WriteLine();
  251.             }
  252.         }
  253.         public void preorder(Wezel root)
  254.         {
  255.             Console.Write(root.wartosc + " ");
  256.             if (root.lewe != null)
  257.             {
  258.                 preorder(root.lewe);
  259.             }
  260.             if (root.prawe != null)
  261.             {
  262.                 preorder(root.prawe);
  263.             }
  264.         }
  265.         public void inorder(Wezel root)
  266.         {
  267.             if (root.lewe != null)
  268.             {
  269.                 inorder(root.lewe);
  270.             }
  271.             Console.Write(root.wartosc + " ");
  272.             if (root.prawe != null)
  273.             {
  274.                 inorder(root.prawe);
  275.             }
  276.         }
  277.         public void postorder(Wezel root)
  278.         {
  279.             if (root.lewe != null)
  280.             {
  281.                 postorder(root.lewe);
  282.             }
  283.             if (root.prawe != null)
  284.             {
  285.                 postorder(root.prawe);
  286.             }
  287.             Console.Write(root.wartosc + " ");
  288.         }
  289.  
  290.  
  291.  
  292.         static void Main(string[] args)
  293.         {
  294.             Drzewo x = new Drzewo();
  295.             x.push(9);
  296.             x.push(13);
  297.             x.push(14);
  298.             x.push(15);
  299.             x.push(16);
  300.             x.push(17);
  301.             x.push(19);
  302.             x.show2();
  303.             x.preorder(x.root);
  304.             Console.WriteLine();
  305.             Console.WriteLine();
  306.             x.inorder(x.root);
  307.             Console.WriteLine();
  308.             Console.WriteLine();
  309.             x.postorder(x.root);
  310.             Console.WriteLine();
  311.             Console.ReadKey();
  312.         }
  313.     }
  314. }