- packagecom.eshore.sweetop.dataframe;
- importcom.eshore.sweetop.data.KeyData;
- /*
- *开放寻址法解决碰撞问题
- */
- publicclassOpenHash{
- protectedKeyData[]table;
- publicOpenHash(intsize){
- table=newKeyData[size];
- }
- publicvoidinsert(KeyDatakd){
- intj=0,k=kd.getKey();
- for(inti=0;i<table.length;i++){
- j=hash(k,i);
- if(table[j]==null){
- table[j]=kd;
- return;
- }
- }
- thrownewRuntimeException("hashtableoverflow!");
- }
- publicKeyDatasearch(intk){
- intj=0;
- for(inti=0;i<table.length;i++){
- j=hash(k,i);
- if(table[j].getKey()==k){
- returntable[j];
- }
- }
- returnnull;
- }
- /*
- *当然不能这么写,这里重点是开放寻址法,关于探查序列,后面有续
- */
- publicinthash(intk,inti){
- returni;
- }
- publicstaticvoidmain(String[]args){
- OpenHashoh=newOpenHash(10);
- oh.insert(newKeyData(1));
- oh.insert(newKeyData(2));
- oh.insert(newKeyData(3));
- oh.insert(newKeyData(14));
- KeyDatak=oh.search(2);
- System.out.println(k);
- }
- }
算法-----开放寻址法
最新推荐文章于 2024-05-15 16:07:26 发布