Advertisement
Meetes

Profundidade e largura recursivas

Apr 1st, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1.  
  2.         public Stack<Node<T>> PRO (Node<T> node, System.Predicate<Node<T>> match)
  3.         {
  4.             var fila = new Stack<Node<T>> ();
  5.  
  6.             Node<T> recursivo (Node<T> ESTADO)
  7.             {
  8.                 if (match (node)) return node;
  9.                 else
  10.                 {
  11.                     //REMOVER_DA_PILHA (esTADO);
  12.                     fila.Pop ();
  13.                     foreach (Node<T> child in node.Children)
  14.                     {
  15.                         fila.Push (child);
  16.                     }
  17.                 }
  18.                 while (fila.Count > 0)
  19.                 {
  20. #if DEBUG
  21.                     System.Diagnostics.Debug.WriteLine (node.ToString ());
  22. #endif
  23.                     if (match (fila.First ()))
  24.                     {
  25.                         return fila.First ();
  26.                     }
  27.                     fila.Pop ();
  28.                 }
  29.                 return null;
  30.             }
  31.             recursivo (node);
  32.             return fila;
  33.         }
  34.  
  35.         public Node<T> LARGURA (Node<T> node, System.Predicate<Node<T>> match)
  36.         {
  37.             var fila = new Stack<Node<T>> ();
  38.             var current = node;
  39.  
  40.             do
  41.             {
  42. #if DEBUG
  43.                 System.Diagnostics.Debug.WriteLine (current.ToString ());
  44. #endif
  45.  
  46.                 if (match (current)) return current;
  47.  
  48.                 else
  49.                 {
  50.                     foreach (Node<T> child in current.Children)
  51.                         fila.Push (child);
  52.                 }
  53.  
  54.                 if (fila.Count == 0) return default (Node<T>);
  55.                 current = fila.Pop ();
  56.             } while (true);
  57.         }
  58.  
  59.         public class ItsAlmostAStack<T>
  60.         {
  61.             private List<T> items = new List<T> ();
  62.  
  63.             public void Push (T item)
  64.             {
  65.                 items.Add (item);
  66.             }
  67.             public T Pop ()
  68.             {
  69.                 if (items.Count > 0)
  70.                 {
  71.                     T temp = items[items.Count - 1];
  72.                     items.RemoveAt (items.Count - 1);
  73.                     return temp;
  74.                 }
  75.                 else
  76.                     return default (T);
  77.             }
  78.             public void Remove (int itemAtPosition)
  79.             {
  80.                 items.RemoveAt (itemAtPosition);
  81.             }
  82.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement