Untitled
unknown
plain_text
a year ago
3.7 kB
5
Indexable
using System; namespace drzewko_binarne_lab_06 { class Drzewko { private int Wartość; private Drzewko Lewo; private Drzewko Prawo; public Drzewko() { Lewo = null; Prawo = null; } private int CharToInt(char x) { return (int)x - 48; } public void Dopisz() { Console.WriteLine("Podaj ciąg znaków, tworzących drzewko binarne (np. 4P5L7*8): "); string input = Console.ReadLine(); Drzewko korzeń = this; korzeń.Wartość = CharToInt(input[0]); for (int i = 1; i < input.Length; i++) { if (CharToInt(input[i]) >= 0 && CharToInt(input[i]) <= 9) { if (CharToInt(input[i - 1]) >= 0 && CharToInt(input[i - 1]) <= 9) { korzeń.Lewo = new Drzewko(); korzeń = korzeń.Lewo; } korzeń.Wartość = CharToInt(input[i]); } if (input[i] == 'L') { korzeń.Lewo = new Drzewko(); korzeń = korzeń.Lewo; } if (input[i] == 'P') { korzeń.Prawo = new Drzewko(); korzeń = korzeń.Prawo; } if (input[i] == '*') { if (input[i + 1] == '*') { continue; } if (CharToInt(input[i - 1]) >= 0 && CharToInt(input[i - 1]) <= 9) { korzeń.Prawo = new Drzewko(); korzeń = korzeń.Prawo; } } } } public void PreOrder() { Console.Write($"{Wartość} "); if (Lewo != null) { Console.Write("L"); Lewo.PreOrder(); } else { Console.Write("*L"); } if (Prawo != null) { Console.Write("P"); Prawo.PreOrder(); } else { Console.Write("*P"); } } public void InOrder() { if (Lewo != null) Lewo.InOrder(); Console.Write($"{Wartość} "); if (Prawo != null) Prawo.InOrder(); } public void PostOrder() { if (Lewo != null) Lewo.PostOrder(); if (Prawo != null) Prawo.PostOrder(); Console.Write($"{Wartość} "); } public void Usuń() { if (Lewo != null) Lewo.Usuń(); if (Prawo != null) Prawo.Usuń(); Lewo = null; Prawo = null; } } class Program { static void Main() { Drzewko binarne = new Drzewko(); binarne.Dopisz(); Console.WriteLine("\nMetoda PreOrder:"); binarne.PreOrder(); Console.WriteLine("\nMetoda InOrder:"); binarne.InOrder(); Console.WriteLine("\nMetoda PostOrder:"); binarne.PostOrder(); binarne.Usuń(); Console.WriteLine("\nDrzewko zostało usunięte z pamięci RAM."); Console.ReadKey(); } } }
Editor is loading...
Leave a Comment