Advertisement
mikymaione

navigazione iterativa

Jun 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1.  
  2. public static void PreOrder_Ricorsivo(BST T, Action<BST> Action_PreOrder)
  3. {
  4.     if (T != null)
  5.     {
  6.         Action_PreOrder(T);
  7.         PreOrder_Ricorsivo(T.Sx, Action_PreOrder);
  8.         PreOrder_Ricorsivo(T.Dx, Action_PreOrder);
  9.     }
  10. }
  11.  
  12. public static void InOrder_Ricorsivo(BST T, Action<BST> Action_InOrder)
  13. {
  14.     if (T != null)
  15.     {
  16.         InOrder_Ricorsivo(T.Sx, Action_InOrder);
  17.         Action_InOrder(T);
  18.         InOrder_Ricorsivo(T.Dx, Action_InOrder);
  19.     }
  20. }
  21.  
  22. public static void PostOrder_Ricorsivo(BST T, Action<BST> Action_PreOrder, Action<BST> Action_InOrder, Action<BST> Action_PostOrder)
  23. {
  24.     if (T != null)
  25.     {
  26.         Action_PreOrder(T);
  27.         PostOrder_Ricorsivo(T.Sx, Action_PreOrder, Action_InOrder, Action_PostOrder);
  28.         Action_InOrder(T);
  29.         PostOrder_Ricorsivo(T.Dx, Action_PreOrder, Action_InOrder, Action_PostOrder);
  30.         Action_PostOrder(T);
  31.     }
  32. }
  33.  
  34. public static void PreOrder_Iterativo(BST T, Action<BST> Action_PreOrder)
  35. {
  36.     var S = new Stack<BST>();
  37.  
  38.     while (S.Count > 0 || T != null)
  39.         if (T != null)
  40.         {
  41.             Action_PreOrder(T);
  42.             S.Push(T);
  43.             T = T.Sx;
  44.         }
  45.         else
  46.         {
  47.             T = S.Peek();
  48.             T = T.Dx;
  49.             S.Pop();
  50.         }
  51. }
  52.  
  53. public static void PostOrder_Iterativo(BST T, Action<BST> Action_PreOrder, Action<BST> Action_InOrder, Action<BST> Action_PostOrder)
  54. {
  55.     BST last = null;
  56.     var S = new Stack<BST>();
  57.  
  58.     while (S.Count > 0 || T != null)
  59.         if (T != null)
  60.         {
  61.             Action_PreOrder(T);
  62.  
  63.             S.Push(T);
  64.             T = T.Sx;
  65.         }
  66.         else
  67.         {
  68.             T = S.Peek();
  69.  
  70.             if (T.Dx != null && T.Dx != last)
  71.             {
  72.                 Action_InOrder(T);
  73.  
  74.                 T = T.Dx;
  75.             }
  76.             else
  77.             {
  78.                 Action_PostOrder(T);
  79.  
  80.                 last = T;
  81.                 T = null;
  82.                 S.Pop();
  83.             }
  84.         }
  85. }
  86.  
  87. public static void InOrder_Iterativo(BST T, Action<BST> Action_InOrder)
  88. {
  89.     var S = new Stack<BST>();
  90.  
  91.     while (S.Count > 0 || T != null)
  92.         if (T != null)
  93.         {
  94.             S.Push(T);
  95.             T = T.Sx;
  96.         }
  97.         else
  98.         {
  99.             T = S.Pop();
  100.             Action_InOrder(T);
  101.             T = T.Dx;
  102.         }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement