コード例 #1
0
        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);
        }
コード例 #2
0
        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());
        }
コード例 #3
0
 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);
 }
コード例 #4
0
 public AnotherTreeIterator(TreeWithIterator <T> tree)
 {
     this.tree  = tree;
     this.queue = new Queue <IEnumerator <TreeWithIterator <T> > >();
 }
コード例 #5
0
 public void Reset()
 {
     this.current = null;
 }
コード例 #6
0
 private bool TraverseRight()
 {
     this.crumb.Push(this.current);
     this.current = this.current.RightNode;
     return(true);
 }
コード例 #7
0
 public TreeIterator(TreeWithIterator <T> tree)
 {
     this.tree  = tree;
     this.crumb = new Stack <TreeWithIterator <T> >();
 }
コード例 #8
0
        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();
        }