從B樹(shù)談到R樹(shù)之B樹(shù)的C實(shí)現
作者:weedge,July。編程藝術(shù)室出品。
前言
代碼大全的作者Steve McConnell曾稱(chēng),他所見(jiàn)識的任何一本書(shū)都不是某一個(gè)人能完全獨立即能完成的。吾深以為然。
本blog內的文章十有八九系我個(gè)人參考資料原創(chuàng )所作,與此同時(shí)十有二三系本人與吾的朋友共同創(chuàng )作完成。所以,諸君在瀏覽本博客內任何一篇文章時(shí),務(wù)必尊重他人勞動(dòng)成果。當然,有任何問(wèn)題,歡迎隨時(shí)不吝指正。
ok,在本blog之前的一篇文章中:從B樹(shù)、B+樹(shù)、B*樹(shù)談到R 樹(shù),各位讀者反應熱烈。這次,咱們來(lái)編碼實(shí)現B樹(shù)的查找,插入,刪除等操作。同時(shí)此文也算作是上一篇文章從B樹(shù)談到R樹(shù)的續。望諸君不吝賜教。謝謝。
第一部分、B樹(shù)的查找,插入,刪除等具體操作
編碼實(shí)現B樹(shù)之前,咱們先來(lái)回顧一下上文中所給出的B樹(shù)的查找,插入,刪除等具體的操作都是怎么一回事兒。明白了原理之后,再來(lái)編程實(shí)現,就相對來(lái)說(shuō)有方向感了。ok,請看下文(援引自從B樹(shù)、B+樹(shù)、B*樹(shù)談到R 樹(shù)第3小節):
B樹(shù)的插入、刪除操作
上文第3小節簡(jiǎn)單介紹了利用B樹(shù)這種結構如何訪(fǎng)問(wèn)外存磁盤(pán)中的數據的情況,下面咱們通過(guò)另外一個(gè)實(shí)例來(lái)對這棵B樹(shù)的插入(insert),刪除(delete)基本操作進(jìn)行詳細的介紹。
但在此之前,咱們還得簡(jiǎn)單回顧下一棵m階的B 樹(shù) (m叉樹(shù))的特性,如下:
樹(shù)中每個(gè)結點(diǎn)含有最多含有m個(gè)孩子,即m滿(mǎn)足:ceil(m/2)<=m<=m。
除根結點(diǎn)和葉子結點(diǎn)外,其它每個(gè)結點(diǎn)至少有[ceil(m / 2)]個(gè)孩子(其中ceil(x)是一個(gè)取上限的函數);
若根結點(diǎn)不是葉子結點(diǎn),則至少有2個(gè)孩子(特殊情況:沒(méi)有孩子的根結點(diǎn),即根結點(diǎn)為葉子結點(diǎn),整棵樹(shù)只有一個(gè)根節點(diǎn));
所有葉子結點(diǎn)都出現在同一層,葉子結點(diǎn)不包含任何關(guān)鍵字信息(可以看做是外部接點(diǎn)或查詢(xún)失敗的接點(diǎn),實(shí)際上這些結點(diǎn)不存在,指向這些結點(diǎn)的指針都為null);
每個(gè)非終端結點(diǎn)中包含有n個(gè)關(guān)鍵字信息: (n,P0,K1,P1,K2,P2,......,Kn,Pn)。其中:
a) Ki (i=1...n)為關(guān)鍵字,且關(guān)鍵字按順序升序排序K(i-1)< Ki。
b) Pi為指向子樹(shù)根的接點(diǎn),且指針P(i-1)指向子樹(shù)種所有結點(diǎn)的關(guān)鍵字均小于Ki,但都大于K(i-1)。
c) 除根結點(diǎn)之外的結點(diǎn)的關(guān)鍵字的個(gè)數n必須滿(mǎn)足: [ceil(m / 2)-1]<= n <= m-1(葉子結點(diǎn)也必須滿(mǎn)足此條關(guān)于關(guān)鍵字數的性質(zhì),根結點(diǎn)除外)。
ok,下面咱們以一棵5階(m=5,即除根結點(diǎn)和葉子結點(diǎn)之外的內結點(diǎn)最多5個(gè)孩子,最少3個(gè)孩子)B樹(shù)實(shí)例進(jìn)行講。
備注:
- 關(guān)鍵字數(2-4個(gè))針對--非根結點(diǎn)(包括葉子結點(diǎn)在內),孩子數(3-5個(gè))--針對根結點(diǎn)和葉子結點(diǎn)之外的內結點(diǎn)。當然,根結點(diǎn)是必須至少有2個(gè)孩子的,不然就成直線(xiàn)型搜索樹(shù)了。
- 我說(shuō)的再明白點(diǎn)就是,一棵5階的B樹(shù)中任何一個(gè)結點(diǎn)的關(guān)鍵字數是1-4,孩子樹(shù)是2-5。同時(shí),一棵5階的B樹(shù)的最大高度應為log_ceil(m/2)N(下劃線(xiàn)表示以ceil(m/2)為底)。
下圖中關(guān)鍵字為大寫(xiě)字母,順序為字母升序。
結點(diǎn)定義如下:
typedef struct{
int Count; // 當前節點(diǎn)中關(guān)鍵元素數目
ItemType Key[4]; // 存儲關(guān)鍵字元素的數組
long Branch[5]; // 偽指針數組,(記錄數目)方便判斷合并和分裂的情況
} NodeType;
1.1、插入(insert)操作
插入一個(gè)元素時(shí),首先在B樹(shù)中是否存在,如果不存在,即在葉子結點(diǎn)處結束,然后在葉子結點(diǎn)中插入該新的元素,注意:
- 如果葉子結點(diǎn)空間足夠,這里需要向右移動(dòng)該葉子結點(diǎn)中大于新插入關(guān)鍵字的元素,
- 如果空間滿(mǎn)了以致沒(méi)有足夠的空間去添加新的元素,則將該結點(diǎn)進(jìn)行“分裂”,將一半數量的關(guān)鍵字元素分裂到新的其相鄰右結點(diǎn)中,中間關(guān)鍵字元素上移到父結點(diǎn)中(當然,如果父結點(diǎn)空間滿(mǎn)了,也同樣需要“分裂”操作),而且當結點(diǎn)中關(guān)鍵元素向右移動(dòng)了,相關(guān)的指針也需要向右移。
- 如果在根結點(diǎn)插入新元素,空間滿(mǎn)了,則進(jìn)行分裂操作,這樣原來(lái)的根結點(diǎn)中的中間關(guān)鍵字元素向上移動(dòng)到新的根結點(diǎn)中,因此導致樹(shù)的高度增加一層。
1、咱們通過(guò)一個(gè)實(shí)例來(lái)逐步講解下。插入以下字符字母到一棵空的B 樹(shù)中(非根結點(diǎn)關(guān)鍵字數小了(小于2個(gè))就合并,大了(超過(guò)4個(gè))就分裂):C N G A H E K Q M F W L T Z D P R X Y S,首先,結點(diǎn)空間足夠,4個(gè)字母插入相同的結點(diǎn)中,如下圖:
2、當咱們試著(zhù)插入H時(shí),結點(diǎn)發(fā)現空間不夠,以致將其分裂成2個(gè)結點(diǎn),移動(dòng)中間元素G上移到新的根結點(diǎn)中,在實(shí)現過(guò)程中,咱們把A和C留在當前結點(diǎn)中,而H和N放置新的其右鄰居結點(diǎn)中。如下圖:
3、當咱們插入E,K,Q時(shí),不需要任何分裂操作
4、插入M需要一次分裂,注意M恰好是中間關(guān)鍵字元素,以致向上移到父節點(diǎn)中
5、插入F,W,L,T不需要任何分裂操作
6、插入Z時(shí),最右的葉子結點(diǎn)空間滿(mǎn)了,需要進(jìn)行分裂操作,中間元素T上移到父節點(diǎn)中,注意通過(guò)上移中間元素,樹(shù)最終還是保持平衡,分裂結果的結點(diǎn)存在2個(gè)關(guān)鍵字元素。
7、插入D時(shí),導致最左邊的葉子結點(diǎn)被分裂,D恰好也是中間元素,上移到父節點(diǎn)中,然后字母P,R,X,Y陸續插入不需要任何分裂操作(別忘了,樹(shù)中至多5個(gè)孩子)。
8、最后,當插入S時(shí),含有N,P,Q,R的結點(diǎn)需要分裂,把中間元素Q上移到父節點(diǎn)中,但是情況來(lái)了,父節點(diǎn)中空間已經(jīng)滿(mǎn)了,所以也要進(jìn)行分裂,將父節點(diǎn)中的中間元素M上移到新形成的根結點(diǎn)中,注意以前在父節點(diǎn)中的第三個(gè)指針在修改后包括D和G節點(diǎn)中。這樣具體插入操作的完成,下面介紹刪除操作,刪除操作相對于插入操作要考慮的情況多點(diǎn)。
1.2、刪除(delete)操作
首先查找B樹(shù)中需刪除的元素,如果該元素在B樹(shù)中存在,則將該元素在其結點(diǎn)中進(jìn)行刪除,如果刪除該元素后,首先判斷該元素是否有左右孩子結點(diǎn),如果有,則上移孩子結點(diǎn)中的某相近元素到父節點(diǎn)中,然后是移動(dòng)之后的情況;如果沒(méi)有,直接刪除后,移動(dòng)之后的情況。
刪除元素,移動(dòng)相應元素之后,
- 如果某結點(diǎn)中元素數目(即關(guān)鍵字數)小于ceil(m/2)-1,則需要看其某相鄰兄弟結點(diǎn)是否豐滿(mǎn)(結點(diǎn)中元素個(gè)數大于ceil(m/2)-1)(還記得第一節中關(guān)于B樹(shù)的第5個(gè)特性中的c點(diǎn)么?: c)除根結點(diǎn)之外的結點(diǎn)(包括葉子結點(diǎn))的關(guān)鍵字的個(gè)數n必須滿(mǎn)足: (ceil(m / 2)-1)<= n <= m-1。m表示最多含有m個(gè)孩子,n表示關(guān)鍵字數。在本小節中舉的一顆B樹(shù)的示例中,關(guān)鍵字數n滿(mǎn)足:2<=n<=4),
- 如果豐滿(mǎn),則向父節點(diǎn)借一個(gè)元素來(lái)滿(mǎn)足條件;
- 如果其相鄰兄弟都剛脫貧,即借了之后其結點(diǎn)數目小于ceil(m/2)-1,則該結點(diǎn)與其相鄰的某一兄弟結點(diǎn)進(jìn)行“合并”成一個(gè)結點(diǎn),以此來(lái)滿(mǎn)足條件。
那咱們通過(guò)下面實(shí)例來(lái)詳細了解吧。
以上述插入操作構造的一棵5階B樹(shù)(樹(shù)中最多含有m(m=5)個(gè)孩子,因此關(guān)鍵字數最小為ceil(m / 2)-1=2。還是這句話(huà),關(guān)鍵字數小了(小于2個(gè))就合并,大了(超過(guò)4個(gè))就分裂)為例,依次刪除H,T,R,E。
1、首先刪除元素H,當然首先查找H,H在一個(gè)葉子結點(diǎn)中,且該葉子結點(diǎn)元素數目3大于最小元素數目ceil(m/2)-1=2,則操作很簡(jiǎn)單,咱們只需要移動(dòng)K至原來(lái)H的位置,移動(dòng)L至K的位置(也就是結點(diǎn)中刪除元素后面的元素向前移動(dòng))
2、下一步,刪除T,因為T(mén)沒(méi)有在葉子結點(diǎn)中,而是在中間結點(diǎn)中找到,咱們發(fā)現他的繼承者W(字母升序的下個(gè)元素),將W上移到T的位置,然后將原包含W的孩子結點(diǎn)中的W進(jìn)行刪除,這里恰好刪除W后,該孩子結點(diǎn)中元素個(gè)數大于2,無(wú)需進(jìn)行合并操作。
3、下一步刪除R,R在葉子結點(diǎn)中,但是該結點(diǎn)中元素數目為2,刪除導致只有1個(gè)元素,已經(jīng)小于最小元素數目ceil(5/2)-1=2,而由前面我們已經(jīng)知道:如果其某個(gè)相鄰兄弟結點(diǎn)中比較豐滿(mǎn)(元素個(gè)數大于ceil(5/2)-1=2),則可以向父結點(diǎn)借一個(gè)元素,然后將最豐滿(mǎn)的相鄰兄弟結點(diǎn)中上移最后或最前一個(gè)元素到父節點(diǎn)中(有沒(méi)有看到紅黑樹(shù)中左旋操作的影子?),在這個(gè)實(shí)例中,右相鄰兄弟結點(diǎn)中比較豐滿(mǎn)(3個(gè)元素大于2),所以先向父節點(diǎn)借一個(gè)元素W下移到該葉子結點(diǎn)中,代替原來(lái)S的位置,S前移;然后X在相鄰右兄弟結點(diǎn)中上移到父結點(diǎn)中,最后在相鄰右兄弟結點(diǎn)中刪除X,后面元素前移。
4、最后一步刪除E, 刪除后會(huì )導致很多問(wèn)題,因為E所在的結點(diǎn)數目剛好達標,剛好滿(mǎn)足最小元素個(gè)數(ceil(5/2)-1=2),而相鄰的兄弟結點(diǎn)也是同樣的情況,刪除一個(gè)元素都不能滿(mǎn)足條件,所以需要該節點(diǎn)與某相鄰兄弟結點(diǎn)進(jìn)行合并操作;首先移動(dòng)父結點(diǎn)中的元素(該元素在兩個(gè)需要合并的兩個(gè)結點(diǎn)元素之間)下移到其子結點(diǎn)中,然后將這兩個(gè)結點(diǎn)進(jìn)行合并成一個(gè)結點(diǎn)。所以在該實(shí)例中,咱們首先將父節點(diǎn)中的元素D下移到已經(jīng)刪除E而只有F的結點(diǎn)中,然后將含有D和F的結點(diǎn)和含有A,C的相鄰兄弟結點(diǎn)進(jìn)行合并成一個(gè)結點(diǎn)。
5、也許你認為這樣刪除操作已經(jīng)結束了,其實(shí)不然,在看看上圖,對于這種特殊情況,你立即會(huì )發(fā)現父節點(diǎn)只包含一個(gè)元素G,沒(méi)達標(因為非根節點(diǎn)包括葉子結點(diǎn)的關(guān)鍵字數n必須滿(mǎn)足于2=<n<=4,而此處的n=1),這是不能夠接受的。如果這個(gè)問(wèn)題結點(diǎn)的相鄰兄弟比較豐滿(mǎn),則可以向父結點(diǎn)借一個(gè)元素。假設這時(shí)右兄弟結點(diǎn)(含有Q,X)有一個(gè)以上的元素(Q右邊還有元素),然后咱們將M下移到元素很少的子結點(diǎn)中,將Q上移到M的位置,這時(shí),Q的左子樹(shù)將變成M的右子樹(shù),也就是含有N,P結點(diǎn)被依附在M的右指針上。所以在這個(gè)實(shí)例中,咱們沒(méi)有辦法去借一個(gè)元素,只能與兄弟結點(diǎn)進(jìn)行合并成一個(gè)結點(diǎn),而根結點(diǎn)中的唯一元素M下移到子結點(diǎn),這樣,樹(shù)的高度減少一層。
為了進(jìn)一步詳細討論刪除的情況,再舉另外一個(gè)實(shí)例:
這里是一棵不同的5序B樹(shù),那咱們試著(zhù)刪除C
于是將刪除元素C的右子結點(diǎn)中的D元素上移到C的位置,但是出現上移元素后,只有一個(gè)元素的結點(diǎn)的情況。
又因為含有E的結點(diǎn),其相鄰兄弟結點(diǎn)才剛脫貧(最少元素個(gè)數為2),不可能向父節點(diǎn)借元素,所以只能進(jìn)行合并操作,于是這里將含有A,B的左兄弟結點(diǎn)和含有E的結點(diǎn)進(jìn)行合并成一個(gè)結點(diǎn)。
這樣又出現只含有一個(gè)元素F結點(diǎn)的情況,這時(shí),其相鄰的兄弟結點(diǎn)是豐滿(mǎn)的(元素個(gè)數為3>最小元素個(gè)數2),這樣就可以想父結點(diǎn)借元素了,把父結點(diǎn)中的J下移到該結點(diǎn)中,相應的如果結點(diǎn)中J后有元素則前移,然后相鄰兄弟結點(diǎn)中的第一個(gè)元素(或者最后一個(gè)元素)上移到父節點(diǎn)中,后面的元素(或者前面的元素)前移(或者后移);注意含有K,L的結點(diǎn)以前依附在M的左邊,現在變?yōu)橐栏皆贘的右邊。這樣每個(gè)結點(diǎn)都滿(mǎn)足B樹(shù)結構性質(zhì)。
從以上操作可看出:除根結點(diǎn)之外的結點(diǎn)(包括葉子結點(diǎn))的關(guān)鍵字的個(gè)數n滿(mǎn)足:(ceil(m / 2)-1)<= n <= m-1,即2<=n<=4。這也佐證了咱們之前的觀(guān)點(diǎn)。刪除操作完。
第二部分、B+ Tree
B+Tree
B-Tree有許多變種,其中最常見(jiàn)的是B+Tree,例如MySQL就普遍使用B+Tree實(shí)現其索引結構。與B-Tree相比,B+Tree有以下不同點(diǎn):
- 每個(gè)節點(diǎn)的指針上限為2d而不是2d+1。
- 內節點(diǎn)不存儲data,只存儲key;葉子節點(diǎn)不存儲指針。
圖3是一個(gè)簡(jiǎn)單的B+Tree示意。
圖1
由于并不是所有節點(diǎn)都具有相同的域,因此B+Tree中葉節點(diǎn)和內節點(diǎn)一般大小不同。這點(diǎn)與B-Tree不同,雖然B-Tree中不同節點(diǎn)存放的key和指針可能數量不一致,但是每個(gè)節點(diǎn)的域和上限是一致的,所以在實(shí)現中B-Tree往往對每個(gè)節點(diǎn)申請同等大小的空間。
一般來(lái)說(shuō),B+Tree比B-Tree更適合實(shí)現外存儲索引結構,具體原因與外存儲器原理及計算機存取原理有關(guān),在此不作具體討論。
帶有順序訪(fǎng)問(wèn)指針的B+Tree
一般在數據庫系統或文件系統中使用的B+Tree結構都在經(jīng)典B+Tree的基礎上進(jìn)行了優(yōu)化,增加了順序訪(fǎng)問(wèn)指針。
圖4
如上圖4所示,在B+Tree的每個(gè)葉子節點(diǎn)增加一個(gè)指向相鄰葉子節點(diǎn)的指針,就形成了帶有順序訪(fǎng)問(wèn)指針的B+Tree。做這個(gè)優(yōu)化的目的是為了提高區間訪(fǎng)問(wèn)的性能,例如圖4中如果要查詢(xún)key為從18到49的所有數據記錄,當找到18后,只需順著(zhù)節點(diǎn)和指針順序遍歷就可以一次性訪(fǎng)問(wèn)到所有數據節點(diǎn),極大提到了區間查詢(xún)效率。(此第二部分參考自:http://www.cnblogs.com/leoo2sk/archive/2011/07/10/mysql-index.html。)
第三部分、B樹(shù)的編碼實(shí)現
既然明白了B樹(shù)的插入,和刪除操作的原理,接下來(lái),咱們來(lái)一步一步實(shí)現它。不過(guò),有一點(diǎn)必須說(shuō)明的是:這個(gè)實(shí)現只是實(shí)現了偶數序order(階)的情況;還有奇數序order(階)的情況沒(méi)有考慮。待日后改進(jìn)。
- //實(shí)現對order序(階)的B-TREE結構基本操作的封裝。
- //查找:search,插入:insert,刪除:remove。
- //創(chuàng )建:create,銷(xiāo)毀:destory,打?。簆rint。
- #ifndef BTREE_H
- #define BTREE_H
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- ////* 定義m序(階)B 樹(shù)的最小度數BTree_D=ceil(m)*/
- /// 在這里定義每個(gè)節點(diǎn)中關(guān)鍵字的最大數目為:2 * BTree_D - 1,即序(階):2 * BTree_D.
- #define BTree_D 2
- #define ORDER (BTree_D * 2) //定義為4階B-tree,2-3-4樹(shù)。最簡(jiǎn)單為3階B-tree,2-3樹(shù)。
- //#define ORDER (BTree_T * 2-1) //最簡(jiǎn)單為3階B-tree,2-3樹(shù)。
-
- typedef int KeyType;
- typedef struct BTNode{
- int keynum; /// 結點(diǎn)中關(guān)鍵字的個(gè)數,keynum <= BTree_N
- KeyType key[ORDER-1]; /// 關(guān)鍵字向量為key[0..keynum - 1]
- struct BTNode* child[ORDER]; /// 孩子指針向量為child[0..keynum]
- bool isLeaf; /// 是否是葉子節點(diǎn)的標志
- }BTNode;
-
- typedef BTNode* BTree; ///定義BTree
-
- ///給定數據集data,創(chuàng )建BTree。
- void BTree_create(BTree* tree, const KeyType* data, int length);
-
- ///銷(xiāo)毀BTree,釋放內存空間。
- void BTree_destroy(BTree* tree);
-
- ///在BTree中插入關(guān)鍵字key。
- void BTree_insert(BTree* tree, KeyType key);
-
- ///在BTree中移除關(guān)鍵字key。
- void BTree_remove(BTree* tree, KeyType key);
-
- ///深度遍歷BTree打印各層結點(diǎn)信息。
- void BTree_print(const BTree tree, int layer=1);
-
- /// 在BTree中查找關(guān)鍵字 key,
- /// 成功時(shí)返回找到的節點(diǎn)的地址及 key 在其中的位置 *pos
- /// 失敗時(shí)返回 NULL 及查找失敗時(shí)掃描到的節點(diǎn)位置 *pos
- BTNode* BTree_search(const BTree tree, int key, int* pos);
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif
- //實(shí)現對order序(階)的B-TREE結構基本操作的封裝。
- //查找:search,插入:insert,刪除:remove。
- //創(chuàng )建:create,銷(xiāo)毀:destory,打?。簆rint。
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include "btree.h"
-
- //#define max(a, b) (((a) > (b)) ? (a) : (b))
- #define cmp(a, b) ( ( ((a)-(b)) >= (0) ) ? (1) : (0) ) //比較a,b大小
- #define DEBUG_BTREE
-
-
- // 模擬向磁盤(pán)寫(xiě)入節點(diǎn)
- void disk_write(BTNode* node)
- {
- //打印出結點(diǎn)中的全部元素,方便調試查看keynum之后的元素是否為0(即是否存在垃圾數據);而不是keynum個(gè)元素。
- printf("向磁盤(pán)寫(xiě)入節點(diǎn)");
- for(int i=0;i<ORDER-1;i++){
- printf("%c",node->key[i]);
- }
- printf("\n");
- }
-
- // 模擬從磁盤(pán)讀取節點(diǎn)
- void disk_read(BTNode** node)
- {
- //打印出結點(diǎn)中的全部元素,方便調試查看keynum之后的元素是否為0(即是否存在垃圾數據);而不是keynum個(gè)元素。
- printf("向磁盤(pán)讀取節點(diǎn)");
- for(int i=0;i<ORDER-1;i++){
- printf("%c",(*node)->key[i]);
- }
- printf("\n");
- }
-
- // 按層次打印 B 樹(shù)
- void BTree_print(const BTree tree, int layer)
- {
- int i;
- BTNode* node = tree;
-
- if (node) {
- printf("第 %d 層, %d node : ", layer, node->keynum);
-
- //打印出結點(diǎn)中的全部元素,方便調試查看keynum之后的元素是否為0(即是否存在垃圾數據);而不是keynum個(gè)元素。
- for (i = 0; i < ORDER-1; ++i) {
- //for (i = 0; i < node->keynum; ++i) {
- printf("%c ", node->key[i]);
- }
-
- printf("\n");
-
- ++layer;
- for (i = 0 ; i <= node->keynum; i++) {
- if (node->child[i]) {
- BTree_print(node->child[i], layer);
- }
- }
- }
- else {
- printf("樹(shù)為空。\n");
- }
- }
-
- // 結點(diǎn)node內對關(guān)鍵字進(jìn)行二分查找。
- int binarySearch(BTNode* node, int low, int high, KeyType Fkey)
- {
- int mid;
- while (low<=high)
- {
- mid = low + (high-low)/2;
- if (Fkey<node->key[mid])
- {
- high = mid-1;
- }
- if (Fkey>node->key[mid])
- {
- low = mid+1;
- }
- if (Fkey==node->key[mid])
- {
- return mid;//返回下標。
- }
- }
- return 0;//未找到返回0.
- }
-
- <span style="color:#660000;"></span>//insert
- /***************************************************************************************
- 將分裂的結點(diǎn)中的一半元素給新建的結點(diǎn),并且將分裂結點(diǎn)中的中間關(guān)鍵字元素上移至父節點(diǎn)中。
- parent 是一個(gè)非滿(mǎn)的父節點(diǎn)
- node 是 tree 孩子表中下標為 index 的孩子節點(diǎn),且是滿(mǎn)的,需分裂。
- *******************************************************************/
- void BTree_split_child(BTNode* parent, int index, BTNode* node)
- {
- #ifdef DEBUG_BTREE
- printf("BTree_split_child!\n");
- #endif
- assert(parent && node);
- int i;
-
- // 創(chuàng )建新節點(diǎn),存儲 node 中后半部分的數據
- BTNode* newNode = (BTNode*)calloc(sizeof(BTNode), 1);
- if (!newNode) {
- printf("Error! out of memory!\n");
- return;
- }
-
- newNode->isLeaf = node->isLeaf;
- newNode->keynum = BTree_D - 1;
-
- // 拷貝 node 后半部分關(guān)鍵字,然后將node后半部分置為0。
- for (i = 0; i < newNode->keynum; ++i){
- newNode->key[i] = node->key[BTree_D + i];
- node->key[BTree_D + i] = 0;
- }
-
- // 如果 node 不是葉子節點(diǎn),拷貝 node 后半部分的指向孩子節點(diǎn)的指針,然后將node后半部分指向孩子節點(diǎn)的指針置為NULL。
- if (!node->isLeaf) {
- for (i = 0; i < BTree_D; i++) {
- newNode->child[i] = node->child[BTree_D + i];
- node->child[BTree_D + i] = NULL;
- }
- }
-
- // 將 node 分裂出 newNode 之后,里面的數據減半
- node->keynum = BTree_D - 1;
-
- // 調整父節點(diǎn)中的指向孩子的指針和關(guān)鍵字元素。分裂時(shí)父節點(diǎn)增加指向孩子的指針和關(guān)鍵元素。
- for (i = parent->keynum; i > index; --i) {
- parent->child[i + 1] = parent->child[i];
- }
-
- parent->child[index + 1] = newNode;
-
- for (i = parent->keynum - 1; i >= index; --i) {
- parent->key[i + 1] = parent->key[i];
- }
-
- parent->key[index] = node->key[BTree_D - 1];
- ++parent->keynum;
-
- node->key[BTree_D - 1] = 0;
-
- // 寫(xiě)入磁盤(pán)
- disk_write(parent);
- disk_write(newNode);
- disk_write(node);
- }
-
- void BTree_insert_nonfull(BTNode* node, KeyType key)
- {
- assert(node);
-
- int i;
-
- // 節點(diǎn)是葉子節點(diǎn),直接插入
- if (node->isLeaf) {
- i = node->keynum - 1;
- while (i >= 0 && key < node->key[i]) {
- node->key[i + 1] = node->key[i];
- --i;
- }
-
- node->key[i + 1] = key;
- ++node->keynum;
-
- // 寫(xiě)入磁盤(pán)
- disk_write(node);
- }
-
- // 節點(diǎn)是內部節點(diǎn)
- else {
- /* 查找插入的位置*/
- i = node->keynum - 1;
- while (i >= 0 && key < node->key[i]) {
- --i;
- }
-
- ++i;
-
- // 從磁盤(pán)讀取孩子節點(diǎn)
- disk_read(&node->child[i]);
-
- // 如果該孩子節點(diǎn)已滿(mǎn),分裂調整值
- if (node->child[i]->keynum == (ORDER-1)) {
- BTree_split_child(node, i, node->child[i]);
- // 如果待插入的關(guān)鍵字大于該分裂結點(diǎn)中上移到父節點(diǎn)的關(guān)鍵字,在該關(guān)鍵字的右孩子結點(diǎn)中進(jìn)行插入操作。
- if (key > node->key[i]) {
- ++i;
- }
- }
- BTree_insert_nonfull(node->child[i], key);
- }
- }
-
- void BTree_insert(BTree* tree, KeyType key)
- {
- #ifdef DEBUG_BTREE
- printf("BTree_insert:\n");
- #endif
- BTNode* node;
- BTNode* root = *tree;
-
- // 樹(shù)為空
- if (NULL == root) {
- root = (BTNode*)calloc(sizeof(BTNode), 1);
- if (!root) {
- printf("Error! out of memory!\n");
- return;
- }
- root->isLeaf = true;
- root->keynum = 1;
- root->key[0] = key;
-
- *tree = root;
-
- // 寫(xiě)入磁盤(pán)
- disk_write(root);
-
- return;
- }
-
- // 根節點(diǎn)已滿(mǎn),插入前需要進(jìn)行分裂調整
- if (root->keynum == (ORDER-1)) {
- // 產(chǎn)生新節點(diǎn)當作根
- node = (BTNode*)calloc(sizeof(BTNode), 1);
- if (!node) {
- printf("Error! out of memory!\n");
- return;
- }
-
- *tree = node;
- node->isLeaf = false;
- node->keynum = 0;
- node->child[0] = root;
-
- BTree_split_child(node, 0, root);
-
- BTree_insert_nonfull(node, key);
- }
-
- // 根節點(diǎn)未滿(mǎn),在當前節點(diǎn)中插入 key
- else {
- BTree_insert_nonfull(root, key);
- }
- }
- <p> </p><p>//remove
- // 對 tree 中的節點(diǎn) node 進(jìn)行合并孩子節點(diǎn)處理.
- // 注意:孩子節點(diǎn)的 keynum 必須均已達到下限,即均等于 BTree_D - 1
- // 將 tree 中索引為 index 的 key 下移至左孩子結點(diǎn)中,
- // 將 node 中索引為 index + 1 的孩子節點(diǎn)合并到索引為 index 的孩子節點(diǎn)中,右孩子合并到左孩子結點(diǎn)中。
- // 并調相關(guān)的 key 和指針。</p>void BTree_merge_child(BTree* tree, BTNode* node, int index)
- {
- #ifdef DEBUG_BTREE
- printf("BTree_merge_child!\n");
- #endif
- assert(tree && node && index >= 0 && index < node->keynum);
-
- int i;
-
- KeyType key = node->key[index];
- BTNode* leftChild = node->child[index];
- BTNode* rightChild = node->child[index + 1];
-
- assert(leftChild && leftChild->keynum == BTree_D - 1
- && rightChild && rightChild->keynum == BTree_D - 1);
-
- // 將 node中關(guān)鍵字下標為index 的 key 下移至左孩子結點(diǎn)中,該key所對應的右孩子結點(diǎn)指向node的右孩子結點(diǎn)中的第一個(gè)孩子。
- leftChild->key[leftChild->keynum] = key;
- leftChild->child[leftChild->keynum + 1] = rightChild->child[0];
- ++leftChild->keynum;
-
- // 右孩子的元素合并到左孩子結點(diǎn)中。
- for (i = 0; i < rightChild->keynum; ++i) {
- leftChild->key[leftChild->keynum] = rightChild->key[i];
- leftChild->child[leftChild->keynum + 1] = rightChild->child[i + 1];
- ++leftChild->keynum;
- }
-
- // 在 node 中下移的 key后面的元素前移
- for (i = index; i < node->keynum - 1; ++i) {
- node->key[i] = node->key[i + 1];
- node->child[i + 1] = node->child[i + 2];
- }
- node->key[node->keynum - 1] = 0;
- node->child[node->keynum] = NULL;
- --node->keynum;
-
- // 如果根節點(diǎn)沒(méi)有 key 了,并將根節點(diǎn)調整為合并后的左孩子節點(diǎn);然后刪除釋放空間。
- if (node->keynum == 0) {
- if (*tree == node) {
- *tree = leftChild;
- }
-
- free(node);
- node = NULL;
- }
-
- free(rightChild);
- rightChild = NULL;
- }
-
- void BTree_recursive_remove(BTree* tree, KeyType key)
- {
- // B-數的保持條件之一:
- // 非根節點(diǎn)的內部節點(diǎn)的關(guān)鍵字數目不能少于 BTree_D - 1
-
- int i, j, index;
- BTNode *root = *tree;
- BTNode *node = root;
-
- if (!root) {
- printf("Failed to remove %c, it is not in the tree!\n", key);
- return;
- }
-
- // 結點(diǎn)中找key。
- index = 0;
- while (index < node->keynum && key > node->key[index]) {
- ++index;
- }
-
- /*======================含有key的當前結點(diǎn)時(shí)的情況====================
- node:
- index of Key: i-1 i i+1
- +---+---+---+---+
- * key *
- +---+---+---+---+---+
- / \
- index of Child: i i+1
- / \
- +---+---+ +---+---+
- * * * *
- +---+---+---+ +---+---+---+
- leftChild rightChild
- ============================================================*/
- /*一、結點(diǎn)中找到了關(guān)鍵字key的情況.*/
- BTNode *leftChild, *rightChild;
- KeyType leftKey, rightKey;
- if (index < node->keynum && node->key[index] == key) {
- /* 1,所在節點(diǎn)是葉子節點(diǎn),直接刪除*/
- if (node->isLeaf) {
- for (i = index; i < node->keynum-1; ++i) {
- node->key[i] = node->key[i + 1];
- //node->child[i + 1] = node->child[i + 2];葉子節點(diǎn)的孩子結點(diǎn)為空,無(wú)需移動(dòng)處理。
- }
- node->key[node->keynum-1] = 0;
- //node->child[node->keynum] = NULL;
- --node->keynum;
-
- if (node->keynum == 0) {
- assert(node == *tree);
- free(node);
- *tree = NULL;
- }
-
- return;
- }
- /*2.選擇脫貧致富的孩子結點(diǎn)。*/
- // 2a,選擇相對富有的左孩子結點(diǎn)。
- // 如果位于 key 前的左孩子結點(diǎn)的 key 數目 >= BTree_D,
- // 在其中找 key 的左孩子結點(diǎn)的最后一個(gè)元素上移至父節點(diǎn)key的位置。
- // 然后在左孩子節點(diǎn)中遞歸刪除元素leftKey。
- else if (node->child[index]->keynum >= BTree_D) {
- leftChild = node->child[index];
- leftKey = leftChild->key[leftChild->keynum - 1];
- node->key[index] = leftKey;
-
- BTree_recursive_remove(&leftChild, leftKey);
- }
- // 2b,選擇相對富有的右孩子結點(diǎn)。
- // 如果位于 key 后的右孩子結點(diǎn)的 key 數目 >= BTree_D,
- // 在其中找 key 的右孩子結點(diǎn)的第一個(gè)元素上移至父節點(diǎn)key的位置
- // 然后在右孩子節點(diǎn)中遞歸刪除元素rightKey。
- else if (node->child[index + 1]->keynum >= BTree_D) {
- rightChild = node->child[index + 1];
- rightKey = rightChild->key[0];
- node->key[index] = rightKey;
-
- BTree_recursive_remove(&rightChild, rightKey);
- }
- /*左右孩子結點(diǎn)都剛脫貧。刪除前需要孩子結點(diǎn)的合并操作*/
- // 2c,左右孩子結點(diǎn)只包含 BTree_D - 1 個(gè)節點(diǎn),
- // 合并是將 key 下移至左孩子節點(diǎn),并將右孩子節點(diǎn)合并到左孩子節點(diǎn)中,
- // 刪除右孩子節點(diǎn),在父節點(diǎn)node中移除 key 和指向右孩子節點(diǎn)的指針,
- // 然后在合并了的左孩子節點(diǎn)中遞歸刪除元素key。
- else if (node->child[index]->keynum == BTree_D - 1
- && node->child[index + 1]->keynum == BTree_D - 1){
- leftChild = node->child[index];
-
- BTree_merge_child(tree, node, index);
-
- // 在合并了的左孩子節點(diǎn)中遞歸刪除 key
- BTree_recursive_remove(&leftChild, key);
- }
- }
-
- /*======================未含有key的當前結點(diǎn)時(shí)的情況====================
- node:
- index of Key: i-1 i i+1
- +---+---+---+---+
- * keyi *
- +---+---+---+---+---+
- / | \
- index of Child: i-1 i i+1
- / | \
- +---+---+ +---+---+ +---+---+
- * * * * * *
- +---+---+---+ +---+---+---+ +---+---+---+
- leftSibling Child rightSibling
- ============================================================*/
- /*二、結點(diǎn)中未找到了關(guān)鍵字key的情況.*/
- else {
- BTNode *leftSibling, *rightSibling, *child;
- // 3. key 不在內節點(diǎn) node 中,則應當在某個(gè)包含 key 的子節點(diǎn)中。
- // key < node->key[index], 所以 key 應當在孩子節點(diǎn) node->child[index] 中
- child = node->child[index];
- if (!child) {
- printf("Failed to remove %c, it is not in the tree!\n", key);
- return;
- }
- /*所需查找的該孩子結點(diǎn)剛脫貧的情況*/
- if (child->keynum == BTree_D - 1) {
- leftSibling = NULL;
- rightSibling = NULL;
-
- if (index - 1 >= 0) {
- leftSibling = node->child[index - 1];
- }
-
- if (index + 1 <= node->keynum) {
- rightSibling = node->child[index + 1];
- }
- /*選擇致富的相鄰兄弟結點(diǎn)。*/
- // 3a,如果所在孩子節點(diǎn)相鄰的兄弟節點(diǎn)中有節點(diǎn)至少包含 BTree_D 個(gè)關(guān)鍵字
- // 將 node 的一個(gè)關(guān)鍵字key[index]下移到 child 中,將相對富有的相鄰兄弟節點(diǎn)中一個(gè)關(guān)鍵字上移到
- // node 中,然后在 child 孩子節點(diǎn)中遞歸刪除 key。
- if ((leftSibling && leftSibling->keynum >= BTree_D)
- || (rightSibling && rightSibling->keynum >= BTree_D)) {
- int richR = 0;
- if(rightSibling) richR = 1;
- if(leftSibling && rightSibling) {
- richR = cmp(rightSibling->keynum,leftSibling->keynum);
- }
- if (rightSibling && rightSibling->keynum >= BTree_D && richR) {
- //相鄰右兄弟相對富有,則該孩子先向父節點(diǎn)借一個(gè)元素,右兄弟中的第一個(gè)元素上移至父節點(diǎn)所借位置,并進(jìn)行相應調整。
- child->key[child->keynum] = node->key[index];
- child->child[child->keynum + 1] = rightSibling->child[0];
- ++child->keynum;
-
- node->key[index] = rightSibling->key[0];
-
- for (j = 0; j < rightSibling->keynum - 1; ++j) {//元素前移
- rightSibling->key[j] = rightSibling->key[j + 1];
- rightSibling->child[j] = rightSibling->child[j + 1];
- }
- rightSibling->key[rightSibling->keynum-1] = 0;
- rightSibling->child[rightSibling->keynum-1] = rightSibling->child[rightSibling->keynum];
- rightSibling->child[rightSibling->keynum] = NULL;
- --rightSibling->keynum;
- }
- else {//相鄰左兄弟相對富有,則該孩子向父節點(diǎn)借一個(gè)元素,左兄弟中的最后元素上移至父節點(diǎn)所借位置,并進(jìn)行相應調整。
- for (j = child->keynum; j > 0; --j) {//元素后移
- child->key[j] = child->key[j - 1];
- child->child[j + 1] = child->child[j];
- }
- child->child[1] = child->child[0];
- child->child[0] = leftSibling->child[leftSibling->keynum];
- child->key[0] = node->key[index - 1];
- ++child->keynum;
-
- node->key[index - 1] = leftSibling->key[leftSibling->keynum - 1];
-
- leftSibling->key[leftSibling->keynum - 1] = 0;
- leftSibling->child[leftSibling->keynum] = NULL;
-
- --leftSibling->keynum;
- }
- }
- /*相鄰兄弟結點(diǎn)都剛脫貧。刪除前需要兄弟結點(diǎn)的合并操作,*/
- // 3b, 如果所在孩子節點(diǎn)相鄰的兄弟節點(diǎn)都只包含 BTree_D - 1 個(gè)關(guān)鍵字,
- // 將 child 與其一相鄰節點(diǎn)合并,并將 node 中的一個(gè)關(guān)鍵字下降到合并節點(diǎn)中,
- // 再在 node 中刪除那個(gè)關(guān)鍵字和相關(guān)指針,若 node 的 key 為空,刪之,并調整根為合并結點(diǎn)。
- // 最后,在相關(guān)孩子節點(diǎn)child中遞歸刪除 key。
- else if ((!leftSibling || (leftSibling && leftSibling->keynum == BTree_D - 1))
- && (!rightSibling || (rightSibling && rightSibling->keynum == BTree_D - 1))) {
- if (leftSibling && leftSibling->keynum == BTree_D - 1) {
-
- BTree_merge_child(tree, node, index - 1);//node中的右孩子元素合并到左孩子中。
-
- child = leftSibling;
- }
-
- else if (rightSibling && rightSibling->keynum == BTree_D - 1) {
-
- BTree_merge_child(tree, node, index);//node中的右孩子元素合并到左孩子中。
- }
- }
- }
-
- BTree_recursive_remove(&child, key);//調整后,在key所在孩子結點(diǎn)中繼續遞歸刪除key。
- }
- }
-
- void BTree_remove(BTree* tree, KeyType key)
- {
- #ifdef DEBUG_BTREE
- printf("BTree_remove:\n");
- #endif
- if (*tree==NULL)
- {
- printf("BTree is NULL!\n");
- return;
- }
-
- BTree_recursive_remove(tree, key);
- }
-
- //=====================================search====================================
-
- BTNode* BTree_recursive_search(const BTree tree, KeyType key, int* pos)
- {
- int i = 0;
-
- while (i < tree->keynum && key > tree->key[i]) {
- ++i;
- }
-
- // Find the key.
- if (i < tree->keynum && tree->key[i] == key) {
- *pos = i;
- return tree;
- }
-
- // tree 為葉子節點(diǎn),找不到 key,查找失敗返回
- if (tree->isLeaf) {
- return NULL;
- }
-
- // 節點(diǎn)內查找失敗,但 tree->key[i - 1]< key < tree->key[i],
- // 下一個(gè)查找的結點(diǎn)應為 child[i]
-
- // 從磁盤(pán)讀取第 i 個(gè)孩子的數據
- disk_read(&tree->child[i]);
-
- // 遞歸地繼續查找于樹(shù) tree->child[i]
- return BTree_recursive_search(tree->child[i], key, pos);
- }
-
- BTNode* BTree_search(const BTree tree, KeyType key, int* pos)
- {
- #ifdef DEBUG_BTREE
- printf("BTree_search:\n");
- #endif
- if (!tree) {
- printf("BTree is NULL!\n");
- return NULL;
- }
- *pos = -1;
- return BTree_recursive_search(tree,key,pos);
- }
-
- //===============================create===============================
- void BTree_create(BTree* tree, const KeyType* data, int length)
- {
- assert(tree);
-
- int i;
-
- #ifdef DEBUG_BTREE
- printf("\n 開(kāi)始創(chuàng )建 B-樹(shù),關(guān)鍵字為:\n");
- for (i = 0; i < length; i++) {
- printf(" %c ", data[i]);
- }
- printf("\n");
- #endif
-
- for (i = 0; i < length; i++) {
- #ifdef DEBUG_BTREE
- printf("\n插入關(guān)鍵字 %c:\n", data[i]);
- #endif
- int pos = -1;
- BTree_search(*tree,data[i],&pos);//樹(shù)的遞歸搜索。
- if (pos!=-1)
- {
- printf("this key %c is in the B-tree,not to insert.\n",data[i]);
- }else{
- BTree_insert(tree, data[i]);//插入元素到BTree中。
- }
-
- #ifdef DEBUG_BTREE
- BTree_print(*tree);//樹(shù)的深度遍歷。
- #endif
- }
-
- printf("\n");
- }
- //===============================destroy===============================
- void BTree_destroy(BTree* tree)
- {
- int i;
- BTNode* node = *tree;
-
- if (node) {
- for (i = 0; i <= node->keynum; i++) {
- BTree_destroy(&node->child[i]);
- }
-
- free(node);
- }
-
- *tree = NULL;
- }
- //測試order序(階)的B-TREE結構基本操作。
- //查找:search,插入:insert,刪除:remove。
- //創(chuàng )建:create,銷(xiāo)毀:destory,打?。簆rint。
-
- #include <stdio.h>
- #include "btree.h"
-
- void test_BTree_search(BTree tree, KeyType key)
- {
- int pos = -1;
- BTNode* node = BTree_search(tree, key, &pos);
- if (node) {
- printf("在%s節點(diǎn)(包含 %d 個(gè)關(guān)鍵字)中找到關(guān)鍵字 %c,其索引為 %d\n",
- node->isLeaf ? "葉子" : "非葉子",
- node->keynum, key, pos);
- }
- else {
- printf("在樹(shù)中找不到關(guān)鍵字 %c\n", key);
- }
- }
-
- void test_BTree_remove(BTree* tree, KeyType key)
- {
- printf("\n移除關(guān)鍵字 %c \n", key);
- BTree_remove(tree, key);
- BTree_print(*tree);
- printf("\n");
- }
-
- void test_btree()
- {
-
- KeyType array[] = {
- 'G','G', 'M', 'P', 'X', 'A', 'C', 'D', 'E', 'J', 'K',
- 'N', 'O', 'R', 'S', 'T', 'U', 'V', 'Y', 'Z', 'F', 'X'
- };
- const int length = sizeof(array)/sizeof(KeyType);
- BTree tree = NULL;
- BTNode* node = NULL;
- int pos = -1;
- KeyType key1 = 'R'; // in the tree.
- KeyType key2 = 'B'; // not in the tree.
-
- // 創(chuàng )建
- BTree_create(&tree, array, length);
-
- printf("\n=== 創(chuàng )建 B- 樹(shù) ===\n");
- BTree_print(tree);
- printf("\n");
-
- // 查找
- test_BTree_search(tree, key1);
- printf("\n");
- test_BTree_search(tree, key2);
-
- // 移除不在B樹(shù)中的元素
- test_BTree_remove(&tree, key2);
- printf("\n");
-
- // 插入關(guān)鍵字
- printf("\n插入關(guān)鍵字 %c \n", key2);
- BTree_insert(&tree, key2);
- BTree_print(tree);
- printf("\n");
-
- test_BTree_search(tree, key2);
-
- // 移除關(guān)鍵字
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'M';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'E';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'G';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'A';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'D';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'K';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'P';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'J';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'C';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'X';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'O';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'V';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'R';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'U';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'T';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- key2 = 'N';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
- key2 = 'S';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
- key2 = 'Y';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
- key2 = 'F';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
- key2 = 'Z';
- test_BTree_remove(&tree, key2);
- test_BTree_search(tree, key2);
-
- // 銷(xiāo)毀
- BTree_destroy(&tree);
- }
-
- int main()
- {
- test_btree();
-
- return 0;
- }
運行結果部分截圖如下:
參考:
- http://www.cppblog.com/converse/archive/2009/10/13/98521.html;
- 此外,這里有一份google關(guān)于B樹(shù)的C++實(shí)現:https://code.google.com/p/cpp-btree/downloads/detail?name=cpp-btree-1.0.1.tar.gz。
聯(lián)系作者:
若有任何問(wèn)題,歡迎隨時(shí)不吝指正?;蛘呗?lián)系我們:
后記:
本blog日后會(huì )更多的關(guān)注數據結構與算法之外的東西,如分布式架構,海量數據處理,搜索引擎相關(guān)。畢竟,算法之外的東西,如瀚海般無(wú)止境,要學(xué)的東西,還有很多。
若轉載,請注明出處,謝謝。完。二零一一年八月三十一日。