Advertisement
mikymaione

Algo_20170628_2_2

Jul 1st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1.  
  2.         public static BST Algo_20170628_2_2_Miky(BST T, int a, int b)
  3.         {
  4.             BST last = null;
  5.             var S = new Stack<BST>();
  6.  
  7.             while (S.Count > 0 || T != null)
  8.             {
  9.                 if (T != null)
  10.                 {
  11.                     Console.WriteLine("Ispezionando " + T.key);
  12.                     S.Push(T);
  13.                 }
  14.  
  15.                 if (T != null && (T.key < a || (T.key >= a && T.key <= b)))
  16.                 {
  17.                     T = T.Sx;
  18.                 }
  19.                 else
  20.                 {
  21.                     if (T == null)
  22.                         T = S.Peek();
  23.  
  24.                     if (T.Dx != null && T.Dx != last && ((T.key > b) || (T.key >= a && T.key <= b)))
  25.                     {                        
  26.                         T = T.Dx;
  27.                     }
  28.                     else
  29.                     {                              
  30.                         S.Pop();
  31.                         last = T;
  32.  
  33.                         if (T.key >= a && T.key <= b)
  34.                         {
  35.                             T = Cancella(T);                          
  36.                            
  37.                             if (S.Count > 0)
  38.                             {
  39.                                 var P = S.Peek();
  40.  
  41.                                 if (P.dx == last)
  42.                                     P.dx = T;
  43.                                 else if (P.sx == last)
  44.                                     P.sx = T;                            
  45.                             }      
  46.  
  47.                             if (T != null)
  48.                                 last = T;
  49.                         }
  50.                            
  51.                         T = null;
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             return last;
  57.         }
  58.  
  59.         public static BST Algo_20170628_2_2(BST T, int a, int b)
  60.         {
  61.             if (T != null)
  62.             {
  63.                 Console.WriteLine("Ispezionando " + T.key);
  64.  
  65.                 if (T.key < a)
  66.                     T.sx = Algo_20170628_2_2(T.sx, a, b);
  67.                 else if (T.key > b)
  68.                     T.dx = Algo_20170628_2_2(T.dx, a, b);
  69.                 else
  70.                 {
  71.                     T.sx = Algo_20170628_2_2(T.sx, a, b);
  72.                     T.dx = Algo_20170628_2_2(T.dx, a, b);
  73.                     T = Cancella(T);
  74.                 }
  75.             }
  76.  
  77.             return T;
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement