private bool TraverseUpAndRight() { if (this.crumb.Count > 0) { this.current = this.crumb.Pop(); if (this.current.RightNode != null) { this.current = this.current.RightNode; return(true); } } return(false); }
public bool MoveNext() { if (this.current == null) { Reset(); this.current = this.tree; return(true); } if (this.current.LeftNode != null) { return(TraverseLeft()); } if (this.current.RightNode != null) { return(TraverseRight()); } return(TraverseUpAndRight()); }
public bool MoveNext() { if (this.current == null) { this.Reset(); this.current = this.tree; this.queue.Enqueue(this.current.GetChildren().GetEnumerator()); return(true); } while (this.queue.Count > 0) { var enumerator = this.queue.Peek(); if (enumerator.MoveNext()) { this.current = enumerator.Current; this.queue.Enqueue(this.current.GetChildren().GetEnumerator()); return(true); } this.queue.Dequeue(); } return(false); }
public AnotherTreeIterator(TreeWithIterator <T> tree) { this.tree = tree; this.queue = new Queue <IEnumerator <TreeWithIterator <T> > >(); }
public void Reset() { this.current = null; }
private bool TraverseRight() { this.crumb.Push(this.current); this.current = this.current.RightNode; return(true); }
public TreeIterator(TreeWithIterator <T> tree) { this.tree = tree; this.crumb = new Stack <TreeWithIterator <T> >(); }
public static void Main(string[] args) { var tree = new Tree <string>("0") { LeftNode = new Tree <string>("1") { LeftNode = new Tree <string>("2") { LeftNode = new Tree <string>("8"), RightNode = new Tree <string>("9") }, RightNode = new Tree <string>("5") }, RightNode = new Tree <string>("3") { LeftNode = new Tree <string>("6"), RightNode = new Tree <string>("7") } }; var list = tree.ToList(); Console.WriteLine("Non iterator tree"); Console.WriteLine(string.Join(" ", list)); var tree2 = new TreeWithIterator <string>("0") { LeftNode = new TreeWithIterator <string>("1") { LeftNode = new TreeWithIterator <string>("2") { LeftNode = new TreeWithIterator <string>("8"), RightNode = new TreeWithIterator <string>("9") }, RightNode = new TreeWithIterator <string>("5") }, RightNode = new TreeWithIterator <string>("3") { LeftNode = new TreeWithIterator <string>("6"), RightNode = new TreeWithIterator <string>("7") } }; Console.WriteLine(); Console.WriteLine("Iterator tree"); foreach (var item in tree2) { Console.Write(item + " "); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Direct root children"); foreach (var item in tree2.GetChildren()) { Console.Write(item + " "); } Console.WriteLine(); Console.ReadKey(); }