Advertisement
Guest User

Untitled

a guest
Aug 4th, 2010
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Exercise_1
  7. {
  8.     public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
  9.     {
  10.         public T Value { get { return Value; } set { Value = value; } }
  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 left = false;
  18.             while (node != null) {
  19.                 if (node.Value.Equals(value))
  20.                     return;
  21.                 else {
  22.                     //search left if value is smaller
  23.                     bool searchLeft = Convert.ToBoolean(value.CompareTo(node.Value));
  24.  
  25.                     if (searchLeft) {
  26.                         previous = node;
  27.                         left = true;
  28.                         node = node.Left; //search left
  29.                     }
  30.                     else {
  31.                         previous = node;
  32.                         left = false;
  33.                         node = node.Right; //search right
  34.                     }
  35.                 }
  36.  
  37.             }//while
  38.             if (left)
  39.                 previous.Left = new Node<T>(value, previous);
  40.             else
  41.                 previous.Right = new Node<T>(value, previous);
  42.         } //insert
  43.  
  44.         public Node<T> Find(T value, Node<T> test = null) { //returns null if not found
  45.             Node<T> node = this;
  46.             while (node != null) {
  47.                 if (node.Value.Equals(value))
  48.                     return node;
  49.                 else {
  50.                     //search left if value is smaller
  51.                     bool searchLeft = Convert.ToBoolean(value.CompareTo(node.Value));
  52.  
  53.                     if (searchLeft)
  54.                         node = node.Left; //search left
  55.                     else
  56.                         node = node.Right; //search right
  57.                 }
  58.  
  59.             } //while
  60.             return null;
  61.         } //find
  62.  
  63.  
  64.         // Constructor
  65.         public Node(T value, Node<T> parent=null) { //if no second parameter, this will be the root of a new tree
  66.             //throw new Exception();
  67.             Value = value;
  68.             Parent = parent;
  69.             Left = null;
  70.             Right = null;
  71.         }
  72.  
  73.         //private static int CompareElements(IComparable x, IComparable y) {
  74.         //    return x.CompareTo(y);
  75.         //}
  76.  
  77.         public int CompareTo(Node<T> rhs) {
  78.             return Value.CompareTo(rhs.Value);
  79.         }
  80.  
  81.         public bool Equals(Node<T> rhs) {
  82.             return this.Value.Equals(rhs.Value);
  83.         }
  84.  
  85.  
  86.         public override string ToString() {
  87.             /*
  88.              * Simply do print left, print self, print right
  89.              */
  90.             if (this.Left != null)
  91.                 this.Left.ToString();
  92.  
  93.             Console.WriteLine(this.Value);
  94.  
  95.             if (this.Right != null)
  96.                 this.Right.ToString();
  97.  
  98.             return "";
  99.         }
  100.  
  101.         public static Node<T> MakeTree(params T[] list) {
  102.             Node<T> root = null;
  103.             if (list.Length > 0) {
  104.                 root = new Node<T>(list[0]);
  105.                 for (int i = 1; i < list.Length; i++) {
  106.                     root.Insert(list[i]);
  107.                 }
  108.             }
  109.             return root;
  110.         }
  111.     }
  112.     class Program
  113.     {
  114.         static void Main(string[] args) {
  115.             //Node<int> test;
  116.             //test = new Node<int>(5);
  117.             Node<char>.MakeTree('p', 'x', 's', 't', 'r', 'q', 'b', 'a');
  118.             System.Console.Read();
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement