Пример #1
0
        public static void InsertKey(T key, IntervalTree <T, R> tree)
        {
            IntervalTree <T, R> curr = new IntervalTree <T, R>(tree);

            if (FindTree(key, tree, ref curr))
            {
                return;
            }
            curr.key         = key;
            curr.isNILT      = false;
            curr.color       = Color.Red;
            curr.left_child  = new IntervalTree <T, R>(curr);
            curr.right_child = new IntervalTree <T, R>(curr);

            //link parent with curr
            if (key.CompareTo(curr.parent.key) > 0)
            {
                curr.parent.right_child = curr;
            }
            else
            {
                curr.parent.left_child = curr;
            }
            //Fix insertion violations
            FixUpInsert(ref curr);
            tree.maxInterval = tree.GetMax();
        }
Пример #2
0
        public static void DeleteKey(T key, ref IntervalTree <T, R> tree)
        {
            IntervalTree <T, R> curr = new IntervalTree <T, R>(null);

            FindTree(key, tree, ref curr);
            if (curr.isNILT)
            {
                return;
            }
            if (curr.IsLeaf)//A red node is leaf
            {
                if (IsLeftChild(curr))
                {
                    curr.parent.left_child = new IntervalTree <T, R>(curr.parent);//remove links with parent
                }
                else//symmetric
                {
                    curr.parent.right_child = new IntervalTree <T, R>(curr.parent);
                }
                if (curr.color == Color.Black)
                {
                    try
                    {
                        FixUpDelete(ref curr.parent);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                if (curr.left_child.isNILT)
                {
                    IntervalTree <T, R> suc = curr.right_child.TreeMinimum();
                    curr.key = suc.key;
                    DeleteKey(suc.key, ref suc);
                }
                else
                {
                    IntervalTree <T, R> suc = curr.left_child.TreeMaximum();
                    curr.key = suc.key;
                    DeleteKey(suc.key, ref suc);
                }
            }
            tree.maxInterval = tree.GetMax();
        }