Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Zadanie3
- {
- class Node
- {
- public int val;
- public Node r;
- public Node l;
- }
- private static int[] tab = new int[] {2,3,7,1,5,6,4};
- private static int[] tab2 = new int[] {2,3,7,1,5,6,4};
- public static void OdtworzDrzewo()
- {
- if (CzyPoprawne(tab))
- {
- int i = tab.Length - 1;
- Node root = TreeCreator(tab, ref i, Int32.MinValue);
- }
- else
- {
- Console.WriteLine("Preorder niepoprawny");
- }
- }
- // 1 3 2 5 7 6 4
- static Node TreeCreator(int[] tab, ref int i, int bound)
- {
- if (i < 0|| tab[i] < bound)
- return null;
- Node root = new Node();
- root.val = tab[i--];
- root.r = TreeCreator(tab, ref i, root.val);
- root.l = TreeCreator(tab, ref i, bound);
- return root;
- }
- static bool CzyPoprawne(int[] tab)
- {
- if (tab.Length > 1)
- {
- int root = tab[tab.Length - 1];
- int leftSub = 0;
- int i = 0;
- for (i = tab.Length - 2; i >= 0; i--)
- {
- if (tab[i] < root)
- {
- leftSub = i;
- break;
- }
- }
- for (int j = 0; j < leftSub; j++)
- {
- if (tab[j] > root)
- return false;
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment