Разработать таблицу имен, обеспечивающую динамическое выделение памяти под имена по мере надобности - C#
Формулировка задачи:
Разработать таблицу имен, обеспечивающую динамическое выделение памяти под имена по мере надобности. Таблица должна быть построена на основе двоичного дерева.
Решение задачи: «Разработать таблицу имен, обеспечивающую динамическое выделение памяти под имена по мере надобности»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
namespace ConsoleApplication3
{
public class Tree : IEnumerable<Tree>
{
public Tree Left { get; set; }
public Tree Right { get; set; }
public string Value { get; set; }
public Tree() { }
public Tree(string node)
{
Value = node;
}
public void Add(string Value)
{
if (this.Value == null)
this.Value = Value;
else
{
if (this.Value.CompareTo(Value) < 0)
if (Left != null)
Left.Add(Value);
else
Left = new Tree(Value);
if (this.Value.CompareTo(Value) > 0)
if (Right != null)
Right.Add(Value);
else
Right = new Tree(Value);
}
}
#region IEnumerable
private IEnumerable<Tree> Across(Tree node)
{
if (node != null)
{
Queue<Tree> spisok = new Queue<Tree>();
spisok.Enqueue(node);
while (spisok.Count > 0)
{
var x = spisok.Peek();
if (x.Left != null)
spisok.Enqueue(x.Left);
if (x.Right != null)
spisok.Enqueue(x.Right);
yield return spisok.Dequeue();
}
}
}
public IEnumerator<Tree> GetEnumerator()
{
foreach (var i in Across(this))
yield return i;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
class Program
{
static void Main()
{
Tree tr = new Tree();
tr.Add("F");
tr.Add("B");
tr.Add("H");
tr.Add("A");
tr.Add("D");
tr.Add("G");
tr.Add("N");
foreach (var i in tr)
Console.WriteLine(i.Value);
Console.ReadLine();
}
}
}