예제 #1
0
 public void FindLargestBelowTest()
 {
     tree = new SplayTree <int> {
         5, 11, 8, 13, 3, 7, 12
     };
     Assert.AreEqual(8, tree.LargestBelow(10));
 }
예제 #2
0
 public void FindSmallestAboveTest()
 {
     tree = new SplayTree <int> {
         5, 11, 8, 13, 3, 7, 12
     };
     Assert.AreEqual(11, tree.SmallestAbove(8));
 }
예제 #3
0
 public void RemoveFailTest()
 {
     tree = new SplayTree <int> {
         77, 85, 66, 56, 74, 94, 27, 61, 48, 57
     };
     Assert.IsFalse(tree.Remove(34));
 }
예제 #4
0
 public void FindSmallestAboveTestFail()
 {
     tree = new SplayTree <int> {
         5, 11, 8, 13, 3, 7, 12
     };
     tree.SmallestAbove(13);
 }
예제 #5
0
        public void WeissTest()
        {
            tree = new SplayTree <int>();
            const int NUMS = 40000;
            const int GAP  = 307;

            for (int i = GAP; i != 0; i = (i + GAP) % NUMS)
            {
                tree.Add(i);
            }

            for (int i = 1; i < NUMS; i += 2)
            {
                tree.Remove(i);
            }

            Assert.AreEqual(2, tree.Minimum);
            Assert.AreEqual(NUMS - 2, tree.Maximum);

            for (int i = 2; i < NUMS; i += 2)
            {
                Assert.IsTrue(tree.Contains(i));
            }

            for (int i = 1; i < NUMS; i += 2)
            {
                Assert.IsFalse(tree.Contains(i));
            }
        }
예제 #6
0
        public void EnumerationEmptyTest()
        {
            tree = new SplayTree <int>();
            IEnumerator <int> e = tree.GetEnumerator();

            Assert.IsFalse(e.MoveNext());
        }
예제 #7
0
 public void FindLargestBelowTestFail()
 {
     tree = new SplayTree <int> {
         5, 11, 8, 13, 3, 7, 12
     };
     tree.LargestBelow(3);
 }
예제 #8
0
        /// <summary>
        /// Merge two splay trees, assumes all of the items in rhs are
        /// greater than last.
        /// All of the items in rhs are transferred to this, replacing all
        /// elements in this tree greater than last. After the
        /// operator rhs IsEmpty.
        /// </summary>
        /// <param name="last">The last (greatest) element of this tree that will be preserved</param>
        /// <param name="rhs">The tree to be merged in</param>
        /// <returns>The merged result tree</returns>
        /// <exception cref="ArgumentNullException">Throws in rhs is null</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if all elements in rhs are not greater than last.</exception>
        public SplayTree <T> Merge(T last, SplayTree <T> rhs)
        {
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs", "Merge attemped with null rhs SplayTree");
            }

            if (rhs.IsEmpty)
            {
                return(this);
            }

            if (comparer.Compare(last, rhs.Minimum) > 0)
            {
                throw new ArgumentOutOfRangeException("rhs", rhs, "All elements in the rhs SplayTree must be greater than last.");
            }
            Debug.Assert(comparer.Compare(last, rhs.Minimum) <= 0);

            if (IsEmpty)
            {
                root  = rhs.root;
                count = rhs.count;
                rhs.Clear();
                return(this);
            }
            Contains(last);
            root.right = rhs.root;
            count      = null; // Invalidate count
            rhs.Clear();
            return(this);
        }
예제 #9
0
        public void TryGetValueNegativeTest()
        {
            int[] array = new int[] { 64, 3, 16, 73, 67 };
            tree = new SplayTree <int>(array);
            int  result;
            bool success = tree.TryGetValue(15, out result);

            Assert.IsFalse(success);
        }
예제 #10
0
 public void ClearTest()
 {
     int[] array = new int[] { 100, 29, 77, 59, 61 };
     tree = new SplayTree <int>(array);
     Assert.IsFalse(tree.IsEmpty);
     tree.Clear();
     Assert.IsTrue(tree.IsEmpty);
     Assert.AreEqual(0, tree.Count);
 }
예제 #11
0
        public void TryGetValuePositiveTest()
        {
            int[] array = new int[] { 64, 3, 16, 73, 67 };
            tree = new SplayTree <int>(array);
            int  result;
            bool success = tree.TryGetValue(16, out result);

            Assert.IsTrue(success);
            Assert.AreEqual(16, result);
        }
예제 #12
0
 public void ConstructFromEnumerable()
 {
     int[] array = new int[] { 41, 92, 71, 12, 20 };
     tree = new SplayTree <int>(array);
     Assert.AreEqual(array.Length, tree.Count);
     foreach (int item in array)
     {
         Assert.IsTrue(tree.Contains(item));
     }
 }
