Beispiel #1
0
        public void StoreFifthValueInLeftRightNode()
        {
            int            expectedValue = 5;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            Assert.AreEqual(expectedValue, myTree.LeftChild.RightChild.Value);
        }
Beispiel #2
0
        public void StoreThirdValueInRightNode()
        {
            int            expectedValue = 3;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            Assert.AreEqual(expectedValue, myTree.RightChild.Value);
        }
Beispiel #3
0
        public void StoreFourthValueInLeftLeftNode()
        {
            int            expectedValue = 4;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            Assert.AreEqual(expectedValue, myTree.LeftChild.LeftChild.Value);
        }
Beispiel #4
0
 public bool MoveNext()
 {
     if (_current == null)
     {
         Reset();
         _current = _tree;
         _enumerators.Enqueue(_current.Children().GetEnumerator());
         return(true);
     }
     while (_enumerators.Count > 0)
     {
         var enumerator = _enumerators.Peek();
         if (enumerator.MoveNext())
         {
             _current = enumerator.Current;
             _enumerators.Enqueue(_current.Children().GetEnumerator());
             return(true);
         }
         else
         {
             _enumerators.Dequeue();
         }
     }
     return(false);
 }
Beispiel #5
0
        public void StoreValueInRootNode()
        {
            int            expectedValue = 1;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            Assert.AreEqual(expectedValue, myTree.Value);
        }
Beispiel #6
0
        public void StoreSixthValueInRightLeftNode()
        {
            int            expectedValue = 6;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            DisplayTree(myTree);
            Assert.AreEqual(expectedValue, myTree.RightChild.LeftChild.Value);
        }
Beispiel #7
0
        public void InitialTreeHasDepthZero()
        {
            var myTree = new DemoTree <int>();

            myTree.Add(1);

            Assert.AreEqual(0, myTree.Depth());
        }
Beispiel #8
0
        public void ListValuesInDepthFirstOrderWithEnumerator()
        {
            int            expectedValue = 6;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            DisplayTree(myTree);
            var valuesString = String.Join(" ", myTree.ToArray());

            Assert.AreEqual("1 2 4 5 3 6", valuesString);
        }
Beispiel #9
0
        public void ListValuesInBreadthFirstOrderWithEnumerator()
        {
            int            expectedValue = 14;
            DemoTree <int> myTree        = CreateTreeWithValues(expectedValue);

            myTree.UseBreadthFirstEnumerator = true;

            DisplayTree(myTree);
            var valuesString = String.Join(" ", myTree.ToArray());

            Assert.AreEqual("1 2 3 4 5 6 7 8 9 12 13 10 11 14", valuesString);
        }
Beispiel #10
0
        private DemoTree <int> CreateTreeWithValues(int numberOfValues)
        {
            if (numberOfValues <= 0)
            {
                return(null);
            }

            var tree = new DemoTree <int>(1);

            for (int i = 2; i <= numberOfValues; i++)
            {
                tree.Add(i);
            }
            return(tree);
        }
Beispiel #11
0
 public bool MoveNext()
 {
     if (_current == null)
     {
         Reset();
         _current = _tree;
         return(true);
     }
     if (_current.LeftChild != null)
     {
         return(TraverseLeft());
     }
     if (_current.RightChild != null)
     {
         return(TraverseRight());
     }
     return(TraverseUpAndRight());
 }
Beispiel #12
0
 public void Add(T value)
 {
     if (LeftChild == null)
     {
         LeftChild = new DemoTree <T>(value);
         return;
     }
     if (RightChild == null)
     {
         RightChild = new DemoTree <T>(value);
         return;
     }
     if (LeftChild.Depth() <= RightChild.Depth())
     {
         LeftChild.Add(value);
         return;
     }
     RightChild.Add(value);
 }
Beispiel #13
0
 private bool TraverseUpAndRight()
 {
     if (_breadcrumb.Count > 0)
     {
         _previous = _current;
         while (true)
         {
             _current = _breadcrumb.Pop();
             if (_previous != _current.RightChild)
             {
                 break;
             }
         }
         if (_current.RightChild != null)
         {
             _breadcrumb.Push(_current);
             _current = _current.RightChild;
             return(true);
         }
     }
     return(false);
 }
Beispiel #14
0
        public void DisplayTree(DemoTree <int> tree)
        {
            var serializer = new XmlSerializer(tree.GetType());

            serializer.Serialize(Console.Out, tree);
        }
Beispiel #15
0
 public void Reset()
 {
     _current = null;
     _enumerators.Clear();
 }
Beispiel #16
0
        public void StartEmpty()
        {
            var myTree = new DemoTree <string>();

            Assert.IsNull(myTree.Value);
        }
Beispiel #17
0
 public DemoTreeEnumerator(DemoTree <T> tree)
 {
     _tree = tree;
 }
Beispiel #18
0
 private bool TraverseRight()
 {
     _breadcrumb.Push(_current);
     _current = _current.RightChild;
     return(true);
 }
Beispiel #19
0
 public void Reset()
 {
     _current = null;
 }
Beispiel #20
0
 public DemoTreeBreadthFirstEnumerator(DemoTree <T> tree)
 {
     _tree = tree;
 }