2 using System.Collections;
3 using System.Collections.Generic;
5 namespace Mercury.Nucleus.Trees
13 public class DFSTreeEnumerator<T> : IEnumerator<T>, IEnumerator
19 private Stack<Tree<T>> nodes;
32 this.nodes =
new Stack<Tree<T>>();
41 public T Current {
get {
return nodes.Peek().Value; } }
47 object IEnumerator.Current {
get {
return nodes.Peek().Value; } }
51 #region [ IEnumerator ]
61 if (end)
throw new InvalidOperationException();
64 { nodes.Push(root);
return true; }
66 var tree = nodes.Pop();
68 for (
int ix = tree.Children.Count - 1; ix >= 0; --ix)
71 return !(end = (nodes.Count == 0));
90 public class BFSTreeEnumerator<T> : IEnumerator<T>, IEnumerator
96 private Queue<Tree<T>> queue;
109 this.queue =
new Queue<Tree<T>>();
118 public T Current {
get {
return queue.Peek().Value; } }
124 object IEnumerator.Current {
get {
return queue.Peek().Value; } }
128 #region [ IEnumerator ]
138 if (end)
throw new InvalidOperationException();
140 if (queue.Count == 0)
141 { queue.Enqueue(root);
return true; }
143 Tree<T> tree = queue.Dequeue();
144 for (
int ix = 0; ix < tree.Children.Count; ++ix)
145 queue.Enqueue(tree[ix]);
147 return !(end = (queue.Count == 0));
DFSTreeEnumerator(Tree< T > root)
Creates a new instance of the DFSTreeEnumerator{T} class.
BFSTreeEnumerator(Tree< T > root)
Creates a new instance of the BFSTreeEnumerator{T} class.
A non-balanced tree implementation that can be referenced from many other trees. Because of that...