예제 #13
0
 public void RootTest()
 {
     tree = new SplayTree <int> {
         5, 3, 7
     };
     Assert.IsTrue(tree.Contains(5));
     Assert.AreEqual(5, tree.Root);
     Assert.AreEqual(7, tree.Right);
     Assert.AreEqual(3, tree.Minimum);
     Assert.AreEqual(7, tree.Maximum);
 }
예제 #14
0
 public void CountTest()
 {
     tree = new SplayTree <int>();
     tree.Add(5);
     Assert.AreEqual(1, tree.Count);
     tree.Add(3);
     Assert.AreEqual(2, tree.Count);
     tree.Remove(5);
     Assert.AreEqual(1, tree.Count);
     tree.Add(7);
     Assert.AreEqual(2, tree.Count);
     tree.Add(9);
     Assert.AreEqual(3, tree.Count);
     tree.Add(5);
     Assert.AreEqual(4, tree.Count);
     tree.Remove(7);
     Assert.AreEqual(3, tree.Count);
 }
예제 #15
0
        public void EnumerationTest()
        {
            int[] array = new int[] { 64, 3, 16, 73, 67 };
            tree = new SplayTree <int>(array);
            IEnumerator <int> e = tree.GetEnumerator();

            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(3, e.Current);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(16, e.Current);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(64, e.Current);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(67, e.Current);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(73, e.Current);
            Assert.IsFalse(e.MoveNext());
        }
예제 #16
0
        public void SplitKeepTest()
        {
            tree = new SplayTree <int> {
                5, 11, 8, 13, 3, 7, 12
            };
            SplayTree <int> leftover = tree.SplitAt(11, true);

            Assert.AreEqual(5, tree.Count);
            Assert.AreEqual(2, leftover.Count);
            Assert.IsTrue(tree.Contains(3));
            Assert.IsTrue(tree.Contains(5));
            Assert.IsTrue(tree.Contains(7));
            Assert.IsTrue(tree.Contains(8));
            Assert.IsTrue(tree.Contains(11));
            Assert.IsFalse(tree.Contains(12));
            Assert.IsFalse(tree.Contains(13));
            Assert.IsTrue(leftover.Contains(12));
            Assert.IsTrue(leftover.Contains(13));
        }
예제 #17
0
        public void MergeTest()
        {
            tree = new SplayTree <int> {
                5, 3, 7, 12
            };
            SplayTree <int> tree2 = new SplayTree <int> {
                11, 8, 13
            };

            tree.Merge(7, tree2);
            Assert.AreEqual(6, tree.Count);
            Assert.AreEqual(0, tree2.Count);
            Assert.IsTrue(tree.Contains(3));
            Assert.IsTrue(tree.Contains(5));
            Assert.IsTrue(tree.Contains(7));
            Assert.IsTrue(tree.Contains(8));
            Assert.IsTrue(tree.Contains(11));
            Assert.IsFalse(tree.Contains(12));
            Assert.IsTrue(tree.Contains(13));
        }
예제 #18
0
        /// <summary>
        /// Split the splay tree into to at the given value.  Elements less than value
        /// will remain in this tree, elements greater than value will be in the returned tree.
        /// </summary>
        /// <param name="value">The value at which the split is to be made.</param>
        /// <param name="keep">Controls whether elements equal to value are remain in this tree (true)
        /// or are moved to the returned tree (false).</param>
        /// <returns></returns>
        public SplayTree <T> SplitAt(T value, bool keep)
        {
            SplayTree <T> leftover = new SplayTree <T>();

            Contains(value);
            int comp = comparer.Compare(value, root.element);

            if (comp > 0 || comp == 0 && keep)
            {
                leftover.root = root.right;
                root.right    = nullNode;
            }
            else
            {
                leftover.root      = root;
                root               = root.left;
                leftover.root.left = nullNode;
            }
            count          = null; // Invalidate count
            leftover.count = null; // Invalidate count
            return(leftover);
        }
예제 #19
0
        public SplayDictionary(IComparer <K> comparer)
        {
            var keyComparer = new KeyComparer <K, V>(comparer);

            tree = new SplayTree <KeyValuePair <K, V> >(keyComparer);
        }
예제 #20
0
 public void MaximumFailTest()
 {
     tree = new SplayTree <int>();
     int dummy = tree.Maximum;
 }
예제 #21
0
 public void EmptinessTest()
 {
     tree = new SplayTree <int>();
     tree.Add(42);
     Assert.IsFalse(tree.IsEmpty);
 }
예제 #22
0
 public void DefaultConstructor()
 {
     tree = new SplayTree <int>();
     Assert.IsTrue(tree.IsEmpty);
     Assert.AreEqual(0, tree.Count);
 }