Advertisement
pinionszki

WypisujPreOrder

Jan 22nd, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace PreORderv2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] t = { 6, 3, 1, 2, 4, 5, 7 };
  14.             //int[] t = { 6, 3, 1, 2, 4, 7, 5 };
  15.             Tree root = null;
  16.  
  17.             int i = 0;
  18.             bool badTree = false;
  19.  
  20.             root = constructTree(t, ref i, Int32.MaxValue);
  21.  
  22.             WypiszDrzewo(root);
  23.  
  24.             Console.ReadKey();
  25.         }
  26.  
  27.         class Tree
  28.         {
  29.             public int val;
  30.             public Tree left;
  31.             public Tree right;
  32.  
  33.             public Tree(int val)
  34.             {
  35.                 this.val = val;
  36.             }
  37.         }
  38.  
  39.         static void WypiszDrzewo(Tree root)
  40.         {
  41.             if (root == null)
  42.                 return;
  43.  
  44.             Queue<Tree> q = new Queue<Tree>();
  45.             q.Enqueue(root);
  46.  
  47.             while (true)
  48.             {
  49.                 int nodeCount = q.Count;
  50.                 if (nodeCount == 0)
  51.                     break;
  52.  
  53.                 while (nodeCount > 0)
  54.                 {
  55.                     Tree node = q.Dequeue();
  56.                     Console.Write(node.val + " ");
  57.  
  58.                     if (node.left != null)
  59.                         q.Enqueue(node.left);
  60.                     if (node.right != null)
  61.                         q.Enqueue(node.right);
  62.                     nodeCount--;
  63.                 }
  64.                 Console.WriteLine();
  65.             }
  66.         }
  67.         static bool IsValid(int[] pre)
  68.         {
  69.             Stack<int> s = new Stack<int>();
  70.             int? lowerBound = null;
  71.  
  72.             for (int i = 0; i < pre.Length; i++)
  73.             {
  74.                 if (lowerBound != null && pre[i] < lowerBound) return false;
  75.                 while (s.Count != 0 && s.Peek() < pre[i]) lowerBound = s.Pop();
  76.                 s.Push(pre[i]);
  77.             }
  78.  
  79.             return true;
  80.         }
  81.  
  82.         static Tree constructTree(int[] pre, ref int i, int high)
  83.         {
  84.             if (!IsValid(pre)) return null;
  85.             else
  86.             {
  87.                 if (i >= pre.Length || pre[i] > high) return null;
  88.  
  89.                 Tree root = new Tree(pre[i++]);
  90.                 root.left = constructTree(pre, ref i, root.val);
  91.                 root.right = constructTree(pre, ref i, high);
  92.                 return root;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98.  
  99. using System;
  100. using System.Collections.Generic;
  101. using System.Linq;
  102. using System.Text;
  103. using System.Threading.Tasks;
  104.  
  105. namespace PreOreder
  106. {
  107.     class Program
  108.     { //odtwórz drzewo na bazie przejscia go w PreOrder
  109.         static void Main(string[] args)
  110.         {
  111.             int[] tab = { 6, 3, 1, 2, 4, 5, 7 };
  112.             //int[] tab = { 6, 3, 1, 2, 4, 7, 5 };
  113.            
  114.             Drzewo d = Drzewo.BudujZPreOrder(tab);
  115.  
  116.             if (d != null)
  117.                 d.Wyswietl();
  118.             else
  119.                 Console.WriteLine("Drzewo było null");
  120.  
  121.             Console.ReadKey();
  122.         }
  123.     }
  124.    
  125.     class Drzewo
  126.     {
  127.         public Element głowa;
  128.  
  129.         public static Drzewo BudujZPreOrder(int[] tab)
  130.         {
  131.             Drzewo d = new Drzewo();
  132.             d.głowa = new Element(tab[0]);
  133.  
  134.             for (int i = 1; i < tab.Length; i++)
  135.             {
  136.                 Element tmp = d.głowa;
  137.  
  138.                 while(tmp != null)
  139.                 {
  140.                     if (tab[i] < tmp.wartosc)
  141.                     {
  142.                         if (tmp.prawy != null)
  143.                         {
  144.                             return null;
  145.                         }
  146.                         if (tmp.lewy != null)
  147.                         {
  148.                             tmp = tmp.lewy;
  149.                         }
  150.                         else
  151.                         {
  152.                             tmp.lewy = new Element(tab[i]);
  153.                             tmp = null;
  154.                         }
  155.                     }
  156.                     else
  157.                     {
  158.                         if (tmp.prawy != null)
  159.                         {
  160.                             tmp = tmp.prawy;
  161.                         }
  162.                         else
  163.                         {
  164.                             tmp.prawy = new Element(tab[i]);
  165.                             tmp = null;
  166.                         }
  167.                     }
  168.                 }
  169.             }
  170.             return d;
  171.         }
  172.  
  173.         public void Wyswietl()
  174.         {
  175.             if (głowa == null)
  176.             {
  177.                 Console.WriteLine("Drzewo bez glowy!");
  178.                 return;
  179.             }
  180.  
  181.             głowa.WyswietlPoddrzewo();
  182.         }
  183.  
  184.  
  185.     }
  186.     class Element
  187.     {
  188.         public Element(int w) => wartosc = w;
  189.         public int wartosc;
  190.         public Element lewy;
  191.         public Element prawy;
  192.  
  193.         public void WyswietlPoddrzewo()
  194.         {
  195.             Console.WriteLine(wartosc + " ");
  196.             if (lewy != null)
  197.             {
  198.                 lewy.WyswietlPoddrzewo();
  199.             }
  200.             if (prawy != null)
  201.             {
  202.                 prawy.WyswietlPoddrzewo();
  203.             }
  204.         }
  205.  
  206.         public override string ToString()
  207.         {
  208.             return wartosc.ToString();
  209.         }
  210.     }
  211. }
  212.  
  213.  
  214.  
  215. //metody in, post, pre
  216. using System;
  217. using System.Collections;
  218.  
  219. //Zadanie 2
  220. //Dla drzewa binarnego w implementacji dowiązaniowej napisz:
  221. //a) metodę wyszukującą czy istnieje element(węzeł) o zadanej wartości.
  222. //b) metodę przechodzenia pre-order nierekurencyjną(wsk.wykorzystaj stos)
  223. // klasa pomocnicza Węzeł
  224. class Węzeł
  225. {
  226.     public String wartość;
  227.     public Węzeł lewy;
  228.     public Węzeł prawy;
  229. }
  230.  
  231. // klasa Drzewo
  232. class Drzewo
  233. {
  234.     public Węzeł korzeń;
  235. }
  236.  
  237. class Program
  238. {
  239.     static Węzeł UtwórzWęzeł(String wartość)
  240.     {
  241.         Węzeł węzeł = new Węzeł();
  242.         węzeł.wartość = wartość;
  243.         return węzeł;
  244.     }
  245.  
  246.     static void InicjujWęzeł(Węzeł węzeł, String wartość)
  247.     {
  248.         węzeł.wartość = wartość;
  249.     }
  250.  
  251.     static void DodajLewy(Węzeł węzeł, Węzeł dziecko)
  252.     {
  253.         węzeł.lewy = dziecko;
  254.     }
  255.     static void DodajPrawy(Węzeł węzeł, Węzeł dziecko)
  256.     {
  257.         węzeł.prawy = dziecko;
  258.     }
  259.  
  260.     // pre-order
  261.     static void WypisujPre(Węzeł węzeł)
  262.     {
  263.         if (węzeł == null) return;
  264.         Console.Write(węzeł.wartość + " ");
  265.         WypisujPre((Węzeł)węzeł.lewy);
  266.         WypisujPre((Węzeł)węzeł.prawy);
  267.     }
  268.  
  269.     // post-order
  270.     static void WypisujPost(Węzeł węzeł)
  271.     {
  272.         if (węzeł == null) return;
  273.         WypisujPost((Węzeł)węzeł.lewy);
  274.         WypisujPost((Węzeł)węzeł.prawy);
  275.         Console.Write(węzeł.wartość + " ");
  276.     }
  277.  
  278.     // in-order
  279.     static void WypisujIn(Węzeł węzeł)
  280.     {
  281.         if (węzeł == null) return;
  282.         WypisujIn((Węzeł)węzeł.lewy);
  283.         Console.Write(węzeł.wartość + " ");
  284.         WypisujIn((Węzeł)węzeł.prawy);
  285.     }
  286.  
  287.     static int Wysokość(Węzeł węzeł)
  288.     {
  289.         int wysokość = 0;
  290.         if (węzeł == null) return 0;
  291.         if (węzeł.lewy != null)
  292.             wysokość = Math.Max(wysokość, Wysokość(węzeł.lewy) + 1);
  293.         if (węzeł.prawy != null)
  294.             wysokość = Math.Max(wysokość, Wysokość(węzeł.prawy) + 1);
  295.         return wysokość;
  296.     }// Zwraca wynik
  297.  
  298.     // metodę wyszukującą czy istnieje element(węzeł) o zadanej wartości.
  299.  
  300.  
  301.     static Węzeł CzyIstniejeWęzeł(Drzewo d, string s)
  302.     {
  303.         return CzyIstniejeWęzeł(d.korzeń, s);
  304.     }
  305.     static Węzeł CzyIstniejeWęzeł(Węzeł w, string s)
  306.     {
  307.         if (w == null) return null;
  308.  
  309.         if (w.wartość == s) return w; // znalazłem
  310.         if (w.lewy != null)
  311.         {
  312.             Węzeł t = CzyIstniejeWęzeł(w.lewy, s);
  313.             if (t != null) return t; // znalazłem
  314.         }
  315.         return CzyIstniejeWęzeł(w.prawy, s);
  316.     }
  317.  
  318.     //metodę przechodzenia pre-order nierekurencyjną
  319.     static void WypisujPreNierekurencyjnie(Węzeł węzeł)
  320.     {
  321.         if (węzeł == null) return;
  322.         Stack s = new Stack();
  323.         s.Push(węzeł);
  324.         while(s.Count>0)
  325.         {
  326.             Węzeł tmp = (Węzeł)s.Pop();
  327.             Console.Write(tmp.wartość + " ");
  328.             if(tmp.prawy!=null)
  329.                 s.Push(tmp.prawy);
  330.             if(tmp.lewy!=null)
  331.                 s.Push(tmp.lewy);
  332.         }
  333.     }
  334.     static void Main()
  335.     {
  336.         Drzewo drzewo = new Drzewo();
  337.  
  338.         drzewo.korzeń = UtwórzWęzeł("F");
  339.         Węzeł wB = UtwórzWęzeł("B");
  340.         Węzeł wA = UtwórzWęzeł("A");
  341.         Węzeł wC = UtwórzWęzeł("C");
  342.         Węzeł wD = UtwórzWęzeł("D");
  343.         Węzeł wE = UtwórzWęzeł("E");
  344.         Węzeł wG = UtwórzWęzeł("G");
  345.         Węzeł wH = UtwórzWęzeł("H");
  346.         Węzeł wI = UtwórzWęzeł("I");
  347.         Węzeł wX = UtwórzWęzeł("X");
  348.  
  349.         DodajLewy(wD, wC);
  350.         DodajPrawy(wD, wE);
  351.         DodajPrawy(wA, wX);
  352.         DodajLewy(wB, wA);
  353.         DodajPrawy(wB, wD);
  354.  
  355.         DodajLewy(wI, wH);
  356.         DodajPrawy(wG, wI);
  357.  
  358.         DodajLewy(drzewo.korzeń, wB);
  359.         DodajPrawy(drzewo.korzeń, wG);
  360.  
  361.         WypisujPre(drzewo.korzeń);
  362.         Console.WriteLine();
  363.         WypisujPost(drzewo.korzeń);
  364.         Console.WriteLine();
  365.         WypisujIn(drzewo.korzeń);
  366.         Console.WriteLine();
  367.         Console.WriteLine("wysokość = " + Wysokość(drzewo.korzeń));
  368.  
  369.         Węzeł t = CzyIstniejeWęzeł(drzewo, "X");
  370.         Console.WriteLine(t != null);
  371.         t = CzyIstniejeWęzeł(drzewo, "A");
  372.         Console.WriteLine(t != null);
  373.         t = CzyIstniejeWęzeł(drzewo, "G");
  374.         Console.WriteLine(t != null);
  375.         t = CzyIstniejeWęzeł(drzewo, "Z");
  376.         Console.WriteLine(t != null);
  377.  
  378.         WypisujPreNierekurencyjnie(drzewo.korzeń);
  379.  
  380.         Console.ReadKey();
  381.     }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement