예제 #1
0
    // Suppose t != null.
    TreapNode <T> RemoveTarget(TreapNode <T> t, T item)
    {
        if (t.TypedLeft == null && t.TypedRight == null)
        {
            --Count;
            return(null);
        }

        if (t.TypedRight == null)
        {
            return(t.TypedLeft);
        }
        if (t.TypedLeft == null)
        {
            return(t.TypedRight);
        }

        if (t.TypedLeft.Priority > t.TypedRight.Priority)
        {
            t = t.RotateToRight() as TreapNode <T>;
        }
        else
        {
            t = t.RotateToLeft() as TreapNode <T>;
        }
        return(Remove(t, item));
    }
예제 #2
0
    TreapNode <T> Add(TreapNode <T> node, T item, int priority)
    {
        if (node == null)
        {
            ++Count;
            return(new TreapNode <T> {
                Key = item, Priority = priority
            });
        }

        var d = compare(item, node.Key);

        if (d == 0)
        {
            return(node);
        }

        if (d < 0)
        {
            node.TypedLeft = Add(node.TypedLeft, item, priority);
            if (node.Priority < node.TypedLeft.Priority)
            {
                node = node.RotateToRight() as TreapNode <T>;
            }
        }
        else
        {
            node.TypedRight = Add(node.TypedRight, item, priority);
            if (node.Priority < node.TypedRight.Priority)
            {
                node = node.RotateToLeft() as TreapNode <T>;
            }
        }
        return(node);
    }