Facebook
From TajnePrzezPoufne, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp4
  8. {
  9.  
  10.  
  11.  
  12.     class Wezel
  13.     {
  14.         public int wartosc;
  15.         public Wezel lewy;
  16.         public Wezel prawy;
  17.         public void pokaz()
  18.         {
  19.             Console.Write("[");
  20.             Console.Write(wartosc);
  21.             Console.Write("]");
  22.         }
  23.     }
  24.     class Tree
  25.     {
  26.         public Wezel korzen;
  27.         public Tree()
  28.         {
  29.             korzen = null;
  30.         }
  31.         public Wezel ZwrocKorzen()
  32.         {
  33.             return korzen;
  34.         }
  35.         public void Wstaw(int id)
  36.         {
  37.             Wezel newWezel = new Wezel();
  38.             newWezel.wartosc = id;
  39.             if (korzen == null)
  40.                 korzen = newWezel;
  41.             else
  42.             {
  43.                 Wezel obecny = korzen;
  44.                 Wezel rodzic;
  45.                 while (true)
  46.                 {
  47.                     rodzic = obecny;
  48.                     if (id < obecny.wartosc)
  49.                     {
  50.                         obecny = obecny.lewy;
  51.                         if (obecny == null)
  52.                         {
  53.                             rodzic.lewy = newWezel;
  54.                             return;
  55.                         }
  56.                     }
  57.                     else
  58.                     {
  59.                         obecny = obecny.prawy;
  60.                         if (obecny == null)
  61.                         {
  62.                             rodzic.prawy = newWezel;
  63.                             return;
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         public void Preorder(Wezel Root)
  70.         {
  71.             if (Root != null)
  72.             {
  73.                 Console.Write(Root.wartosc + " ");
  74.                 Preorder(Root.lewy);
  75.                 Preorder(Root.prawy);
  76.             }
  77.         }
  78.         public void Inorder(Wezel Root)
  79.         {
  80.             if (Root != null)
  81.             {
  82.                 Inorder(Root.lewy);
  83.                 Console.Write(Root.wartosc + " ");
  84.                 Inorder(Root.prawy);
  85.             }
  86.         }
  87.         public void Postorder(Wezel Root)
  88.         {
  89.             if (Root != null)
  90.             {
  91.                 Postorder(Root.lewy);
  92.                 Postorder(Root.prawy);
  93.                 Console.Write(Root.wartosc + " ");
  94.             }
  95.         }
  96.     }
  97.     class Program
  98.     {
  99.         static void Main(string[] args)
  100.         {
  101.             Tree Test = new Tree();
  102.             Test.Wstaw(14);
  103.             Test.Wstaw(33);
  104.             Test.Wstaw(67);
  105.             Test.Wstaw(43);
  106.             Test.Wstaw(32);
  107.             Test.Wstaw(88);
  108.             Test.Wstaw(43);
  109.             Test.Wstaw(32);
  110.             Test.Wstaw(12);
  111.             Test.Wstaw(32);
  112.             Test.Wstaw(54);
  113.             Test.Wstaw(32);
  114.             Test.Wstaw(65);
  115.             Test.Wstaw(13);
  116.             Console.WriteLine("Inorder : ");
  117.             Test.Inorder(Test.ZwrocKorzen());
  118.             Console.WriteLine(" ");
  119.             Console.WriteLine();
  120.             Console.WriteLine("Preorder : ");
  121.             Test.Preorder(Test.ZwrocKorzen());
  122.             Console.WriteLine(" ");
  123.             Console.WriteLine();
  124.             Console.WriteLine("Postorder : ");
  125.             Test.Postorder(Test.ZwrocKorzen());
  126.             Console.WriteLine(" ");
  127.             Console.ReadLine();
  128.         }
  129.     }
  130. }