• API
• FAQ
• Tools
• Archive
SHARE
TWEET # TreeNode g-stoyanov   Jun 2nd, 2013 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
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.
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>
55.         /// </summary>
56.         /// <param name="child">Child of type TreeNode</param>
58.         {
59.             child.Parent = this;
60.             child.NodeTreeLevel = this.NodeTreeLevel + 1;
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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top