public static bool Delete <T>(this Skiplist <T> skiplist, T key)
        {
            if (skiplist.Head == null)
            {
                return(false);
            }

            if (skiplist.Head.Down == null && skiplist.AreEqual(skiplist.Head.Key, key))
            {
                return(TryRemoveCurrentHead(skiplist, key));
            }

            var keyFound =
                skiplist.Find(
                    key: key,
                    pathAction: node => HandlePathToDeletionPoint(node, key, skiplist.MinimumGapSize, skiplist.AreEqual),
                    origin: skiplist.Head.Down);

            if (skiplist.Head.Down.LengthOfList() <= 1)
            {
                skiplist.Head    = skiplist.Head.Down;
                skiplist.Head.Up = null;
            }

            return(keyFound);
        }
Ejemplo n.º 2
0
        public static bool Find <T>(
            this Skiplist <T> skiplist,
            T key,
            INode <T> origin = null,
            Action <INode <T> > pathAction = null)
        {
            var nodeOfKey = skiplist.FetchNode(key, origin, pathAction);

            return(skiplist.AreEqual(nodeOfKey.Key, key));
        }
 private static bool TryRemoveCurrentHead <T>(Skiplist <T> skiplist, T key)
 {
     if (skiplist.Head.Right == skiplist.Head && skiplist.AreEqual(skiplist.Head.Key, key))
     {
         skiplist.Head = null;
         return(true);
     }
     else
     {
         return(false);
     }
 }