public void BinarySearchTreeEnumeratorTest() { // enumerator BinarySearchTree <Int32, String> tree = new BinarySearchTree <Int32, String>(); foreach (KeyValuePair <Int32, String> keyValuePair in this.values) { if (!tree.Contains(keyValuePair.Key)) { tree.Insert(keyValuePair.Key, keyValuePair.Value); } } Int32 prevKey = this.values.Min(keyValuePair => keyValuePair.Key) - 1; Int32 count = 0; foreach (KeyValuePair <Int32, String> element in tree) { element.Key.ShouldBeGreaterThan(prevKey); prevKey = element.Key; count++; } tree.Count.ShouldBe(count); // search tree enumerator tree = new BinarySearchTree <Int32, String>(); foreach (KeyValuePair <Int32, String> keyValuePair in this.values) { if (!tree.Contains(keyValuePair.Key)) { tree.Insert(keyValuePair.Key, keyValuePair.Value); } } ISearchTreeEnumerator <Int32, String> enumerator = tree.GetTreeEnumerator(); List <Int32> forwardList = new List <Int32>(); List <Int32> backwardList = new List <Int32>(); if (enumerator.MoveMin()) { do { forwardList.Add(enumerator.Current.Key); }while (enumerator.MoveNext()); } if (enumerator.MoveMax()) { do { backwardList.Add(enumerator.Current.Key); }while (enumerator.MovePrev()); } backwardList.Reverse(); forwardList.ShouldBe(backwardList); }
public void BinarySearchTreeEnumeratorTest() { // test case 1 BinarySearchTree <Int32, String> bst = new BinarySearchTree <Int32, String>(); Random random = new Random(); for (Int32 i = 0; i < 1000; i++) { Int32 key = random.Next(1, 1000); if (!bst.Contains(key)) { bst.Insert(key, i.ToString()); } } Int32 prevKey = 0; Int32 count = 0; foreach (KeyValuePair <Int32, String> element in bst) { Assert.IsTrue(element.Key > prevKey); prevKey = element.Key; count++; } Assert.AreEqual(bst.Count, count); // test case 2 bst = new BinarySearchTree <Int32, String>(); for (Int32 i = 0; i < 1000; i++) { Int32 key = random.Next(1, 1000); if (!bst.Contains(key)) { bst.Insert(key, i.ToString()); } } ISearchTreeEnumerator <Int32, String> enumerator = bst.GetTreeEnumerator(); List <Int32> forwardList = new List <Int32>(); List <Int32> backwardList = new List <Int32>(); if (enumerator.MoveMin()) { do { forwardList.Add(enumerator.Current.Key); } while (enumerator.MoveNext()); } if (enumerator.MoveMax()) { do { backwardList.Add(enumerator.Current.Key); } while (enumerator.MovePrev()); } backwardList.Reverse(); Assert.IsTrue(forwardList.SequenceEqual(backwardList)); }