public void AvlTreeEnumeratorTest() { // enumerator AvlTree <Int32, String> tree = new AvlTree <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++; } count.ShouldBe(tree.Count); // search tree enumerator tree = new AvlTree <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 AvlTree <Int32, String> avl = new AvlTree <Int32, String>(); Random random = new Random(); for (Int32 i = 0; i < 1000; i++) { Int32 key = random.Next(1, 1000); if (!avl.Contains(key)) { avl.Insert(key, i.ToString()); } } Int32 prevKey = 0; Int32 count = 0; foreach (KeyValuePair <Int32, String> element in avl) { Assert.IsTrue(element.Key > prevKey); prevKey = element.Key; count++; } Assert.AreEqual(avl.Count, count); // test case 2 avl = new AvlTree <Int32, String>(); random = new Random(); for (Int32 i = 0; i < 1000; i++) { Int32 key = random.Next(1, 1000); if (!avl.Contains(key)) { avl.Insert(key, i.ToString()); } } ISearchTreeEnumerator <Int32, String> enumerator = avl.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)); }