Advertisement
g-stoyanov

TreeNode

Jun 2nd, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. /*
  2.  * Task 10*
  3.  * We are given numbers N and M and the following operations:
  4.  * N = N+1
  5.  * N = N+2
  6.  * N = N*2
  7.  * Write a program that finds the shortest sequence of operations
  8.  * from the list above that starts from N and finishes in M.
  9.  * Hint: use a queue.
  10.  * Example: N = 5, M = 16
  11.  * Sequence: 5 ---> 6 ---> 8 ---> 16
  12.  */
  13.  
  14. namespace Task10Version2ShortestSequenceOfOperation.Common
  15. {
  16.     using System;
  17.     using System.Collections.Generic;
  18.     using System.Linq;
  19.  
  20.     public class TreeNode<T>
  21.     {  
  22.         /// <summary>
  23.         /// List with children of node.
  24.         /// </summary>
  25.         private List<TreeNode<T>> children;
  26.  
  27.         /// <summary>
  28.         /// Initializes a new instance of the TreeNode<T> class.
  29.         /// </summary>
  30.         /// <param name="value">Value that keep node</param>
  31.         public TreeNode(T value)
  32.         {
  33.             this.Value = value;
  34.             this.children = new List<TreeNode<T>>();
  35.             this.NodeTreeLevel = 0;
  36.         }
  37.  
  38.         /// <summary>
  39.         /// Gets or sets value of node
  40.         /// </summary>
  41.         public T Value { get; set; }
  42.  
  43.         /// <summary>
  44.         /// Gets or sets node parent.
  45.         /// </summary>
  46.         public TreeNode<T> Parent { get; set; }
  47.  
  48.         /// <summary>
  49.         /// Gets or sets tree level of node.
  50.         /// </summary>
  51.         public int NodeTreeLevel { get; set; }
  52.  
  53.         /// <summary>
  54.         /// Adds node child.
  55.         /// </summary>
  56.         /// <param name="child">Child of type TreeNode</param>
  57.         public void AddChild(TreeNode<T> child)
  58.         {
  59.             child.Parent = this;
  60.             child.NodeTreeLevel = this.NodeTreeLevel + 1;
  61.             this.children.Add(child);
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Counts node children.
  66.         /// </summary>
  67.         /// <returns>Number of children</returns>
  68.         public int CountChildren()
  69.         {
  70.             int count = this.children.Count;
  71.  
  72.             return count;
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Get node child by index.
  77.         /// </summary>
  78.         /// <param name="index">Index of child.</param>
  79.         /// <returns>Child on this index.</returns>
  80.         public TreeNode<T> GetChild(int index)
  81.         {
  82.             return this.children[index];
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement