日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

B树建立与遍历

發(fā)布時(shí)間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 B树建立与遍历 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

# include <stdio.h>
# include <stdlib.h># include "btrees.h"/* 給一個(gè)結(jié)點(diǎn)分配空間 */
struct btnode * allocateNode(struct btnode *ptr){int i,max;ptr = (struct btnode *)malloc(sizeof(struct btnode));if(!ptr){printf("allocated error!\n");exit(1);}max = 2*M;for(i=0; i<max; i++)ptr->p[i] = NULL;   /* 初始化指針 */memset(ptr->k, 0, (max-1)*sizeof(int)); /* 初始化鍵的值*/return ptr;
}/* 創(chuàng)建一個(gè)空的B樹(shù),就一個(gè)根結(jié)點(diǎn) */
struct btnode * btreeCreate(struct btnode *root){root = allocateNode(root);root->keyNum = 0;root->isleaf = 1;return root;
}/*函數(shù)目的:在B樹(shù)中遞歸的搜索給定的數(shù)據(jù),如果存在則返回?cái)?shù)據(jù)所在節(jié)點(diǎn)和在節(jié)點(diǎn)中* 的位置,不存在返回NULL*/
/*
struct searchResult * btreeSearch(struct btree *root, int data){int i=0;struct searchResult result;//找到節(jié)點(diǎn)中數(shù)據(jù)所在位置while((i<(2*M-1)) && (data>root->k[i]))i++;//數(shù)據(jù)找到,返回結(jié)果if(root->k[i] == data){result.ptr = root;result.pos = i;return result;}//已經(jīng)搜索到葉節(jié)點(diǎn),數(shù)據(jù)不存在,返回NULLif(root->isleaf){result.ptr = NULL;result.pos = -1;return result;}else{   //非葉節(jié)點(diǎn),繼續(xù)搜索子樹(shù)節(jié)點(diǎn)return btreeSearch(root->p[i], data);}
}
*///函數(shù)目的:分裂存儲(chǔ)數(shù)達(dá)到最大的節(jié)點(diǎn)
void btreeSplitChild(struct btnode *parent, int pos, struct btnode *child){struct btnode *child2;int i;//為新分裂出的節(jié)點(diǎn)分配空間child2 = allocateNode(child2);//與被分裂點(diǎn)同級(jí)child2->isleaf = child->isleaf;//設(shè)置節(jié)點(diǎn)數(shù)child2->keyNum = M-1;//復(fù)制數(shù)據(jù)for(i=0; i<M-1; i++)child2->k[i] = child->k[i+M];//如果不是葉節(jié)點(diǎn),復(fù)制指針if(!child->isleaf)for(i=0; i<M; i++)child2->p[i] = child->p[i+M];child->keyNum = M-1;//將中間數(shù)作為索引插入到雙親節(jié)點(diǎn)中//插入點(diǎn)后面的關(guān)鍵字和指針都往后移動(dòng)一個(gè)位置for(i=parent->keyNum; i>pos; i--){parent->k[i] = parent->k[i-1];parent->p[i+1] = parent->p[i];}parent->k[pos] = child->k[M-1];parent->keyNum++;parent->p[pos+1] = child2;
}/* 函數(shù)目的:向非滿的節(jié)點(diǎn)中插入一個(gè)數(shù)據(jù)* 注意:插入前保證key在原來(lái)的B樹(shù)中不存在*/
void btreeInsertNoneFull(struct btnode *ptr, int data){int i;struct btnode *child;   //要插入結(jié)點(diǎn)的子結(jié)點(diǎn)i = ptr->keyNum;//如果是葉節(jié)點(diǎn),直接插入數(shù)據(jù)if(ptr->isleaf){while((i>0) && (data<ptr->k[i-1])){ptr->k[i] = ptr->k[i-1];i--;}//插入數(shù)據(jù)ptr->k[i] = data;ptr->keyNum++;}else{   //不是葉節(jié)點(diǎn),找到數(shù)據(jù)應(yīng)插入的子節(jié)點(diǎn)并插入while((i>0) && (data<ptr->k[i-1]))i--;child = ptr->p[i];if(child->keyNum == 2*M-1){btreeSplitChild(ptr, i, child);if(data > ptr->k[i])i++;}child = ptr->p[i];btreeInsertNoneFull(child, data);   //在子樹(shù)中遞歸}
}/* 插入一個(gè)結(jié)點(diǎn) */
struct btnode * btreeInsert(struct btnode *root, int data){struct btnode *new;/* 檢查是否根節(jié)點(diǎn)已滿,如果已滿,分裂并生成新的根節(jié)點(diǎn) */if(root->keyNum == 2*M-1){new = allocateNode(new);new->isleaf = 0;new->keyNum = 0;new->p[0] = root;btreeSplitChild(new, 0, root);btreeInsertNoneFull(new, data);return new;}else{    //還沒(méi)到最大數(shù)據(jù)數(shù),直接插入btreeInsertNoneFull(root, data);return root;}
}//函數(shù)目的:廣度優(yōu)先顯示樹(shù)
void btreeDisplay(struct btnode *root){int i, queueNum=0;int j;struct btnode *queue[20];struct btnode *current;//加入隊(duì)列queue[queueNum] = root;queueNum++;while(queueNum>0){//出隊(duì)current = queue[0];queueNum--;//移出第一個(gè)元素后后面的元素往前移動(dòng)一個(gè)位置for(i=0; i<queueNum; i++)queue[i] = queue[i+1];//顯示節(jié)點(diǎn)j = current->keyNum;printf("[ ");for(i=0; i<j; i++){printf("%d ", current->k[i]);}printf("]  ");//子節(jié)點(diǎn)入隊(duì)if(current!=NULL && current->isleaf!=1){for(i=0; i<=(current->keyNum); i++){queue[queueNum] = current->p[i];queueNum++;}}}printf("\n");
}int main()
{struct btnode *root;int a[13] = {18, 31, 12, 10, 15, 48, 45, 47, 50, 52, 23, 30, 20};int i;root = btreeCreate(root);for(i=0; i<13; i++){root = btreeInsert(root, a[i]);btreeDisplay(root);}return 0;
}


?

總結(jié)

以上是生活随笔為你收集整理的B树建立与遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。