Advertisement
mikymaione

CountOddRic BENE style

Jun 18th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. //BENE STYLE
  2. public static int CountOddRic(BST T)
  3. {
  4.     BST last = null;
  5.     var S = new Stack<BST>(); // stack dell'albero
  6.     var RR = new Stack<int>(); // memorizzo il ret calcolato in preorder
  7.     var RS = new Stack<int>(); // memorizzo il r_sx calcolato in inorder
  8.     var ret = 0;
  9.  
  10.     while (S.Count > 0 || T != null)
  11.         if (T != null)
  12.         {
  13.             // preorder
  14.             ret = (T.Key % 2 == 1 ? T.Key : 0);        
  15.             RR.Push(ret);
  16.            
  17.             S.Push(T);
  18.             T = T.Sx;
  19.         }
  20.         else
  21.         {
  22.             T = S.Peek();
  23.  
  24.             if (T.Dx != null && T.Dx != last)
  25.             {
  26.                 // inorder
  27.                 if (T.Sx != null)
  28.                     RS.Push(ret);
  29.  
  30.                 T = T.Dx;
  31.             }
  32.             else
  33.             {
  34.                 // postorder
  35.                 var r_sx = 0;
  36.                 var r_dx = 0;
  37.  
  38.                 if (T.Sx != null && T.Dx == null)
  39.                     r_sx = ret;
  40.                 else if (T.Sx != null && T.Dx != null)
  41.                     r_sx = RS.Pop();
  42.  
  43.                 if (T.Dx != null)
  44.                     r_dx = ret;
  45.  
  46.                 ret = RR.Pop();
  47.                 ret = ret + r_sx + r_dx;               
  48.  
  49.                 last = T;
  50.                 T = null;
  51.                 S.Pop();
  52.             }
  53.         }
  54.  
  55.     return ret;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement