|
using System;
using System.Collections.Generic;
using System.Text;
namespace TreeIterator
{
public class BreadthTreeEnumerator<T> : TreeEnumerator<T>
{
//private INode<T> _Node = null;
private Queue<INode<T>> _Queue;
public BreadthTreeEnumerator(INode<T> tree)
: base(tree) {
_Queue = new Queue<INode<T>>();
_Queue.Enqueue(tree);
}
//public override bool MoveNext() {
// if (_Current == null) _Current = _Tree;
// else if (_Current.Right != null) _Current = _Current.Right;
// else {
// if (_Current == _Tree) _Current = _Tree.Child;
// else _Current = _Current.Parent.Child.Child;
// return _Current != null;
// }
// return true;
//}
public override bool MoveNext() {
if (_Queue.Count == 0 && _Current.Right == null) return false;
else if(_Current == null || _Current.Right == null)
{
_Current = _Queue.Dequeue();
if(_Current.Child != null) _Queue.Enqueue(_Current.Child);
}
else
{
_Current = _Current.Right;
if (_Current.Child != null) _Queue.Enqueue(_Current.Child);
}
return true;
}
// p = out(q);
// if (!is_empty_tree(p)) {
// f(p->label);
// in(p->left, q);
// in(p->right, q);
// }
// }
//}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.