Advertisement
mikymaione

Esercizi_20170224

Jun 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. /*
  2. Copyright (C) 2015 [MAIONE MIKY]
  3. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  4. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  5. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
  6. */
  7. using System.Collections.Generic;
  8.  
  9. namespace Alberi
  10. {
  11.     class Esercizi_20170224 : BST
  12.     {
  13.  
  14.         public Esercizi_20170224(int k) : base(k) { }
  15.  
  16.  
  17.         private static int funzione(BST T, BST P, int x, int r_sx, int r_dx)
  18.         {
  19.             var ret = r_sx + r_dx + 1;
  20.  
  21.             System.Console.WriteLine($"n: {T.key} | ret: {ret}");
  22.  
  23.             if (T.key % 2 == 0 && ret < x && P != null)
  24.             {
  25.                 if (T == P.dx)
  26.                     P.dx = Cancella(T);
  27.                 else
  28.                     P.sx = Cancella(T);
  29.             }
  30.  
  31.             return ret;
  32.         }
  33.  
  34.         public static int Algo(BST T, BST P, int x)
  35.         {
  36.             var ret = 0;
  37.  
  38.             if (T != null)
  39.             {
  40.                 var r_sx = Algo(T.sx, T, x);
  41.                 var r_dx = Algo(T.dx, T, x);
  42.  
  43.                 ret = funzione(T, P, x, r_sx, r_dx);
  44.             }
  45.  
  46.             return ret;
  47.         }
  48.  
  49.         public static int Algo_Miky(BST T, BST P, int x)
  50.         {
  51.             var RS = new Stack<int>();
  52.             var ret = 0;
  53.  
  54.             BST last = null;
  55.             var S = new Stack<BST>();
  56.  
  57.             while (S.Count > 0 || T != null)
  58.                 if (T != null)
  59.                 {
  60.                     S.Push(T);
  61.                     T = T.Sx;
  62.                 }
  63.                 else
  64.                 {
  65.                     T = S.Peek();
  66.  
  67.                     if (T.Dx != null && T.Dx != last)
  68.                     {
  69.                         if (T.Sx != null)
  70.                             RS.Push(ret);
  71.  
  72.                         T = T.Dx;
  73.                     }
  74.                     else
  75.                     {
  76.                         var r_sx = 0;
  77.                         var r_dx = 0;
  78.  
  79.                         if (T.Sx != null && T.Dx == null)
  80.                             r_sx = ret;
  81.                         else if (T.Sx != null && T.Dx != null)
  82.                             r_sx = RS.Pop();
  83.  
  84.                         if (T.Dx != null)
  85.                             r_dx = ret;
  86.  
  87.                         S.Pop();
  88.                         P = (S.Count > 0 ? S.Peek() : null);
  89.  
  90.                         ret = funzione(T, P, x, r_sx, r_dx);
  91.  
  92.                         last = T;
  93.                         T = null;
  94.                     }
  95.                 }
  96.  
  97.             return ret;
  98.         }
  99.  
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement