예제 #1
0
        public void Delete(int key)
        {
            SkipNode[] update = new SkipNode[maxLevel + 1];
            SkipNode   cursor = header;

            for (int i = level; i >= 0; i--)
            {
                while (cursor.link[i].key < key)
                {
                    cursor = cursor.link[i];
                }
                update[i] = cursor;
            }
            cursor = cursor.link[0];
            if (cursor.key == key)
            {
                for (int i = 0; i < level; i++)
                {
                    if (update[i].link[i] == cursor)
                    {
                        update[i].link[i] = cursor.link[i];
                    }
                    while (level > 0 && header.link[level].key == NIL)
                    {
                        level--;
                    }
                }
            }
        }
예제 #2
0
        private void SkipList2(float probable, int maxLevel)
        {
            this.probability = probable;
            this.maxLevel    = maxLevel;
            level            = 0;
            header           = new SkipNode(maxLevel, 0, null);
            SkipNode nilElement = new SkipNode(maxLevel, NIL, null);

            for (int i = 0; i < maxLevel; i++)
            {
                header.link[i] = nilElement;
            }
        }
예제 #3
0
        public Object Search(int key)
        {
            SkipNode cursor = header;

            for (int i = level; i >= 0; i--)
            {
                SkipNode nextElement = cursor.link[i];
                while (nextElement.key < key)
                {
                    cursor      = nextElement;
                    nextElement = cursor.link[i];
                }
            }
            cursor = cursor.link[0];
            if (cursor.key == key)
            {
                return(cursor.value);
            }
            else
            {
                return(" Object not found");
            }
        }
예제 #4
0
        public void Insert(int key, Object value)
        {
            SkipNode[] update = new SkipNode[maxLevel];
            SkipNode   cursor = header;

            for (int i = level; i >= 0; i--)
            {
                while (cursor.link[i].key < key)
                {
                    cursor = cursor.link[i];
                }
                update[i] = cursor;
            }
            cursor = cursor.link[0];
            if (cursor.key == key)
            {
                cursor.value = value;
            }
            else
            {
                int newLevel = GenRandomLevel();
                if (newLevel > level)
                {
                    for (int i = level; i < newLevel; i++)
                    {
                        update[i] = header;
                    }
                    level = newLevel;
                }
                cursor = new SkipNode(newLevel, key, value);
                for (int i = 0; i < newLevel; i++)
                {
                    cursor.link[i]    = update[i].link[i];
                    update[i].link[i] = cursor;
                }
            }
        }