Advertisement
Guest User

Untitled

a guest
Aug 9th, 2010
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Exercises
  7. {
  8.     public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
  9.     {
  10.         public T Value { get; set; }
  11.         public Node<T> Left { get; set; }
  12.         public Node<T> Right { get; set; }
  13.         public Node<T> Parent { get; set; }
  14.         void Insert(T value) {
  15.             Node<T> node = this;
  16.             Node<T> previous = null;
  17.             bool searchLeft = false;
  18.             while (node != null) {
  19.                 if (node.Value.Equals(value))
  20.                     return;
  21.                 else {
  22.                     //search left if value is smaller
  23.                     if (value.CompareTo(node.Value) < 0)
  24.                         searchLeft = true;
  25.                     else
  26.                         searchLeft = false;
  27.  
  28.                     if (searchLeft) {
  29.                         previous = node;
  30.                         node = node.Left; //search left
  31.                     }
  32.                     else {
  33.                         previous = node;
  34.                         node = node.Right; //search right
  35.                     }
  36.                 }
  37.  
  38.             }//while
  39.             if (searchLeft)
  40.                 previous.Left = new Node<T>(value, previous);
  41.             else
  42.                 previous.Right = new Node<T>(value, previous);
  43.         } //insert
  44.  
  45.         public Node<T> Find(T value, Node<T> test = null) { //returns null if not found
  46.             Node<T> node = this;
  47.             bool searchLeft = false;
  48.             while (node != null) {
  49.                 if (node.Value.Equals(value))
  50.                     return node;
  51.                 else {
  52.                     //search left if value is smaller
  53.                     if (value.CompareTo(node.Value) < 0)
  54.                         searchLeft = true;
  55.                     else
  56.                         searchLeft = false;
  57.  
  58.                     if (searchLeft)
  59.                         node = node.Left; //search left
  60.                     else
  61.                         node = node.Right; //search right
  62.                 }
  63.  
  64.  
  65.             } //while
  66.             return null;
  67.         } //find
  68.  
  69.  
  70.         // Constructor
  71.         public Node(T value, Node<T> parent = null) { //if no second parameter, this will be the root of a new tree
  72.             //throw new Exception();
  73.             Value = value;
  74.             Parent = parent;
  75.             Left = null;
  76.             Right = null;
  77.         }
  78.  
  79.         public int CompareTo(Node<T> rhs) {
  80.             return Value.CompareTo(rhs.Value);
  81.         }
  82.  
  83.         public bool Equals(Node<T> rhs) {
  84.             return this.Value.Equals(rhs.Value);
  85.         }
  86.  
  87.  
  88.         public override string ToString() {
  89.             /*
  90.              * Simply do print left, print self, print right
  91.              */
  92.             StringBuilder mystring = new StringBuilder("");
  93.             if (this.Left != null)
  94.                 mystring.Append(this.Left.ToString());
  95.  
  96.             mystring.Append(this.Value);
  97.  
  98.             if (this.Right != null)
  99.                 mystring.Append(this.Right.ToString());
  100.  
  101.             return mystring.ToString();
  102.         } //ToString
  103.  
  104.         public static Node<T> MakeTree(params T[] list) {
  105.             Node<T> root = null;
  106.             if (list.Length > 0) {
  107.                 root = new Node<T>(list[0]);
  108.                 for (int i = 1; i < list.Length; i++) {
  109.                     root.Insert(list[i]);
  110.                 }
  111.             }
  112.             return root;
  113.         } //MakeTree
  114.  
  115.  
  116.         public Node<TOut> MapTree<TOut>(Converter<T, TOut> f) where TOut : IComparable<TOut> {
  117.             Node<TOut> node;
  118.             node = new Node<TOut>(f.Invoke(this.Value));
  119.             if (this.Left != null) {
  120.                 node.Left = this.Left.MapTree<TOut>(new Converter<T, TOut>(f));
  121.                 node.Left.Parent = node;
  122.             }
  123.             if (this.Right != null) {
  124.                 node.Right = this.Right.MapTree<TOut>(new Converter<T, TOut>(f));
  125.                 node.Right.Parent = node;
  126.             }
  127.             return node;
  128.         }
  129.  
  130.         public IEnumerable<T> Values() {
  131.             if (this.Left != null)
  132.                 this.Left.Values();
  133.  
  134.             yield return this.Value;
  135.  
  136.             if (this.Right != null)
  137.                 this.Right.Values();
  138.  
  139.         }
  140.  
  141.     }
  142.  
  143.     class Program
  144.     {
  145.         public static void Pause() {
  146.             Console.Write("Press any key to continue . . . ");
  147.             Console.ReadKey(true);
  148.         }
  149.  
  150.         public static int f(char c) {
  151.             return (int)c;
  152.         }
  153.  
  154.         static void Main(string[] args) {
  155.             Node<char> testnode, testnode2;
  156.             Node<int> testnode3;
  157.             testnode = Node<char>.MakeTree('p', 'x', 's', 't', 'r', 'q', 'b', 'a');
  158.             testnode2 = Node<char>.MakeTree('b', 'x', 'r', 't', 's', 'q', 'p', 'a');
  159.             Console.WriteLine(testnode.ToString());
  160.             Console.WriteLine(testnode2.ToString());
  161.  
  162.             testnode3 = testnode.MapTree<int>(new Converter<char, int>(f));
  163.             Console.WriteLine(testnode3.ToString());
  164.             //List<int> newlist = testnode3.Values();
  165.             IEnumerable<int> yo = testnode3.Values();
  166.             foreach (int item in yo)
  167.                 Console.WriteLine(item);
  168.  
  169.             Pause();
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement