using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace lab09_enumerate { class Program { static void Main(string[] args) { Book b1 = new Book("A Bridge Too Near", "Bonnie", "Taylor", 2000); Book b2 = new Book("A Bridge Too Near 2", "Bonnie", "Taylor", 2002); Book b3 = new Book("A Bridge Too Near 3", "Bonnie", "Taylor", 2005); Book b4 = new Book("A Bridge Too Near: Even Nearer", "Bonnie", "Taylor", 2007); Node root = new Node(b1); root.Add(b2); root.Add(b3); root.Add(b4); foreach (Book b in root.GetBooks()) { if (b.Year > 2002) { Console.WriteLine("({0}) {1}", b.Year, b.Title); } } } } class Book { public string Title { get; private set; } public string AuthorFirstName { get; private set; } public string AuthorLastName { get; private set; } public int Year { get; private set; } public Book(string title, string firstname, string lastname, int year) { Title = title; AuthorFirstName = firstname; AuthorLastName = lastname; Year = year; } public override string ToString() { return String.Format("{0}: {1}, {2} ({3})", Title, AuthorLastName, AuthorFirstName, Year); } } class Node { public Node Left { get; set; } public Node Right { get; set; } public Book Data { get; private set; } public void Add(Book b) { // if I have an open spot, add here if (Left == null) { Left = new Node(b); return; } else if (Right == null) { Right = new Node(b); return; } // otherwise, just add left (very unbalanced) Left.Add(b); } public void Print() { if (Left != null) Left.Print(); Console.WriteLine(Data); if (Right != null) Right.Print(); } public IEnumerable GetBooks() { if (Left != null) foreach (Book b in Left.GetBooks()) yield return b; yield return Data; if (Right != null) foreach (Book b in Right.GetBooks()) yield return b; } public Node(Book b) { Data = b; } public override string ToString() { return Data.ToString(); } } }