Exemplo n.º 1
0
 private static WBT <Pair <k, v> > .N Alter(Func <k, Ord> p, k key, Func <Opt <v>, Opt <v> > f, WBT <Pair <k, v> > .N t) =>
 WBT <Pair <k, v> > .Tip(t)
         ? (f(Ctors.None())).Case(_ => WBT <Pair <k, v> > .Tip(), (v vv) => WBT <Pair <k, v> > .Singleton(Ctors.Pair(key, vv)))
         : (p(t.v.Left()).Case(
                _ => WBT <Pair <k, v> > .Balance(t.v, Alter(p, key, f, t.l), t.r),
                _ => f(Ctors.Some(t.v.Right())).Case(
                    __ => WBT <Pair <k, v> > .Glue(t.l, t.r),
                    vv => WBT <Pair <k, v> > .Bin(t.s, Ctors.Pair(key, vv), t.l, t.r)),
                _ => WBT <Pair <k, v> > .Balance(t.v, t.l, Alter(p, key, f, t.r))));
Exemplo n.º 2
0
            public static Pair <a, N> DeleteFindMax(N t)
            {
                if (Tip(t))
                {
                    throw new InvalidOperationException("No maximum for an empty tree");
                }
                if (Tip(t.r))
                {
                    return(Ctors.Pair(t.v, t.l));
                }
                var max = DeleteFindMax(t.r);

                return(Ctors.Pair(max.Left(), BalanceL(t.v, t.l, max.Right())));
            }
Exemplo n.º 3
0
            public static Pair <a, N> DeleteFindMin(N t)
            {
                if (Tip(t))
                {
                    throw new InvalidOperationException("No minimum for an empty tree");
                }
                if (Tip(t.l))
                {
                    return(Ctors.Pair(t.v, t.r));
                }
                var min = DeleteFindMin(t.l);

                return(Ctors.Pair(min.Left(), BalanceR(t.v, min.Right(), t.r)));
            }
Exemplo n.º 4
0
 public TreeMap <k, v> Singleton(k key, v value, ROrder <k, Unit> ord = null) => New(ord, WBT <Pair <k, v> > .Singleton(Ctors.Pair(key, value)));
Exemplo n.º 5
0
 private static WBT <Pair <k, v> > .N Map(Func <k, v, v> f, WBT <Pair <k, v> > .N t) =>
 WBT <Pair <k, v> > .Tip(t) ? t : WBT <Pair <k, v> > .Bin(t.s, Ctors.Pair(t.v.Left(), t.v.Case(f)), Map(f, t.l), Map(f, t.r));