public void Add()
        {
            SplayTree<int, string> tree = new SplayTree<int, string>();

            var a = tree.Add(10, "a");
            var b = tree.Add(5, "b");

            Assert.IsTrue(b == tree.Root);
            Assert.IsTrue(b.Right == a);

            var c = tree.Add(7, "c");

            Assert.IsTrue(c == tree.Root);
            Assert.IsTrue(c.Left == b);
            Assert.IsTrue(c.Right == a);

            var d = tree.Add(3, "d");

            Assert.IsTrue(d == tree.Root);
            Assert.IsTrue(d.Left == null);
            Assert.IsTrue(d.Right == b);
            Assert.IsTrue(b.Left == null);
            Assert.IsTrue(b.Right == c);
            Assert.IsTrue(c.Right == a);
            Assert.IsTrue(c.Left == null);
        }
Beispiel #2
0
        public void Simple()
        {
            var splayTree = new SplayTree<int, string>();

            Assert.IsFalse(splayTree.ContainsKey(5));

            splayTree.Add(4, "4");
            Assert.AreEqual(splayTree[4], "4");
            Assert.IsTrue(splayTree.ContainsKey(4));
            Assert.IsFalse(splayTree.ContainsKey(5));

            splayTree.Add(6, "6");
            Assert.AreEqual(splayTree[6], "6");
            Assert.IsTrue(splayTree.ContainsKey(4));
            Assert.IsFalse(splayTree.ContainsKey(5));
            Assert.IsTrue(splayTree.ContainsKey(6));

            splayTree.Add(2, "2");
            Assert.AreEqual(splayTree[2], "2");
            Assert.IsTrue(splayTree.ContainsKey(2));
            Assert.IsTrue(splayTree.ContainsKey(4));
            Assert.IsFalse(splayTree.ContainsKey(5));
            Assert.IsTrue(splayTree.ContainsKey(6));

            splayTree.Add(5, "5");
            Assert.AreEqual(splayTree[5], "5");
            Assert.IsTrue(splayTree.ContainsKey(2));
            Assert.IsTrue(splayTree.ContainsKey(4));
            Assert.IsTrue(splayTree.ContainsKey(5));
            Assert.IsTrue(splayTree.ContainsKey(6));

            var rand = new Random();

            splayTree = new SplayTree<int, string>();

            var list = new List<int>();

            for (var i = 0; i < 100; i++)
            {
                int r;

                do
                {
                    r = rand.Next(5000);
                }
                while (list.Contains(r));

                list.Add(r);

                splayTree.Add(r, null);

                Assert.IsTrue(splayTree.ContainsKey(r));
            }
        }
        public void Find()
        {
            SplayTree<int, string> tree = new SplayTree<int, string>();

            var a = tree.Add(5, "a");
            var b = tree.Add(3, "b");
            var c = tree.Add(1, "c");
            var d = tree.Add(2, "d");
            var e = tree.Add(10, "e");

            var found = tree.Find(5);
            Assert.IsTrue(found == a);
            Assert.IsTrue(tree.Root == a);
        }
Beispiel #4
0
        public void KeyValuePair()
        {
            var splayTree = new SplayTree<int, string>();

            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(5, "5")));

            splayTree.Add(4, "4");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair<int, string>(4, "4")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(4, "5")));

            splayTree.Add(6, "6");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair<int, string>(6, "6")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(6, "5")));
        }
Beispiel #5
0
        public void KeyValuePair()
        {
            var splayTree = new SplayTree<int, string>();

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                splayTree.Add(new KeyValuePair<int, string>(gen, gen.ToString()));

                Assert.AreEqual(splayTree.Count, i + 1);
                Assert.IsTrue(splayTree.ContainsKey(gen));
            }
        }
Beispiel #6
0
        public void Simple()
        {
            var splayTree = new SplayTree<int, string>();

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                splayTree.Add(gen, gen.ToString());

                string val;

                Assert.AreEqual(splayTree.Count, i + 1);

                splayTree.TryGetValue(gen, out val);
                Assert.AreEqual(val, gen.ToString());
                Assert.AreEqual(splayTree[gen], gen.ToString());
            }

            string val2;
            splayTree.TryGetValue(2001, out val2);
            Assert.IsNull(val2);
        }
Beispiel #7
0
        public void Set()
        {
            var splayTree = new SplayTree <int, string>();

            for (var i = 20; i > 10; i--)
            {
                splayTree.Add(i, i.ToString());
            }

            for (var i = 0; i < 10; i++)
            {
                splayTree.Add(i, i.ToString());
            }

            Assert.AreEqual(splayTree[0], "0");
            splayTree[0] = "1";
            Assert.AreEqual(splayTree[0], "1");

            splayTree[1] = "4";
            Assert.AreEqual(splayTree[1], "4");
        }
Beispiel #8
0
        public void Set()
        {
            var splayTree = new SplayTree<int, string>();

            for (var i = 20; i > 10; i--)
            {
                splayTree.Add(i, i.ToString());
            }

            for (var i = 0; i < 10; i++)
            {
                splayTree.Add(i, i.ToString());
            }

            Assert.AreEqual(splayTree[0], "0");
            splayTree[0] = "1";
            Assert.AreEqual(splayTree[0], "1");

            splayTree[1] = "4";
            Assert.AreEqual(splayTree[1], "4");
        }
        public void SplayTreeAddThreeValues()
        {
            var tree  = new SplayTree <int, int>();
            var node1 = tree.Add(1, 1);
            var node2 = tree.Add(2, 2);
            var node3 = tree.Add(3, 3);

            Assert.AreEqual(node1.Key, 1);
            Assert.AreEqual(node1.Value, 1);
            Assert.AreEqual(node2.Key, 2);
            Assert.AreEqual(node2.Value, 2);
            Assert.AreEqual(node3.Key, 3);
            Assert.AreEqual(node3.Value, 3);
            Assert.AreEqual(tree.Count, 3);
            Assert.IsTrue(tree.Root.HasValue);
            var root = tree.Root.Value;

            Assert.AreEqual(root.Value, 3);
            Assert.AreEqual(root.Key, 3);
            Assert.IsFalse(root.Parent.HasValue);
            Assert.IsTrue(root.Left.HasValue);
            Assert.IsFalse(root.Right.HasValue);
            var left = root.Left.Value;

            Assert.AreEqual(left.Value, 2);
            Assert.AreEqual(left.Key, 2);
            Assert.IsTrue(left.Parent.HasValue);
            Assert.AreEqual(left.Parent.Value.Value, 3);
            Assert.IsFalse(left.Right.HasValue);
            Assert.IsTrue(left.Left.HasValue);
            var leftLeft = left.Left.Value;

            Assert.AreEqual(leftLeft.Value, 1);
            Assert.AreEqual(leftLeft.Key, 1);
            Assert.IsTrue(leftLeft.Parent.HasValue);
            Assert.AreEqual(leftLeft.Parent.Value.Value, 2);
            Assert.IsFalse(leftLeft.Right.HasValue);
            Assert.IsFalse(leftLeft.Left.HasValue);
        }
        public void SplayTreeAddOneValue()
        {
            var tree = new SplayTree <string, int>();
            var node = tree.Add("1", 1);

            Assert.AreEqual(tree.Count, 1);
            Assert.AreEqual(node.Key, "1");
            Assert.AreEqual(node.Value, 1);
            Assert.IsFalse(node.Parent.HasValue);
            Assert.IsFalse(node.Left.HasValue);
            Assert.IsFalse(node.Right.HasValue);
            Assert.ReferenceEquals(tree.Root.Value.ThisNode, node.ThisNode);
        }
Beispiel #11
0
        public void Simple()
        {
            var splayTree = new SplayTree <int, string>();

            for (var i = 20; i > 10; i--)
            {
                splayTree.Add(i, i.ToString());
            }

            for (var i = 0; i <= 10; i++)
            {
                splayTree.Add(i, i.ToString());
            }

            var values = splayTree.Values;

            for (var i = 0; i <= 20; i++)
            {
                Assert.IsTrue(values.Contains(i.ToString()));
            }

            Assert.AreEqual(21, values.Count);
        }
Beispiel #12
0
        public void AddKVPTest()
        {
            SplayTree <string, string> t = new SplayTree <string, string>();

            foreach (var w in _Words)
            {
                t.Add(new KeyValuePair <string, string>(w.Key, w.Value));
                Assert.IsTrue(t.Contains(w.Key, w.Value));

                Exception ex = null;

                try
                {
                    t.Add(new KeyValuePair <string, string>(w.Key, w.Value + "_Test"));
                }
                catch (Exception innerEx)
                {
                    ex = innerEx;
                }

                Assert.IsInstanceOfType(ex, typeof(ArgumentException));
            }
        }
Beispiel #13
0
        public void Simple()
        {
            var splayTree = new SplayTree<int, string>();

            for (var i = 20; i > 10; i--)
            {
                splayTree.Add(i, i.ToString());
            }

            for (var i = 0; i <= 10; i++)
            {
                splayTree.Add(i, i.ToString());
            }

            var values = splayTree.Values;

            for (var i = 0; i <= 20; i++)
            {
                Assert.IsTrue(values.Contains(i.ToString()));
            }

            Assert.AreEqual(values.Count, 21);
        }
Beispiel #14
0
        public void Simple()
        {
            var splayTree = new SplayTree <int, string>();

            for (var i = 20; i > 10; i--)
            {
                splayTree.Add(i, i.ToString());
            }

            for (var i = 0; i <= 10; i++)
            {
                splayTree.Add(i, i.ToString());
            }

            var keys = splayTree.Keys;

            for (var i = 0; i <= 20; i++)
            {
                Assert.IsTrue(keys.Contains(i));
            }

            Assert.AreEqual(keys.Count, 21);
        }
        public void SplayTreeGetEnumerator()
        {
            var keys = new string[] { "A", "B", "C" };
            var tree = new SplayTree <string, int>();

            for (int i = 0; i < keys.Length; i++)
            {
                tree.Add(keys[i], i + 1);
            }
            int counter = 0;

            foreach (var elem in tree)
            {
                Assert.AreEqual(elem.Key, keys[counter]);
                Assert.AreEqual(elem.Value, counter + 1);
                counter++;
            }
        }
        public void SplayTreeCopyTo()
        {
            var values = new string[] { "a", "b", "c", "d" };
            var tree   = new SplayTree <int, string>();

            for (int i = 0; i < values.Length; i++)
            {
                tree.Add(i + 1, values[i]);
            }
            var array = new KeyValuePair <int, string> [5];

            tree.CopyTo(array, 1);
            for (int i = 0; i < values.Length; i++)
            {
                Assert.AreEqual(array[i + 1].Key, i + 1);
                Assert.AreEqual(array[i + 1].Value, values[i]);
            }
        }
Beispiel #17
0
        public void CheckIfNodeIsInvalidatedAfterClearingAndAfterRemoval()
        {
            var tree = new SplayTree <int>();

            tree.Add(3);
            tree.Add(1);
            tree.Add(2);

            // Tree looks like this:
            //   2
            //  / \
            // 1   3

            var node1 = tree.Root.Left;
            var node2 = tree.Root;
            var node3 = tree.Root.Right;

            tree.Remove(2);
            if (node2.Left != null || node2.Right != null)
            {
                Assert.Fail("2");
            }

            tree.Remove(3);
            if (node3.Left != null || node3.Right != null)
            {
                Assert.Fail("3");
            }

            tree.Remove(1);
            if (node1.Left != null || node1.Right != null)
            {
                Assert.Fail("1");
            }

            tree.Add(3);
            tree.Add(1);
            tree.Add(2);

            node1 = tree.Root.Left;
            node2 = tree.Root;
            node3 = tree.Root.Right;

            tree.Clear();

            Assert.IsTrue(node1.Left == null && node1.Right == null &&
                          node2.Left == null && node2.Right == null &&
                          node3.Left == null && node3.Right == null &&
                          tree.Root == null &&
                          tree.Count == 0);
        }
Beispiel #18
0
        public static void TestSplayOperations()
        {
            var init = new int [] {8,3,10,1,6,4,7,14,13};
            var splayTree = new SplayTree(init);

            var result = splayTree.Find(6);
            Assert.AreEqual(result.Value, 6);
            Assert.AreEqual(splayTree.Root.Value, 6);

            splayTree.Remove(10);
            var resultRemove = splayTree.Find(10);
            Assert.IsNull(resultRemove);
            Assert.AreEqual(splayTree.Root.Value, 8);

            splayTree.Add(12);
            Assert.AreEqual(splayTree.Root.Value, 12);
            Assert.AreEqual(splayTree.Root.LeftChild.Value, 8);
            Assert.AreEqual(splayTree.Root.RightChild.Value, 13);
        }
Beispiel #19
0
 public void SearchTThreshold(IList<IList<int>> PostingLists, int T, out IList<int> docs, out IList<short> card)
 {
     // K - T = Number of mismatches (errors)
     int suggestedCardinality = 1024;
     docs = new List<int> (suggestedCardinality);
     card = new List<short> (suggestedCardinality);
     int docsI = 0;
     var Start = new int[PostingLists.Count];
     Comparison<int> comptop = delegate(int x, int y) {
         this.CompCounter++;
         return PostingLists[x][Start[x]] - PostingLists[y][Start[y]];
     };
     SplayTree<int> pqueue = new SplayTree<int> (comptop);
     for (int i = 0; i < PostingLists.Count; i++) {
         pqueue.Add (i);
     }
     docs.Add (ShiftFirst (PostingLists, pqueue, Start));
     card.Add (1);
     while (pqueue.Count > 0) {
         int docid = ShiftFirst (PostingLists, pqueue, Start);
         this.CompCounter++;
         if (docid == docs[docsI]) {
             card[docsI]++;
         } else {
             if (card[docsI] < T) {
                 docs[docsI] = docid;
                 card[docsI] = 1;
             } else {
                 docs.Add (docid);
                 card.Add (1);
                 docsI++;
             }
         }
     }
     // The last item docs[docsI] can holds vals[docsI] < T
     // Console.WriteLine ("===> cards: ");
     if (card[docsI] < T) {
         card.RemoveAt (docsI);
         docs.RemoveAt (docsI);
     }
 }
Beispiel #20
0
        static void Main()
        {
            string s = Console.ReadLine();

            SplayTree tree = new SplayTree();

            foreach (char ch in s)
            {
                tree.Add(ch);
            }

            int n = int.Parse(Console.ReadLine());

            int[] arr;
            for (int i = 0; i < n; i++)
            {
                arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                tree.Replace(arr[0], arr[1], arr[2]);
            }

            tree.ShowString();
            Console.Read();
        }
Beispiel #21
0
    public static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        SplayTree <int> set = new SplayTree <int>();
        var             q   = NextInt;

        for (int i = 0; i < q; i++)
        {
            if (NextInt == 1)
            {
                set.Add(NextInt);
            }
            else
            {
                var res = set[NextInt - 1];
                Console.WriteLine(res);
                set.Remove(res);
            }
        }
        Console.Out.Flush();
    }
Beispiel #22
0
        private void ProcessData(Tuple <Stack <byte>, Stack <Stack <int> > > data)
        {
            var sw = Stopwatch.StartNew();

            var commands  = data.Item1;
            var arguments = data.Item2;

            int argOffset  = 0;
            int listOffset = 0;

            // Prepare the heap
            var heap        = new FibonacciHeap <int, int>();
            int insertCount = GetNextArg(ref argOffset, arguments, ref listOffset);

            NodeItem <int, int>[] insertedNodes = new NodeItem <int, int> [insertCount];
            int deleteDepthCount = 0;
            int deleteCount      = 0;

            // Do insert commands
            int id = 0, key = 0;
            NodeItem <int, int> node;

            foreach (byte command in commands)
            {
                if (command != DelKey)
                {
                    id  = GetNextArg(ref argOffset, arguments, ref listOffset);
                    key = GetNextArg(ref argOffset, arguments, ref listOffset);
                }

                switch (command)
                {
                case InsKey:
                    node = heap.Add(key, id);
                    Debug.Assert(insertedNodes[id] == null);
                    insertedNodes[id] = node;
                    break;

                case DelKey:
                    node = heap.PeekMin();
                    Debug.Assert(insertedNodes[node.Value] != null);
                    insertedNodes[node.Value] = null;
                    heap.DeleteMin();

                    deleteDepthCount += heap.LastConsolidateDepth;
                    deleteCount++;
                    break;

                case DecKey:
                    node = insertedNodes[id];

                    if (node == null || key > node.Key)
                    {
                        break;
                    }

                    heap.DecreaseKey(node, key);
                    break;
                }
            }

            // Cleanup and store the measurements
            //heap.Clear(); // Disassembles tree node pointers .... not a good idea with a GC...

            sw.Stop();

            float avgDeleteDepthCount = 0;

            if (deleteCount > 0)
            {
                avgDeleteDepthCount = deleteDepthCount / (float)deleteCount;
            }

            lock (_results)
                _results.Add(deleteCount, avgDeleteDepthCount);

            Interlocked.Increment(ref _currentJobsDone);
            Log("{0}/{1} done/waiting :: {2:F} sec :: {3} inserts :: {4}/{5:F} deletes/delete depth average",
                _currentJobsDone,
                Buffer.WaitingItemCount,
                sw.ElapsedMilliseconds * 0.001,
                insertCount,
                deleteCount,
                avgDeleteDepthCount);
        }
Beispiel #23
0
        public void Simple2()
        {
            var splayTree = new SplayTree<int, string>();

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                splayTree.Add(gen, gen.ToString());

                Assert.AreEqual(splayTree.Count, i + 1);
                Assert.IsTrue(splayTree.ContainsKey(gen));
            }

            using (var enumerator = dictionary.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Assert.IsTrue(splayTree.Remove(enumerator.Current));
                }
            }
        }
Beispiel #24
0
        public void SortedElementsAfterAddingAndRemoving()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            //Removing every second number
            for (int i = 0; i < elementsCount; i += 2)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }


            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Trace.WriteLine(tree.Count);
            Trace.WriteLine(count);
            Trace.WriteLine(removedEverything);
            Trace.WriteLine(elementsAreSorted);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          removedEverything);
        }
Beispiel #25
0
        public void AddingAfterRemovingAllElements()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            if (tree.Count != elementsCount)
            {
                Assert.Fail();
            }

            //Removing every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    tree.Remove(el);
                    el += i;
                }
            }

            if (tree.Count != 0)
            {
                Assert.Fail();
            }

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Beispiel #26
0
        public void CheckIfTreeIsSplayedAfterRemoval()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                // Get parent of node for removal. That node have to be splayed
                SplayTreeNode <int> curNode  = tree.Root;
                SplayTreeNode <int> lastNode = null;
                while (curNode.Value != i)
                {
                    var cmp = i.CompareTo(curNode.Value);
                    if (cmp == 0)
                    {
                        break;
                    }

                    lastNode = curNode;

                    if (cmp > 0)
                    {
                        curNode = curNode.Right;
                    }
                    else
                    {
                        curNode = curNode.Left;
                    }
                }

                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
                else if (lastNode != null)
                {
                    Assert.IsTrue(tree.Root.Value == lastNode.Value);
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
Beispiel #27
0
        public void CheckIfTreeIsSplayedAfterAddAndAfterContains()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            if (tree.Root.Value != 50)
            {
                Assert.Fail();
            }

            tree.Add(75);
            if (tree.Root.Value != 75)
            {
                Assert.Fail();
            }

            tree.Add(25);
            if (tree.Root.Value != 25)
            {
                Assert.Fail();
            }

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }

                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Beispiel #28
0
        public void RemoveRootEveryTimeUntilTreeElementsAreHalfed()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            while (tree.Count > elementsCount / 2)
            {
                if (!tree.Remove(tree.Root.Value))
                {
                    removedEverything = false;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          count == elementsCount / 2 &&
                          removedEverything);
        }
Beispiel #29
0
        public List <TextBox> getRectsForRange(int start, int end)
        {
            var lineBoxes = new SplayTree <int, List <TextBox> >();

            for (int runIndex = 0; runIndex < this._codeUnitRunsCount; runIndex++)
            {
                var run = this._codeUnitRuns[runIndex];
                if (run.codeUnits.start >= end)
                {
                    break;
                }

                if (run.codeUnits.end <= start)
                {
                    continue;
                }

                float top = (run.lineNumber == 0) ? 0 : this._lineHeights[run.lineNumber - 1];
                float bottom = this._lineHeights[run.lineNumber];
                float left, right;
                if (run.codeUnits.start >= start && run.codeUnits.end <= end)
                {
                    left  = run.xPos.start;
                    right = run.xPos.end;
                }
                else
                {
                    left  = float.MaxValue;
                    right = float.MinValue;
                    for (int i = 0; i < run.count; i++)
                    {
                        var gp = run.get(i, this._glyphPositions);
                        if (gp.codeUnit >= start && gp.codeUnit + 1 <= end)
                        {
                            left  = Mathf.Min(left, gp.xPos.start);
                            right = Mathf.Max(right, gp.xPos.end);
                        }
                    }

                    if (left == float.MaxValue || right == float.MinValue)
                    {
                        continue;
                    }
                }

                List <TextBox> boxs;
                if (!lineBoxes.TryGetValue(run.lineNumber, out boxs))
                {
                    boxs = new List <TextBox>();
                    lineBoxes.Add(run.lineNumber, boxs);
                }

                boxs.Add(TextBox.fromLTBD(left, top, right, bottom, run.direction));
            }

            for (int lineNumber = 0; lineNumber < this._lineRangeCount; ++lineNumber)
            {
                var line = this._lineRanges[lineNumber];
                if (line.start >= end)
                {
                    break;
                }

                if (line.endIncludingNewLine <= start)
                {
                    continue;
                }

                if (!lineBoxes.ContainsKey(lineNumber))
                {
                    if (line.end != line.endIncludingNewLine && line.end >= start && line.endIncludingNewLine <= end)
                    {
                        var x      = this._lineWidths[lineNumber];
                        var top    = (lineNumber > 0) ? this._lineHeights[lineNumber - 1] : 0;
                        var bottom = this._lineHeights[lineNumber];
                        lineBoxes.Add(lineNumber, new List <TextBox> {
                            TextBox.fromLTBD(
                                x, top, x, bottom, TextDirection.ltr)
                        });
                    }
                }
            }

            var result = new List <TextBox>();

            foreach (var keyValuePair in lineBoxes)
            {
                result.AddRange(keyValuePair.Value);
            }

            return(result);
        }
