Facebook
From Pablo, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.  
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         class Węzeł
  22.         {
  23.             public int wartość;
  24.             public Węzeł lewy;
  25.             public Węzeł prawy;
  26.         }
  27.  
  28.         class Drzewo
  29.         {
  30.  
  31.             public Węzeł root;
  32.  
  33.             public Drzewo()
  34.             {
  35.                 root = null;
  36.             }
  37.  
  38.             public Węzeł Rodzic()
  39.             {
  40.                 return root;
  41.             }
  42.  
  43.  
  44.             public void Push(int id)
  45.             {
  46.                 Węzeł nowy = new Węzeł();
  47.                 nowy.wartość = id;
  48.                 if (root == null)
  49.                     root = nowy;
  50.                 else
  51.                 {
  52.                     Węzeł tmp = root;
  53.                     Węzeł rodzic;
  54.                     while (true)
  55.                     {
  56.                         rodzic = tmp;
  57.                         if (id < tmp.wartość)
  58.                         {
  59.                             tmp = tmp.lewy;
  60.                             if (tmp == null)
  61.                             {
  62.                                 rodzic.lewy = nowy;
  63.                                 return;
  64.                             }
  65.                         }
  66.                         else
  67.                         {
  68.                             tmp = tmp.prawy;
  69.                             if (tmp == null)
  70.                             {
  71.                                 rodzic.prawy = nowy;
  72.                                 return;
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.  
  79.  
  80.             String text = "";
  81.  
  82.             public String Preorder(Węzeł Root)
  83.             {
  84.                 if (Root != null)
  85.                 {
  86.                     text += Root.wartość + " ";
  87.                     Preorder(Root.lewy);
  88.                     Preorder(Root.prawy);
  89.                 }
  90.                 return text;
  91.             }
  92.             public String Inorder(Węzeł Root)
  93.             {
  94.                 if (Root != null)
  95.                 {
  96.                     Inorder(Root.lewy);
  97.                     text += Root.wartość + " ";
  98.                     Inorder(Root.prawy);
  99.                 }
  100.                 return text;
  101.             }
  102.             public String Postorder(Węzeł Root)
  103.             {
  104.                 if (Root != null)
  105.                 {
  106.                     Postorder(Root.lewy);
  107.                     Postorder(Root.prawy);
  108.                     text += Root.wartość + " ";
  109.                 }
  110.                 return text;
  111.             }
  112.  
  113.             public void generatedata(Drzewo d)
  114.             {
  115.                 d.Push(10);
  116.                 d.Push(5);
  117.                 d.Push(1);
  118.                 d.Push(0);
  119.                 d.Push(2);
  120.                 d.Push(7);
  121.                 d.Push(8);
  122.                 d.Push(15);
  123.                 d.Push(12);
  124.                 d.Push(11);
  125.                 d.Push(20);
  126.             }
  127.  
  128.         }
  129.  
  130.  
  131.         private void button1_Click(object sender, EventArgs e)
  132.         {
  133.             Drzewo d = new Drzewo();
  134.             d.generatedata(d);
  135.             textBox1.Text += "INORDER -> ";
  136.             String text = d.Inorder(d.Rodzic());
  137.             textBox1.Text += text;
  138.             textBox1.Text += "rn";
  139.         }
  140.  
  141.         private void button2_Click(object sender, EventArgs e)
  142.         {
  143.             Drzewo d = new Drzewo();
  144.             d.generatedata(d);
  145.             textBox1.Text += "PREORDER -> ";
  146.             String text = d.Preorder(d.Rodzic());
  147.             textBox1.Text += text;
  148.             textBox1.Text += "rn";
  149.         }
  150.  
  151.         private void button3_Click(object sender, EventArgs e)
  152.         {
  153.             Drzewo d = new Drzewo();
  154.             d.generatedata(d);
  155.             textBox1.Text += "POSTORDER -> ";
  156.             String text = d.Postorder(d.Rodzic());
  157.             textBox1.Text += text;
  158.             textBox1.Text += "rn";
  159.         }
  160.  
  161.     }
  162. }
  163.