static void TreeDestroyer(ref BinPoint head) { if (head.left != null) { TreeDestroyer(ref head.left); } if (head.right != null) { TreeDestroyer(ref head.right); } Console.WriteLine("Element {0} destroying", head.data); head = null; }
static void ShowTree(BinPoint p, int l) { if (p != null) { ShowTree(p.left, l + 3); for (int i = 0; i < l; i++) { Console.Write(" "); } Console.WriteLine(p.data); ShowTree(p.right, l + 3); } //else Console.WriteLine("Дерево пустое"); }
static BinPoint IdealTree(int size, BinPoint p) { BinPoint r; int nl, nr; if (size == 0) { p = null; return(p); } else { nl = size / 2; nr = size - nl - 1; Console.WriteLine("введите элемент дерева"); bool ok = false; char number; string s = Console.ReadLine(); do { ok = char.TryParse(s, out number); if (ok == false) { Console.WriteLine("Incorrect element"); s = Console.ReadLine(); } else if ((s == " ") || (s == "\t")) { Console.WriteLine("Введён пробел/таб, он будет обозначен как _"); number = '_'; } //else if(s=="") //{ // Console.WriteLine //} } while (!ok); r = new BinPoint(number); r.left = IdealTree(nl, r.left); r.right = IdealTree(nr, r.right); return(r); } }
static void Main(string[] args) { BinPoint head = null; Console.WriteLine("Введите количество элементов дерева"); int h = NaturalAndZeroCheck(); if (h != 0) { head = IdealTree(h, head); Console.WriteLine("Дерево введено"); } else { Console.WriteLine("Tree is empty"); } ShowTree(head, 0); TreeDestroyer(ref head); if (head == null) { Console.WriteLine("Three destroyed"); } Console.ReadLine(); }
public BinPoint(char d) { data = d; left = null; right = null; }
public BinPoint() { data = ' '; left = null; right = null; }