版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、B+樹的實(shí)現(xiàn)算法(C+版)B+樹可以看作是B樹的變形,對(duì)于存放在外存 貯器上的字典,B+樹比B樹更為常用。一個(gè)m階的B+樹滿足下列條件:(1) 每個(gè)結(jié)點(diǎn)至多有m棵子樹。(2) 除根結(jié)點(diǎn)外,其它每個(gè)分支至少有m/2棵子樹。(3) 非葉結(jié)點(diǎn)的根結(jié)點(diǎn)至少有兩棵子樹。(4) 有n棵子樹的結(jié)點(diǎn)有n個(gè)關(guān)鍵碼,葉結(jié) 點(diǎn)中至少包含n/2個(gè)關(guān)鍵碼。(5) 葉結(jié)點(diǎn)都在同一層中,其中存放數(shù)據(jù)文件中記錄的關(guān)鍵碼及指向該記錄的指針,或存放數(shù)據(jù)文件分塊后每塊的最大關(guān)鍵碼及指向該塊的指針。葉結(jié)點(diǎn)按關(guān)鍵碼值大小順序鏈接。 可以 把每個(gè)葉結(jié)點(diǎn)看成是一個(gè)基本索引塊(直接指向 數(shù)據(jù)文件中的記錄)。(6) 所有分支結(jié)點(diǎn)可看成是索引的
2、索引。使 結(jié)點(diǎn)中僅包含它的各個(gè)子結(jié)點(diǎn)中最大(或最小)關(guān) 鍵碼的分界值及指向子結(jié)點(diǎn)的指針。/* test.cpp* Created on: 2012-10-10* Author: charle*/typedef char* CHARPTR;#in clude #in clude using n amespace std;#defi ne MAX_CNT 5#defi ne MIN_CNT 2 typedef struct BTreeNode/最大key個(gè)數(shù)為5,最小為2int key5;關(guān)鍵字?jǐn)?shù)組int nodetype;/節(jié)點(diǎn)類型:0-根,1-內(nèi)部節(jié)點(diǎn),2-外節(jié)點(diǎn)bool isleaf;/是否
3、為葉節(jié)點(diǎn)int nsize;/關(guān)鍵字個(gè)數(shù);BTreeNode* succeedi ngno de6;BTreeNode* pare ntno de;_BTreeNode;_BTreeNode* Create_BTree(i nt key)_BTreeNode *roo t;root = new _BTreeNode(); root-isleaf = 0;for(int i = 0;isucceedi ngno dei = NULL; root-pare ntnode = NULL; root- n size = 0;In sert_BTree(root,key);return root;int
4、 middleNode(_BTreeNode* p,i nt key) int ent;int c = p-n size;int tmpc + 1;bool flag = false;int j = c;for(i nt i = c-1;i=0 & j=0;i-)if(p-keyikey)tmpj- = p-keyi;else if(p-keyikeyi; flag = true;elsetmpj- = p-keyi;return tmp(c+1)/2;_BTreeNode* createNode()_BTreeNode* p = new _BTreeNode(); return p;*查詢功
5、能*/*root,i nt_BTreeNode *selectKey(_BTreeNode key)_BTreeNode *p = roo t;_BTreeNode *result = NULL;int i;if(p-isleaf)int c = p-n size;for(i = 0;ikeyi = key) result = p-succeedi ngno dei; break;elseint c = p-n size;for(i = 0;ikeyi = key)resultselectKey(p-succeedi ngno dei + 1,key); break;else if(p-key
6、i key)resultselectKey(p-succeedi ngno dei,key);break;retur n result;* 向下 INSERT*/start no de,i ntbool In sert_BTree(_BTreeNode* key)_BTreeNode* p;bool state = false;if(start node = NULL)state = false;return state;_BTreeNode* p = start node; int c = p-n size;int i;if(p-isleaf) 葉子節(jié)點(diǎn)節(jié)點(diǎn)未滿,則插入if(c = 0; i
7、-)if(p-keyi key) p-keyi+1 = p-keyi;elsebreak;p-keyi = key;p-n size +;state = true;節(jié)點(diǎn)滿,則分裂節(jié)點(diǎn)elsesplitNode(p,key);else /內(nèi)節(jié)點(diǎn)找到key的查找分支for(i=0;ikeyi key) break;順著分支執(zhí)行插入if(i succeedi ngno dei,key);elsestateIn sert_BTree(p-succeedi ngno dec,key); return state;/* 向上 INSERT*/bool In sert_BTree2(_BTreeNode*
8、start no de,i nt key,_BTreeNode*old no de,_BTreeNode*newno de)_BTreeNode* p = start no de;bool state = false;int i;if(p-no detype = 0 | p-no detype = 1)int c = p-n size;if(p-n size =0;i-)if(p-keyi key)p-keyi+1 = p-keyi;else break;p-keyi = key;p-succeedi ngno dei = old no de; p-succeedi ngno dei+1 =
9、newno de; p-n size += 1;state = true;elsefor(i = 0;ikeyi key) break;state = split In terNode(p, newno de,i,key);return state;bool splitNode(_BTreeNode *p,int key)bool state = false;if(p != NULL)if(p-isleaf)int c = p-n size;int i;for(i = 0;ikeyi=key) break;int mid = (c + 1)/2;/構(gòu)建新的子樹節(jié)點(diǎn)_BTreeNode* nod
10、e = createNode();if(i =0;j-)if(p-keyjkey)p-keyj+1 = p-keyj;elsebreak; p-keyj = key;/暫不考慮關(guān)鍵字對(duì)應(yīng)的記錄的存放 位置。將剩余的節(jié)點(diǎn)復(fù)制到新節(jié)點(diǎn)中。for(j = mid - 1;jkeyj-mid = p-keyj; p-keyj = 0;no de-n size = c-mid+1;no de-no detype = p-no detype;no de-isleaf = p-isleaf;int midKey = middleNode(p,key);if(p-pare ntnode = NULL)_BTr
11、eeNode*pare ntNode=createNode();pare ntNode-key0 = midKey;pare ntNode-isleaf = false;parentNode-nodetype = 0;/ 根節(jié)點(diǎn)pare ntNode-n size = 1;pare ntNode-pare ntnode = NULL;p-pare ntnode = no de-pare ntnode =pare ntNode;pare ntNode-succeedi ngno de0=p;pare ntNode-succeedi ngno de1=no de;elseno de-pare nt
12、node=p-pare ntno de;In sert_BTree2(p-pare ntno de,midKey,p, node);else/插 入右子樹int j,pos;pos = 0;bool flag = false;/ 是否添加 for(j = mid;jkeyjvkey)no de-keypos+ = p-keyj;else if(p-keyj=key & flag =false)no de-keypos+ = key;no de-keypos+ = p-keyj; flag = true;elseno de-keypos+ = p-keyj; p-keyj = 0;no de-n
13、 size = c-mid+1;no de-pare ntnode = p-pare ntno de; no de-no detype = p-no detype;no de-isleaf = p-isleaf;int midKey = middleNode(p,key); if(p-pare ntnode = NULL)_BTreeNode*pare ntNodecreateNode();pare ntNode-keyO = midKey;pare ntNode-isleaf = false;parentNode-nodetype = 0;/ 根節(jié)點(diǎn)pare ntNode-n size =
14、1;pare ntNode-pare ntnode = NULL;p-pare ntnode = no de-pare ntnode = pare ntNode;pare ntNode-succeedi ngno de0 = p;pare ntNode-succeedi ngno de1=no de;elseno de-pare ntnode = p-pare ntno de;In sert_BTree2(p-pare ntno de,midKey,p, node );state = true;return state;*分裂內(nèi)部節(jié)點(diǎn)*/boolsplit In terNode(_BTreeN
15、ode*in ternode,_BTreeNode*succeedi ngno de,i ntsplitpos,i nt key)bool state = false;_BTreeNode *p = interno de;if(p != NULL)存在父節(jié)點(diǎn)_BTreeNode *node = new _BTreeNode();int c = p-n size;int i;for(i = 0;ikeyi=key)break;構(gòu)建新的子樹節(jié)點(diǎn)_BTreeNode* node = createNode(); if(splitpos0 & splitposvc)no de-succeedi ngno
16、 de0 succeedi ngno de;for(i=splitpos;ikeyi-splitpos = p-keyi;no de-succeedi ngno dei-splitpos+1 = p-succeedi ngno dei+1;p-keyi=p-succeedi ngno dei+1=0;no de-n size = c-splitpos;no de-isleaf = p-isleaf;no de-no detype = p-no detype;no de-pare ntnode = p-pare ntno de;p-n size = splitpos;if(p-no detype
17、 = 0)_BTreeNode*n ewroot= new_BTreeNode();n ewroot-key0 = key;n ewroot- n size = 1;n ewroot- no detype = 0;n ewroot-isleaf = false;n ewroot-succeedi ngno de0 = p;n ewroot-succeedi ngno de1 = no de; p-no detype = no de-no detype = 1; p-pare ntnode = no de-pare ntnode = n ewroo t;state = true;elsestat
18、e=In sert_BTree2(p-pare ntno de,key,p, no de);else if(splitpos = 0)int tmp = key;key = p-key0;no de-key0 = tmp;no de-succeedi ngno de0=p-succeedi ngno de0;no de-succeedi ngno de1=succeedi ngno de;no de-n size = 1;no de-isleaf = p-isleaf;no de-no detype = p-no detype;no de-pare ntnode = p-pare ntno d
19、e;int c = p-n size;int i;for(i=0;ikeyi = p-keyi+1;p-succeedi ngno dei=p-succeedi ngno dei+1;p-succeedi ngno dec-1=p-succeedi ngno dec;p-n size = c-1;if(p-no detype = 0)_BTreeNode*n ewroot= new_BTreeNode();n ewroot-key0 = key;n ewroot- n size = 1;n ewroot- no detype = 0;n ewroot-isleaf = false;n ewro
20、ot-succeedi ngno de0 = no de;n ewroot-succeedi ngno de1 = p;p-no detype = no de-no detype = 1;p-pare ntnode = no de-pare ntnode = n ewroo t;state = true;elsestate=In sert_BTree2(p-pare ntno de,key, no de,p);else if(splitpos = c)int tmp = key;key = p-keyc-1;no de-key0 = tmp;no de-succeedi ngno de0=p-
21、succeedi ngno dec;no de-succeedi ngno de1=succeedi ngno de;no de-n size = 1;no de-isleaf = p-isleaf;no de-no detype = p-no detype;no de-pare ntnode = p-pare ntno de; int c = p-n size;int i;p-keyc-1 = 0;p-succeedi ngno dec = 0;p-n size = c-1;if(p-no detype = 0)_BTreeNode*n ewroot= new_BTreeNode();n e
22、wroot-key0 = key;n ewroot- n size = 1;n ewroot- no detype = 0;n ewroot-isleaf = false;n ewroot-succeedi ngno de0 = p;n ewroot-succeedi ngno de1 = no de; p-no detype = no de-no detype = 1; p-pare ntnode = no de-pare ntnode = n ewroo t;state = true; elsestateIn sert_BTree2(p-pare ntno de,key,p, no de)
23、;return state;/*刪除節(jié)點(diǎn)*刪除節(jié)點(diǎn)N后,如果N節(jié)點(diǎn)的個(gè)數(shù)少于最少 值,先從左右兄弟借,然后再合并;*當(dāng)左兄弟(或右兄弟)的鍵個(gè)數(shù)大于最小值, 且N節(jié)點(diǎn)個(gè)數(shù)小于最小值時(shí),從左兄弟(或右 兄弟)借一個(gè)給N*當(dāng)無(wú)處可借時(shí),則與左兄弟(或右兄弟)合 并,合并后父節(jié)點(diǎn)需調(diào)整。*/bool Delete_Node(_BTreeNode *node,int key)bool state = false;_BTreeNode *p = node;if(p-isleaf)node 為葉子節(jié)點(diǎn)int c = p-n size;int spos,epos,i;spos = epos = -1;for
24、(i = 0;ikeyi = key & spos = -1)spos = i;else if(p-keyi key & epos = -1) epos = i;break;if(spos = -1)/ 沒找到 key-sods sod HI z_SUAd宀三 + sod 0)POU6P 00nsAd = + Sodsa)pou6u-P 00nsAd 三 + sodM雯八d H = + sodsM天d(+v=sode H 一)04-sods H z_SUAd宀oH =0)POU6U-P 00nsAd oH 曰 Ad (+ovsods 上)04A雯 呢韓皿囁蕪sods W/(T hh soda)
25、七es_e 宀 (D3S una) (Ds-國(guó) H 42sstate = checkJoin(p);檢查是否要合并else/node為非葉子節(jié)點(diǎn)int c = p-n size;int i;/bool flag = false;找到第一個(gè)大于等于key的鍵,然后順著 向下找for(i = 0;ikeyi = key)Delete_Node(p-succeedi ngno dei+1,key); break;else if(p-keyi key)Delete_Node(p-succeedi ngno dei,key); break;if(i = c)state = false;return st
26、ate;state = true;return state;/*檢查節(jié)點(diǎn)是否需要合并*/bool checkJ oin (_BTreeNode *no de)bool state = false;_BTreeNode *p = no de;尋找左_BTreeNode *left = fin dBrother(p,1); 兄弟_BTreeNode *right = findBrother(p,2); 尋找 右兄弟if(p- nsize = MIN_CNT)return false;elseif(p-n sizeMIN_CNT)/p節(jié)點(diǎn)的鍵數(shù)小于最小節(jié)點(diǎn)數(shù),且左兄弟鍵 數(shù)大于最小節(jié)點(diǎn)數(shù),則借其左兄
27、弟最后一個(gè)鍵及 節(jié)點(diǎn)指針int i;int c = p-n size;for(i = c-1;i=0;i-)p-keyi+1 = p-keyi;p-succeedi ngno dei+2=p-succeedi ngno dei+1;p-succeedi ngno de1=p-succeedi ngno de0;p-key0 = left-keyleft-n size - 1;p-succeedi ngno de0=Ieft-succeedi ngno deleft- n size; left-keyleft- nsize-1 = 0; left-succeedi ngno deleft- n
28、size = NULL; left- n size-;p-n size+;int pos = fin dPos(p);if(pos = -1)return false;_BTreeNode *fatherNode = p-pare ntnode; 如果是葉子節(jié)點(diǎn),貝I將父節(jié)點(diǎn)對(duì)應(yīng)鍵換為 該節(jié)點(diǎn)的最小鍵if(p-isleaf) fatherNode-keypos =p-key0;如果非葉節(jié)點(diǎn),貝U將父節(jié)點(diǎn)對(duì)應(yīng)鍵與該節(jié) 點(diǎn)的最小鍵對(duì)換elseint tmp = fatherNode-keypos; fatherNode-keypos = p-key0; p-key0 = tmp;elseif(p-
29、n sizen size MIN_CNT)p節(jié)點(diǎn)的鍵數(shù)小于最小節(jié)點(diǎn)數(shù),且右兄弟鍵 數(shù)大于最小節(jié)點(diǎn)數(shù),則借其右兄弟第一個(gè)鍵及節(jié) 點(diǎn)指針int i;int c = p-n size;p-keyc = right-key0;p-succeedi ngno dec+1 left-succeedi ngno de0;for(i=0;in size;i+)left-keyi = left-keyi+1; left-succeedi ngno deiIeft-succeedi ngno dei+1;left-keyleft- nsize-1 = 0;left-succeedi ngno deleft- n
30、size = NULL; left- n size-;p-n size+;int pos = fin dPos(right);if(pos = -1)return false;_BTreeNode *fatherNode = p-pare ntnode;如果是葉子節(jié)點(diǎn),則將父節(jié)點(diǎn)中右兄弟對(duì) 應(yīng)鍵換為其右兄弟的最小鍵if(p-isleaf) fatherNode-keypos = right-keyO;如果非葉節(jié)點(diǎn),則將父節(jié)點(diǎn)中右兄弟對(duì)應(yīng) 鍵與其右兄弟的最小鍵對(duì)換elseint tmp = fatherNode-keypos; fatherNode-keypos = right-keyO; ri
31、ght-keyO = tmp;else if(p- nsize MIN_CNT & left-nsize isleaf)int c = p-n size;for(i = 0;ikeyleft- n size + i = p-keyi;left-succeedi ngno deleft-n size + i= p-succeedi ngno dei;left-n size += p-n size;int pos = findPos(left);statedeletePare ntNode(p-pare ntno de,pos);state &= deleteNode(p);state &= ch
32、eckJ oin (left-pare ntno de);elseint c = p-n size;left-keyleft- n size=p-succeedi ngno de0-key0;for(i = 0;ikeyleft- n size + i +1=p-keyi;left-succeedi ngno deleft-n size + i +1 = p-succeedi ngno dei;left-succeedi ngno deleft- n size + c + 1 =p-succeedi ngno dec;left-n size += (c + 1);state &= deleteNode(p);state &= checkJ oin (left-pare ntno de);else if(p- nsize n sizeisleaf)int c = right-n size;for(i = O;ikeyleft-n size + i = right-keyi;p-succeedi ngno deleft-n size + i= right-succeedi ngno dei;p-n size += right- n size;int pos = fin dPos(p);state=deletePare ntNode(p-pa
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 福建師范大學(xué)《管理學(xué)概論》2021-2022學(xué)年第一學(xué)期期末試卷
- 福建師范大學(xué)《地圖學(xué)實(shí)驗(yàn)》2021-2022學(xué)年第一學(xué)期期末試卷
- 四川省成都市2020年中考英語(yǔ)真題(含答案)
- 2024秋國(guó)家開放大學(xué)《思想道德與法制》專題測(cè)驗(yàn)1-7答案
- 油田制度流程類 油田動(dòng)火作業(yè)安全管理實(shí)施細(xì)則(陸上石油天然氣開采)
- 班級(jí)常規(guī)調(diào)研活動(dòng)分析
- 檔案行政執(zhí)法人員統(tǒng)計(jì)表
- 感恩教育課件教學(xué)課件
- 2024年四川客運(yùn)從業(yè)資格證實(shí)操考試需要準(zhǔn)備什么
- 2024年合肥客車駕駛員考試試題
- 新《固廢法》解讀(專業(yè)版)
- 領(lǐng)導(dǎo)及上下級(jí)關(guān)系處理講義
- Catia百格線生成宏
- 業(yè)務(wù)流程繪制方法IDEF和IDEFPPT課件
- 鍋爐安全基礎(chǔ)知識(shí)
- 幼兒園科學(xué)教育論文范文
- 駕校質(zhì)量信譽(yù)考核制度
- 用電檢查工作流程圖
- 電動(dòng)葫蘆的設(shè)計(jì)計(jì)算電動(dòng)起重機(jī)械畢業(yè)設(shè)計(jì)論文
- (完整版)學(xué)校安辦主任安全工作職責(zé)
- PCR儀使用手冊(cè)
評(píng)論
0/150
提交評(píng)論