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