Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CustomTree
  8. {
  9.     public class TreeNode : ICloneable, IComparable<TreeNode>
  10.     {
  11.         private int value;
  12.         private bool isVisited;
  13.         public List<TreeNode> LeftChildren { get; set; }
  14.         public List<TreeNode> RightChildren { get; set; }
  15.  
  16.         public TreeNode(int value, List<TreeNode> leftChildren = null, List<TreeNode> rightChildren = null)
  17.         {
  18.             this.Value = value;
  19.             this.IsVisited = false;
  20.             this.RightChildren = new List<TreeNode>();
  21.             this.LeftChildren = new List<TreeNode>();
  22.  
  23.             if (leftChildren != null)
  24.             {
  25.                 foreach (TreeNode currentNode in leftChildren)
  26.                 {
  27.                     this.LeftChildren.Add(currentNode);
  28.                 }
  29.             }
  30.  
  31.             if (rightChildren != null)
  32.             {
  33.                 foreach (TreeNode currentNode in rightChildren)
  34.                 {
  35.                     this.RightChildren.Add(currentNode);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         public int Value
  41.         {
  42.             get
  43.             {
  44.                 return this.value;
  45.             }
  46.             set
  47.             {
  48.                 this.value = value;
  49.             }
  50.         }
  51.  
  52.         public bool IsVisited
  53.         {
  54.             get
  55.             {
  56.                 return this.isVisited;
  57.             }
  58.             set
  59.             {
  60.                 this.isVisited = value;
  61.             }
  62.         }
  63.  
  64.         public object Clone()
  65.         {
  66.             TreeNode clonedNode = new TreeNode(this.Value);
  67.             clonedNode.LeftChildren = new List<TreeNode>(this.LeftChildren);
  68.             clonedNode.RightChildren = new List<TreeNode>(this.RightChildren);
  69.  
  70.             return clonedNode;
  71.         }
  72.  
  73.         public override bool Equals(object obj)
  74.         {
  75.             return this.Value.Equals((obj as TreeNode).Value);
  76.         }
  77.  
  78.         public static bool operator ==(TreeNode firstNode, TreeNode secondNode)
  79.         {
  80.             return firstNode.Equals(secondNode);
  81.         }
  82.  
  83.         public static bool operator !=(TreeNode firstNode, TreeNode secondNode)
  84.         {
  85.             return !firstNode.Equals(secondNode);
  86.         }
  87.  
  88.         public int CompareTo(TreeNode other)
  89.         {
  90.             return this.Value.CompareTo(other.Value);
  91.         }
  92.  
  93.         public override string ToString()
  94.         {
  95.             return string.Format("Node: {0} number of left connections: {1}, number of right connections: {2}",
  96.                 this.Value, this.LeftChildren.Count, this.RightChildren.Count);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement