java skip list_SkipList跳跃表(Java实现)

packageskiplist_m;/******************************************************************************

* Skiplist *

* *

* View README file for information about this project. *

* View LICENSE file for license information. *

******************************************************************************/

importjava.util.Iterator;public class SkipList> extends AbstractSortedSet{private SkipListNodehead;private intmaxLevel;private intsize;private static final double PROBABILITY = 0.5;publicSkipList() {

size= 0;

maxLevel= 0;//a SkipListNode with value null marks the beginning

head = new SkipListNode(null);//null marks the end

head.nextNodes.add(null);

}publicSkipListNode getHead() {returnhead;

}//Adds e to the skiplist.//Returns false if already in skiplist, true otherwise.

public booleanadd(E e) {if(contains(e)) return false;

size++;//random number from 0 to maxLevel+1 (inclusive)

int level = 0;while (Math.random()

level++;while(level > maxLevel) { //should only happen once

head.nextNodes.add(null);

maxLevel++;

}

SkipListNode newNode= new SkipListNode(e);

SkipListNode current=head;do{

current=findNext(e,current,level);

newNode.nextNodes.add(0,current.nextNodes.get(level));

current.nextNodes.set(level,newNode);

}while (level-- > 0);return true;

}//Returns the skiplist node with greatest value <= e

privateSkipListNode find(E e) {returnfind(e,head,maxLevel);

}//Returns the skiplist node with greatest value <= e//Starts at node start and level

private SkipListNode find(E e, SkipListNode current, intlevel) {do{

current=findNext(e,current,level);

}while(level-- > 0);returncurrent;

}//Returns the node at a given level with highest value less than e

private SkipListNode findNext(E e, SkipListNode current, intlevel) {

SkipListNode next=(SkipListNode)current.nextNodes.get(level);while(next != null) {

E value=(E)next.getValue();if(lessThan(e,value)) //e < value

break;

current=next;

next=(SkipListNode)current.nextNodes.get(level);

}returncurrent;

}public intsize() {returnsize;

}public booleancontains(Object o) {

E e=(E)o;

SkipListNode node=find(e);return node != null &&node.getValue()!= null &&equalTo((E)node.getValue(),e);

}public Iteratoriterator() {return new SkipListIterator(this);

}/******************************************************************************

* Utility Functions *

******************************************************************************/

private booleanlessThan(E a, E b) {return a.compareTo(b) < 0;

}private booleanequalTo(E a, E b) {return a.compareTo(b) == 0;

}private booleangreaterThan(E a, E b) {return a.compareTo(b) > 0;

}/******************************************************************************

* Testing *

******************************************************************************/

public static voidmain(String[] args) {

SkipList testList= new SkipList();

System.out.println(testList);

testList.add(4);

System.out.println(testList);

testList.add(1);

System.out.println(testList);

testList.add(2);

System.out.println(testList);

testList= new SkipList();

System.out.println(testList);

testList.add("hello");

System.out.println(testList);

testList.add("beautiful");

System.out.println(testList);

testList.add("world");

System.out.println(testList);

}publicString toString() {

String s= "SkipList: ";for(Object o : this)

s+= o + ", ";return s.substring(0,s.length()-2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值