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;
- //Zadanie 4
- //Drzewo BST odczytano w porządku pre-order i otrzymano 6, 3, 1, 2, 4, 5, 7. Czy
- //możemy odtworzyć to drzewo? Napisz metodę odtwarzającą drzewo na podstawie
- //odczytu pre-order, jeżeli drzewa nie można odtworzyć(dane są sprzeczne np. 6, 3,
- //1, 2, 4, 7, 5) metoda ma zwracać drzewo puste.
- namespace Zadanie_4_przykład
- {
- class Węzeł
- {
- public int wartość;
- public Węzeł lewy;
- public Węzeł prawy;
- }
- class Drzewo
- {
- public Węzeł korzen;
- }
- class Program
- {
- static void Wstaw(Drzewo d, int k)
- {
- Węzeł w = new Węzeł();
- w.wartość = k;
- if (d.korzen == null)
- d.korzen = w;
- else
- {
- WstawRek(d.korzen, w);
- }
- }
- static void WstawRek(Węzeł w, Węzeł val)
- {
- if(val.wartość<w.wartość)
- {
- if (w.lewy == null)
- w.lewy = val;
- else WstawRek(w.lewy, val);
- }
- else
- {
- if (w.prawy== null)
- w.prawy = val;
- else WstawRek(w.prawy, val);
- }
- }
- static void Main(string[] args)
- {
- Drzewo d = new Drzewo();
- int[] tab = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- for(int i=0; i<tab.Length; i++)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement