public static int RemoveTimeSubset <T>(this AvlTree <MappedInterval <T> > tree, long from, long to)
        {
            var removedNodesCount             = 0;
            INode <MappedInterval <T> > first = null;
            INode <MappedInterval <T> > last  = null;

            // TODO Remove .ToArray()
            foreach (var node in tree.FirstOrderedSubsequence(node => AcceptNode(node.Key, from, to)).ToArray())
            {
                if (first == null)
                {
                    first = node;
                }

                last = node;

                tree.Delete(node.Key);
                removedNodesCount++;
            }

            if (first != null && first.Key.IntervalStart < from)
            {
                tree.Insert(new MappedInterval <T>(first.Key.IntervalStart, from, first.Key.Payload));
            }

            if (last != null && to < last.Key.IntervalEnd)
            {
                tree.Insert(new MappedInterval <T>(to, last.Key.IntervalEnd, last.Key.Payload));
            }

            return(removedNodesCount);
        }
Ejemplo n.º 2
0
        public void BatchDelete()
        {
            // The constructed AVL Tree would be
            //     30
            //    /  \ 
            //   20   40
            //  /  \   \ 
            // 10  25   50

            var tree = new AvlTree <int>();

            tree.Insert(10);
            tree.Insert(20);
            tree.Insert(25);
            tree.Insert(30);
            tree.Insert(40);
            tree.Insert(50);
            tree.Insert(60);
            tree.Insert(70);
            tree.Insert(80);
            tree.Insert(90);
            tree.Insert(100);

            var toRemove     = tree.FirstOrderedSubsequence(node => node.Key >= 20 && node.Key < 35);
            var removedCount = tree.BatchDelete(toRemove.Select(node => node.Key));

            Assert.That(removedCount, Is.EqualTo(3));

            tree.TraversalMode = TraversalMode.InOrder;
            var actual = tree;

            CollectionAssert.AreEqual(new[] { 10, 40, 50, 60, 70, 80, 90, 100 }, actual);

            AssertTreeIsBalanced(tree);
        }
        public static IEnumerable <MappedInterval <T> > TimeSubset <T>(this AvlTree <MappedInterval <T> > tree, long from, long to)
        {
            var subsequence = tree.FirstOrderedSubsequence(node => AcceptNode(node.Key, from, to));

            return(subsequence.Select(node => node.Key));
        }