Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PreORderv2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] t = { 6, 3, 1, 2, 4, 5, 7 };
- //int[] t = { 6, 3, 1, 2, 4, 7, 5 };
- Tree root = null;
- int i = 0;
- bool badTree = false;
- root = constructTree(t, ref i, Int32.MaxValue);
- WypiszDrzewo(root);
- Console.ReadKey();
- }
- class Tree
- {
- public int val;
- public Tree left;
- public Tree right;
- public Tree(int val)
- {
- this.val = val;
- }
- }
- static void WypiszDrzewo(Tree root)
- {
- if (root == null)
- return;
- Queue<Tree> q = new Queue<Tree>();
- q.Enqueue(root);
- while (true)
- {
- int nodeCount = q.Count;
- if (nodeCount == 0)
- break;
- while (nodeCount > 0)
- {
- Tree node = q.Dequeue();
- Console.Write(node.val + " ");
- if (node.left != null)
- q.Enqueue(node.left);
- if (node.right != null)
- q.Enqueue(node.right);
- nodeCount--;
- }
- Console.WriteLine();
- }
- }
- static bool IsValid(int[] pre)
- {
- Stack<int> s = new Stack<int>();
- int? lowerBound = null;
- for (int i = 0; i < pre.Length; i++)
- {
- if (lowerBound != null && pre[i] < lowerBound) return false;
- while (s.Count != 0 && s.Peek() < pre[i]) lowerBound = s.Pop();
- s.Push(pre[i]);
- }
- return true;
- }
- static Tree constructTree(int[] pre, ref int i, int high)
- {
- if (!IsValid(pre)) return null;
- else
- {
- if (i >= pre.Length || pre[i] > high) return null;
- Tree root = new Tree(pre[i++]);
- root.left = constructTree(pre, ref i, root.val);
- root.right = constructTree(pre, ref i, high);
- return root;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PreOreder
- {
- class Program
- { //odtwórz drzewo na bazie przejscia go w PreOrder
- static void Main(string[] args)
- {
- int[] tab = { 6, 3, 1, 2, 4, 5, 7 };
- //int[] tab = { 6, 3, 1, 2, 4, 7, 5 };
- Drzewo d = Drzewo.BudujZPreOrder(tab);
- if (d != null)
- d.Wyswietl();
- else
- Console.WriteLine("Drzewo było null");
- Console.ReadKey();
- }
- }
- class Drzewo
- {
- public Element głowa;
- public static Drzewo BudujZPreOrder(int[] tab)
- {
- Drzewo d = new Drzewo();
- d.głowa = new Element(tab[0]);
- for (int i = 1; i < tab.Length; i++)
- {
- Element tmp = d.głowa;
- while(tmp != null)
- {
- if (tab[i] < tmp.wartosc)
- {
- if (tmp.prawy != null)
- {
- return null;
- }
- if (tmp.lewy != null)
- {
- tmp = tmp.lewy;
- }
- else
- {
- tmp.lewy = new Element(tab[i]);
- tmp = null;
- }
- }
- else
- {
- if (tmp.prawy != null)
- {
- tmp = tmp.prawy;
- }
- else
- {
- tmp.prawy = new Element(tab[i]);
- tmp = null;
- }
- }
- }
- }
- return d;
- }
- public void Wyswietl()
- {
- if (głowa == null)
- {
- Console.WriteLine("Drzewo bez glowy!");
- return;
- }
- głowa.WyswietlPoddrzewo();
- }
- }
- class Element
- {
- public Element(int w) => wartosc = w;
- public int wartosc;
- public Element lewy;
- public Element prawy;
- public void WyswietlPoddrzewo()
- {
- Console.WriteLine(wartosc + " ");
- if (lewy != null)
- {
- lewy.WyswietlPoddrzewo();
- }
- if (prawy != null)
- {
- prawy.WyswietlPoddrzewo();
- }
- }
- public override string ToString()
- {
- return wartosc.ToString();
- }
- }
- }
- //metody in, post, pre
- using System;
- using System.Collections;
- //Zadanie 2
- //Dla drzewa binarnego w implementacji dowiązaniowej napisz:
- //a) metodę wyszukującą czy istnieje element(węzeł) o zadanej wartości.
- //b) metodę przechodzenia pre-order nierekurencyjną(wsk.wykorzystaj stos)
- // klasa pomocnicza Węzeł
- class Węzeł
- {
- public String wartość;
- public Węzeł lewy;
- public Węzeł prawy;
- }
- // klasa Drzewo
- class Drzewo
- {
- public Węzeł korzeń;
- }
- class Program
- {
- static Węzeł UtwórzWęzeł(String wartość)
- {
- Węzeł węzeł = new Węzeł();
- węzeł.wartość = wartość;
- return węzeł;
- }
- static void InicjujWęzeł(Węzeł węzeł, String wartość)
- {
- węzeł.wartość = wartość;
- }
- static void DodajLewy(Węzeł węzeł, Węzeł dziecko)
- {
- węzeł.lewy = dziecko;
- }
- static void DodajPrawy(Węzeł węzeł, Węzeł dziecko)
- {
- węzeł.prawy = dziecko;
- }
- // pre-order
- static void WypisujPre(Węzeł węzeł)
- {
- if (węzeł == null) return;
- Console.Write(węzeł.wartość + " ");
- WypisujPre((Węzeł)węzeł.lewy);
- WypisujPre((Węzeł)węzeł.prawy);
- }
- // post-order
- static void WypisujPost(Węzeł węzeł)
- {
- if (węzeł == null) return;
- WypisujPost((Węzeł)węzeł.lewy);
- WypisujPost((Węzeł)węzeł.prawy);
- Console.Write(węzeł.wartość + " ");
- }
- // in-order
- static void WypisujIn(Węzeł węzeł)
- {
- if (węzeł == null) return;
- WypisujIn((Węzeł)węzeł.lewy);
- Console.Write(węzeł.wartość + " ");
- WypisujIn((Węzeł)węzeł.prawy);
- }
- static int Wysokość(Węzeł węzeł)
- {
- int wysokość = 0;
- if (węzeł == null) return 0;
- if (węzeł.lewy != null)
- wysokość = Math.Max(wysokość, Wysokość(węzeł.lewy) + 1);
- if (węzeł.prawy != null)
- wysokość = Math.Max(wysokość, Wysokość(węzeł.prawy) + 1);
- return wysokość;
- }// Zwraca wynik
- // metodę wyszukującą czy istnieje element(węzeł) o zadanej wartości.
- static Węzeł CzyIstniejeWęzeł(Drzewo d, string s)
- {
- return CzyIstniejeWęzeł(d.korzeń, s);
- }
- static Węzeł CzyIstniejeWęzeł(Węzeł w, string s)
- {
- if (w == null) return null;
- if (w.wartość == s) return w; // znalazłem
- if (w.lewy != null)
- {
- Węzeł t = CzyIstniejeWęzeł(w.lewy, s);
- if (t != null) return t; // znalazłem
- }
- return CzyIstniejeWęzeł(w.prawy, s);
- }
- //metodę przechodzenia pre-order nierekurencyjną
- static void WypisujPreNierekurencyjnie(Węzeł węzeł)
- {
- if (węzeł == null) return;
- Stack s = new Stack();
- s.Push(węzeł);
- while(s.Count>0)
- {
- Węzeł tmp = (Węzeł)s.Pop();
- Console.Write(tmp.wartość + " ");
- if(tmp.prawy!=null)
- s.Push(tmp.prawy);
- if(tmp.lewy!=null)
- s.Push(tmp.lewy);
- }
- }
- static void Main()
- {
- Drzewo drzewo = new Drzewo();
- drzewo.korzeń = UtwórzWęzeł("F");
- Węzeł wB = UtwórzWęzeł("B");
- Węzeł wA = UtwórzWęzeł("A");
- Węzeł wC = UtwórzWęzeł("C");
- Węzeł wD = UtwórzWęzeł("D");
- Węzeł wE = UtwórzWęzeł("E");
- Węzeł wG = UtwórzWęzeł("G");
- Węzeł wH = UtwórzWęzeł("H");
- Węzeł wI = UtwórzWęzeł("I");
- Węzeł wX = UtwórzWęzeł("X");
- DodajLewy(wD, wC);
- DodajPrawy(wD, wE);
- DodajPrawy(wA, wX);
- DodajLewy(wB, wA);
- DodajPrawy(wB, wD);
- DodajLewy(wI, wH);
- DodajPrawy(wG, wI);
- DodajLewy(drzewo.korzeń, wB);
- DodajPrawy(drzewo.korzeń, wG);
- WypisujPre(drzewo.korzeń);
- Console.WriteLine();
- WypisujPost(drzewo.korzeń);
- Console.WriteLine();
- WypisujIn(drzewo.korzeń);
- Console.WriteLine();
- Console.WriteLine("wysokość = " + Wysokość(drzewo.korzeń));
- Węzeł t = CzyIstniejeWęzeł(drzewo, "X");
- Console.WriteLine(t != null);
- t = CzyIstniejeWęzeł(drzewo, "A");
- Console.WriteLine(t != null);
- t = CzyIstniejeWęzeł(drzewo, "G");
- Console.WriteLine(t != null);
- t = CzyIstniejeWęzeł(drzewo, "Z");
- Console.WriteLine(t != null);
- WypisujPreNierekurencyjnie(drzewo.korzeń);
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement