Danielos168

BFS PreOrder

Jan 22nd, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. static class Zadanie3
  2.         {
  3.             class Node
  4.             {
  5.                 public int val;
  6.                 public Node r;
  7.                 public Node l;
  8.             }
  9.  
  10.             private static int[] tab = new int[] {2,3,7,1,5,6,4};
  11.             private static int[] tab2 = new int[] {2,3,7,1,5,6,4};
  12.  
  13.             public static void OdtworzDrzewo()
  14.             {
  15.                 if (CzyPoprawne(tab))
  16.                 {
  17.                     int i = tab.Length - 1;
  18.                     Node root = TreeCreator(tab, ref i, Int32.MinValue);
  19.                 }
  20.                 else
  21.                 {
  22.                     Console.WriteLine("Preorder niepoprawny");
  23.                 }
  24.             }
  25.  
  26.             // 1 3 2 5 7 6 4
  27.             static Node TreeCreator(int[] tab, ref int i, int bound)
  28.             {
  29.                 if (i < 0|| tab[i] < bound)
  30.                     return null;
  31.  
  32.                 Node root = new Node();
  33.                 root.val = tab[i--];
  34.                
  35.                 root.r = TreeCreator(tab, ref i, root.val);
  36.                 root.l = TreeCreator(tab, ref i, bound);
  37.  
  38.                 return root;
  39.             }
  40.  
  41.             static bool CzyPoprawne(int[] tab)
  42.             {
  43.                 if (tab.Length > 1)
  44.                 {
  45.                     int root = tab[tab.Length - 1];
  46.                     int leftSub = 0;
  47.                     int i = 0;
  48.                     for (i = tab.Length - 2; i >= 0; i--)
  49.                     {
  50.                         if (tab[i] < root)
  51.                         {
  52.                             leftSub = i;
  53.                             break;
  54.                         }
  55.                     }
  56.  
  57.                     for (int j = 0; j < leftSub; j++)
  58.                     {
  59.                         if (tab[j] > root)
  60.                             return false;
  61.                     }
  62.                 }
  63.  
  64.                 return true;
  65.             }
  66.         }
Add Comment
Please, Sign In to add comment