/// <summary>Constructs the tree</summary> /// <param name="value">the value of the node</param> public Tree(T value) { if (value != null) { this.root = new TreeNodes <T>(value); } }
static void Main(string[] args) { Tree <int> tree = new Tree <int>(8); TreeNodes <int> three = new TreeNodes <int>(10); TreeNodes <int> ten = new TreeNodes <int>(14); TreeNodes <int> one = new TreeNodes <int>(13); TreeNodes <int> six = new TreeNodes <int>(3); TreeNodes <int> fourteen = new TreeNodes <int>(6); TreeNodes <int> four = new TreeNodes <int>(7); TreeNodes <int> seven = new TreeNodes <int>(4); TreeNodes <int> thirteen = new TreeNodes <int>(1); tree.root.AddChild(three); tree.root.AddChild(ten); three.AddChild(one); three.AddChild(six); six.AddChild(four); six.AddChild(seven); ten.AddChild(fourteen); fourteen.AddChild(thirteen); tree.DepthFirstSearch(); Console.ReadLine(); }
public void DepthFirstSearch() { //create new stack Queue <TreeNodes <T> > stack = new Queue <TreeNodes <T> >(); //put root in stack stack.Enqueue(root); while (stack.Count != 0) { //get first item of stack TreeNodes <T> v = stack.Dequeue(); Console.WriteLine(v.value); foreach (TreeNodes <T> c in v.children) { stack.Enqueue(c); } } }
public void AddChild(TreeNodes <T> child) { child.hasParent = true; this.children.Add(child); }