Beispiel #30
0
 int ShiftFirst(IList<IList<int>> PostingLists, SplayTree<int> pqueue, int[] Start)
 {
     int pIndex = pqueue.RemoveFirst ();
     var startIndex = Start[pIndex];
     int docid = PostingLists[pIndex][startIndex];
     startIndex++;
     if (startIndex < PostingLists[pIndex].Count) {
         Start[pIndex] = startIndex;
         pqueue.Add (pIndex);
     }
     return docid;
 }
        public void TestAllOperationsOnRandomNumbers()
        {
            int             count = 100000; //MIN IS 100
            List <int>      numbers = new List <int>(count);
            List <int>      removed = new List <int>(count);
            SplayTree <int> tree = new SplayTree <int>();
            Random          random = new Random();
            int             addCount = 0, succAddCount = 0,
                            findCount = 0, succFindCount = 0,
                            deleteCount = 0, succDeleteCount = 0,
                            compareCheck = 0;


            for (int i = 0; i < count; i++)
            {
                int number = random.Next(0, 100 * count);
                if (number % 3 == 0)
                {
                    addCount++;
                    if (tree.Add(number))
                    {
                        succAddCount++;
                        numbers.Add(number);
                        if (removed.Contains(number))
                        {
                            removed.Remove(number);
                        }
                    }
                }
                else
                if (number % 3 == 1)
                {
                    findCount++;
                    if (numbers.Count > 0)
                    {
                        Assert.IsTrue(tree.Contains(numbers[random.Next(0, numbers.Count)]));
                        //COULD BE CHANGED TO PROFIT FROM SPLAY
                        succFindCount++;
                    }
                    if (removed.Count > 0)
                    {
                        Assert.IsFalse(tree.Contains(removed[random.Next(0, removed.Count)]));
                    }
                }
                else
                if (number % 3 == 2)
                {
                    deleteCount++;
                    if (numbers.Count > 0)
                    {
                        int toDelete = numbers[random.Next(0, numbers.Count)];
                        tree.Delete(toDelete);
                        succDeleteCount++;
                        numbers.Remove(toDelete);
                        removed.Add(toDelete);
                    }
                    if (removed.Count > 0)
                    {
                        tree.Delete(removed[random.Next(0, removed.Count)]); // SHOULD DO NOTHING
                    }
                }
                if (number % (count / 100) == 0)
                {
                    compareCheck++;
                    foreach (int num in numbers)
                    {
                        Assert.IsTrue(tree.Contains(num));
                    }
                    foreach (int num in removed)
                    {
                        Assert.IsFalse(tree.Contains(num));
                    }
                    Assert.IsTrue(tree.CheckTreeStructure());
                }
            }


            String treeTraversalLevelOrder = tree.TraverseLevelOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestLevelOrder.txt",
                              treeTraversalLevelOrder);
            String treeTraversalInOrder = tree.TraverseInOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestInOrder.txt",
                              treeTraversalInOrder);
            String checkInfo = "ADD checks: " + addCount + "\r\nSuccessfully added elements: " + succAddCount +
                               "\r\nFIND checks: " + findCount + "\r\nSuccessfully found elements: " + succFindCount +
                               "\r\nDELETE checks: " + deleteCount + "\r\nSuccessfully deleted elements: " + succDeleteCount +
                               "\r\nTree-List comparisons: " + compareCheck;

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestStats.txt",
                              checkInfo);
        }