Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#

Tree Iterators

Rate me:
Please Sign up or sign in to vote.
4.75/5 (7 votes)
20 Mar 2009CPOL6 min read 60.3K   2K   42  
Discussing about Tree Iterators: Choices of Datastructure and Algorithm
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions