Advertisement
vencinachev

TreeNode

Sep 8th, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. class TreeNode<T>
  2.     {
  3.         private T value;
  4.         private List<TreeNode<T>> children;
  5.         private bool hasParent;
  6.  
  7.         public T Value
  8.         {
  9.             get
  10.             {
  11.                 return this.value;
  12.             }
  13.             set
  14.             {
  15.                 if (value == null)
  16.                 {
  17.                     throw new ArgumentNullException();
  18.                 }
  19.                 this.value = value;
  20.             }
  21.         }
  22.  
  23.         public int ChildrenCount
  24.         {
  25.             get
  26.             {
  27.                 return this.children.Count;
  28.             }
  29.         }
  30.         public TreeNode(T value)
  31.         {
  32.             this.Value = value;
  33.             this.children = new List<TreeNode<T>>();
  34.         }
  35.         public void AddChild(TreeNode<T> child)
  36.         {
  37.             if (child == null)
  38.             {
  39.                 throw new ArgumentNullException();
  40.             }
  41.             if (child.hasParent)
  42.             {
  43.                 throw new ArgumentException();
  44.             }
  45.             child.hasParent = true;
  46.             this.children.Add(child);
  47.         }
  48.  
  49.         public TreeNode<T> GetChild(int index)
  50.         {
  51.             return this.children[index];
  52.         }
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement