




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Skip list層次的選擇:插入列的“高度”較前者來說顯得更加重要,也更加難以確定。由于它的不確定性,使得不 同的決策可能會(huì)導(dǎo)致截然不同的算法效率。為了使插入數(shù)據(jù)之后,保持該數(shù)據(jù)結(jié)構(gòu)進(jìn)行各種 操作均為O(logn)復(fù)雜度的性質(zhì),我們引入隨機(jī)化算法(Randomized Algorithms)。我們定義一個(gè)隨機(jī)決策模塊,它的大致內(nèi)容如下:產(chǎn)生一個(gè)0到1的隨機(jī)數(shù)rr random()如果r小于一個(gè)常數(shù)p,則執(zhí)行方案A,if rp then do A否則,執(zhí)行方案Belse do B初始時(shí)列高為1。插入元素時(shí),不停地執(zhí)行隨機(jī)決策模塊。如果要求執(zhí)行的題 操作,則將 列的高度加1,并且繼續(xù)反復(fù)執(zhí)行隨機(jī)
2、決策模塊。直到第i次,模塊要求執(zhí)行的是B操作, 我們結(jié)束決策,并向跳躍表中插入一個(gè)高度為i的列。性質(zhì)1:根據(jù)上述決策方法,該列的高度大于等于k的概率為plk-1)。此處有一個(gè)地方需要注意,如果得到的i比當(dāng)前跳躍表的高度h還要大的話,則需要增加新 的鏈,使得跳躍表仍滿足先前所提到的條件。randomLevel()Ivl := IrandofnQ that returns a random value in 0.1)while randonfi() p and llvl MaxLevel doIvl :二 M + 1return Ivl自:數(shù)據(jù)結(jié)構(gòu)與算法public void choosePowe
3、rs()powersmaxLevel-1=(2=0;i-,j+)powersi=powersi+1-(2j);public int chooseLevel()int i,r=Math.abs(rd.nextInt()%powersmaxLevel-1+1;for(i=1;imaxLevel;i+)if(rpowersi)return i-1;return i-1;假設(shè)最高層次maxLevel=4.則15個(gè)元素,在第一層需要8個(gè)節(jié)點(diǎn),第二層需要4個(gè),第三層需 要2個(gè),第四層需要1個(gè)。每插入一個(gè)節(jié)點(diǎn)時(shí),就生成一個(gè)1到15之間的隨機(jī)數(shù)r(r=Math. abs(rd .nextInt()%power
4、s maxLevel -1+1;),如果 r9 則節(jié)點(diǎn)在第一層,如果 r13,那么節(jié)點(diǎn)插在第二層,如果rtieaderfor i := list-level down to 1 dowhile xforwardi-key forwardi- searchKjsy forward lif XTKey = search Key then x- value := newValjeelseIvl := randomLevelOif Ivl list-level thenfor i := list-level + 1 to M doupdatei := list-lieaderlistlevel:二 I
5、vlx := make Node(Ivl, search Keyr value)for i := 1 to level doxforwardi := updatei-orwardiupchteiTforwandi := xDeletefl ist: search Key)local update1 MaxLevelx :二 list-lieaderfor i := list-level down to 1 dowhile x-forwardi-key forwardiupdate i - xx := x*fbrward lit XTKey = search Key thenfor i := 1
6、 to list-level doiff updatei-forwardi # x then breakupdatep-*fbrwardi := x-*fonvandiIriee(x)while listlevel 1 andlist-header-forwardlist-level| = NIL dolist-level := list-level - 1Java實(shí)現(xiàn):class SkipNodeE extends Comparable public final E value;public final SkipNode forward; / array of pointersSuppres
7、sWarnings(unchecked)public SkipNode(int level, E value)forward = new SkipNodelevel + 1;this.value = value;public class SkipSetE extends Comparablepublic static final double P = 0.5;public static final int MAX_LEVEL = 6;public static int randomLevel() int lvl = (int)(Math.log(1.-Math.random()/Math.lo
8、g(1.-P); return Math.min(lvl, MAX_LEVEL);public final SkipNode header = new SkipNode(MAX_LEVEL, null);public int level = 0;public String toString()StringBuilder sb = new StringBuilder();sb.append );SkipNode x =header.forward0;while (x != null) sb.append(xvalue);x = xforward0;if (x != null)sb.append,
9、);sb.append );return sb.toString();public boolean contains(E searchvalue) SkipNode x = header;for (int i = level; i = 0; i-) while (x.forwardi != null & x.forwardi.pareTo(searchValue) 0) x = x.forwardi; x = x.forward0;return x != null & x.value.equals(searchValue); SuppressWarnings(unchecked) public
10、 void insert(E value) SkipNode x = header; SkipNode update = new SkipNodeMAX_LEVEL + 1;for (int i = level; i = 0; i-) while (x.forwardi != null & x.forwardi.pareTo(value) level) for (int i = level + 1; i = lvl; i+) updatei =header; level = lvl;x =new SkipNode(lvl, value); for (int i = 0; i = lvl; i+
11、) x.forwardi = updatei.forwardi; updatei .forwardi = x; SuppressWarnings(unchecked)public void delete(E value) SkipNode x = header;SkipNode update = new SkipNodeMAX_LEVEL + 1;for (int i = level; i = 0; i-) while (x.forwardi != null & x.forwardi.pareTo(value) 0) x = x.forwardi;updatei = x;x = x.forwa
12、rd0;if (x.value.equals(value) for (int i = 0; i 0 & header.forwardlevel = null) level-; public static void main(String args) SkipSet ss = new SkipSet();System out.println(ss);ss.insert(5);ss.insert(10);ss.insert(7);ss.insert(7);ss.insert(6);if (ss.contains(7) Systemout.println(7 is in the list); System out.pr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 關(guān)于《昆蟲記》各地中考考了這些題
- 高新技術(shù)產(chǎn)業(yè)在減少環(huán)境污染中的關(guān)鍵作用分析報(bào)告
- 高效行政管理與企業(yè)文化塑造的協(xié)同發(fā)展
- 高效財(cái)務(wù)規(guī)劃助力企業(yè)發(fā)展
- 購物中心的品牌價(jià)值與其服務(wù)質(zhì)量的關(guān)系
- 高中語文情感美文當(dāng)你寂寞時(shí)我會(huì)陪伴你
- 購物車維護(hù)與保養(yǎng)知識(shí)
- 高中語文情感美文繼續(xù)戀愛
- 跨國公司合資中的風(fēng)險(xiǎn)評(píng)估與應(yīng)對(duì)
- 高科技在葡萄酒標(biāo)簽制作中的應(yīng)用
- 大型養(yǎng)路機(jī)械司機(jī)(打磨車)高級(jí)工技能鑒定考試題庫(含答案)
- 車輛使用不過戶免責(zé)協(xié)議書范文范本
- 蟾蜍毒抗病毒藥物篩選
- DB11T 2033-2022 餐廚垃圾源頭減量操作要求
- 1.2 歌曲 《春天來了》 課件(11張)
- 【人教版】pep六年級(jí)英語下全冊(cè)教案(表格版)
- 護(hù)理培訓(xùn)師競(jìng)聘
- 北師大版小學(xué)數(shù)學(xué)五年級(jí)下冊(cè)同步課時(shí)練習(xí)試題含答案(全冊(cè))
- 4《我們的公共生活》第一課時(shí) 教學(xué)設(shè)計(jì)-2023-2024學(xué)年道德與法治五年級(jí)下冊(cè)統(tǒng)編版
- 2023-2024學(xué)年粵教版(2019)高中信息技術(shù)必修一《數(shù)據(jù)與計(jì)算》第一章第一節(jié)《數(shù)據(jù)及其特征》教案
- GB/T 23862-2024文物包裝與運(yùn)輸規(guī)范
評(píng)論
0/150
提交評(píng)論