C語言實現高級數據結構之B樹

B樹

概述

B樹堪稱二叉樹放開了生育限制,其任何一個節點,都可以擁有兩個以上的孩子。但孩子也不能無限地多下去,否則就成了鏈表,所以對於任何一個節點而言,其子節點的個數存在一個上界和下界。存在上界便意味着我們可以通過數組來很方便地表示其子節點。

另一方面,由於子節點變多,所以不能再像二叉樹一樣通過比較左右節點的大小來規範節點的值。所以,每個節點需要有一些鍵值將其子節點分開。

例如,父節點PPnn個子節點C1,C2,,CnC_1,C_2,\ldots,C_n,相應地需要有n1n-1個鍵值k1,k2,,kn1k_1,k_2,\ldots,k_{n-1},使得C1k1C2k2kn1<CnC_1\leqslant k_1\leqslant C_2\leqslant k_2\ldots\leqslant k_{n-1}<C_n

除此之外,B樹要求每一個末代節點具有相同的深度,所以,如果希望通過數組實現,則需要對末代節點進行標記。再樹中,一般把末代節點即沒有子節點的節點成爲葉節點,所以這個屬性寫爲isLeaf,其C語言實現爲

#define M 2

//數組形式的B樹節點
typedef struct BTNODE{
    struct BTNODE* father;
    int nKeys;
    int key[2*M-1];             //鍵值
    struct BTNODE* child[2*M];  //子節點
    int isLeaf;                 //判定是否爲末代節點
}btNode;

其中,M*2即爲B樹子節點的上限,在上面的代碼中,將M設爲2,此時父節點最多有三個鍵值,四個子節點,此時爲B樹的一個特例,被稱爲2-3-4樹,如下圖所示。

在這裏插入圖片描述

上圖父節點的四個子節點中,左數第一個節點小於key[0],第二個處於key[0]key[1]之間,依次類推。

由於父節點的子節點數目可變,所以這個B樹的子節點或許可以借鑑有序鏈表的一些思路,通過另一種形式實現。

//鏈表形式的B樹節點
typedef struct BNODE{
    struct BNODE* father;
    struct BNODE* next;     //兄弟節點
    struct BNODE* child;    //子節點
    int nChilds;            //即其子節點及其兄弟節點個數
}bNode;

鏈表思路下,如果childNULL則說明此節點爲末代節點。然而,B樹之所以被提出是用於解決機械硬盤的存儲問題,鏈表作爲一種指針索引的方式,其數據存儲位置並不連續,用在機械硬盤中效率應該是十分低下的。

所以,接下來對B樹的實現將以數組形式爲主。由於每一個節點都包含許多鍵值,所以對於搜索操作而言,不僅需要返回當前節點,而且需要返回當前節點鍵值所對應的位置。

因此,需要定義一個包含節點指針和鍵值位置的數據結構

typedef struct BKEYNODE{
    btNode* node;           //節點指針
    int key;                //鍵值的序號
}bKeyNode;
//B樹查詢操作
bKeyNode searchBtNode(btNode* root, int val){
    bKeyNode bKey = {NULL,0};
    int i;
    while (root->isLeaf == 0){
        i = 0;
        while (i<root->nKeys && root->key[i]<val)
            i++;
        if(val==root->key[i]){
            bKey.node = root;
            bKey.key = i;
        }else
            root = root->child[i];
    }
    bKey.node = root;
    i = 0;
    while(i<root->nKeys && root->key[i]<val)
        i++;
    if(val==root->key[i]){
        bKey.node = root;
        bKey.key = i;
    }
    return bKey;
}

初始化

B樹最麻煩的性質便是等深性,由於末代節點的深度相同,所以新插入的節點不能像二叉樹那樣掛在已有的末代節點下面,而是需要嵌入到某個父節點的一羣子節點當中。

假設當前的B樹共有nn代,那麼我們新插入的節點最多也只能在第nn代。設其父節點爲第n1n-1代的PP,如果PP的子節點個數已經達到了上限,那麼新插入的節點就裝不下了,所以需要某種操作,將PP裂開。此時n1n-1代將多出一個節點,如果因此而導致PP所在的n1n-1代子節點個數超出上限,那麼就需要把PP的父節點裂開,依次上推,一直裂到不超上限爲止。

如果最後連根節點都不得不裂開,那麼樹高加一。故其分裂算法爲

#define M 2

//當pNode保留[0,M)個節點,將[M,2M]節點分給qNode
//root爲根節點,pNode爲將要分裂的節點
btNode* splitBtNode(btNode* root,btNode* pNode){
    btNode* qNode = (btNode*)malloc(sizeof(btNode));
    btNode* gNode;     //p,q節點的父節點
    int i;
    //pNode將區間(M,2M)分給qNode
    for (i = M; i < M*2-1; i++){
        qNode->key[i-M]=pNode->key[i+1];
        qNode->child[i-M]=pNode->child[i];
    }
    qNode->isLeaf = pNode->isLeaf;

    //如果pNode爲根節點,新建一個空的根節點
    if (pNode->father==NULL){
        gNode = (btNode*)malloc(sizeof(btNode));
        gNode->father = NULL;
        gNode->child[0] = pNode;
        pNode->father = gNode;
        gNode->nKeys = 0;
        gNode->isLeaf = 0;
        root = gNode;
    }else
        gNode=pNode->father;

    //將key[M]分給gNode
    for(i=0;i<gNode->nKeys;i++)
        if(gNode->key[i]>pNode->key[M])
            break;      //i爲pNode->key[M]的插入點
    for (int j = gNode->nKeys; j > i; j--)
        gNode->key[j]=gNode->key[j-1];  //gNode鍵值後移
    for (int j = gNode->nKeys; j>i; j--)
        gNode->child[j+1]=gNode->child[j];

    gNode->key[i] = pNode->key[M];
    gNode->child[i+1] = qNode;          //qNode認父
    gNode->nKeys++;                     //gNode鍵數加1

    qNode->father = gNode;
    qNode->nKeys = pNode->nKeys-M-1;    //(M,nKeys)共有nKeys-M-1個節點
    if(pNode->isLeaf != 1)
        for(i = 0; i <= qNode->nKeys;i++){
            qNode->child[i]=pNode->child[i+M+1];
            qNode->child[i]->father = qNode;
        }
    pNode->nKeys=M;

    //如果gNode的鍵值達到最大,則需要對gNode進行分裂
    if(gNode->nKeys==M*2)
        root=splitBtNode(root,gNode);
    return root;
}

B樹的插入與此前的二叉樹最大的不同在於,二叉樹每次插入一個新值,總結點數就會加1,B樹則不然,每次插入一個新值,只不過是增加某一個節點的鍵數而已。只有噹噹前節點的鍵數大於上限,纔會通過分裂操作增加節點的數目。其插入操作實現爲

//B樹插入操作
btNode* insertBtNode(btNode* root, int val){
    int i;
    btNode *oriRoot = root; //保護根
    while(root->isLeaf!=1){
        i=0;
        while(i<root->nKeys && root->key[i]<val)
            i++;            //插入點
        root = root->child[i];  
    }

    for(i = 0; i < root->nKeys; i++)
        if(root->key[i]>val)
            break;      //i爲val插入tempRoot的位置
    for (int j = root->nKeys; j > i; j--)
        root->key[j]=root->key[j-1];    //gNode鍵值後移
    root->key[i] = val;                 //爲節點添加一個新的鍵值
    root->nKeys++;                      //當前節點鍵數加一
    
    if(root->nKeys==2*M)
        oriRoot = splitBtNode(oriRoot,root);
    printf("%d was inserted\n",val);

    return oriRoot;
}

然後,像往常一樣,建立主函數生成一棵B樹,並且打印出來

//打印B樹
void printBtNode(btNode* root, int n){
    printf("the %dth has %d keys:",n,root->nKeys);
    for (int i = 0; i < root->nKeys; i++)
        printf("%d,",root->key[i]);
    printf("\n");
    if (root->isLeaf)
        return;
    for (int i = 0; i < root->nKeys+1; i++){
        printBtNode(root->child[i],n+1);
    }
}
//主函數
int main(){
    btNode *root = (btNode*)malloc(sizeof(btNode));
    root->isLeaf=1;
    root->key[0] = 10;
    root->father=NULL;
    root->nKeys = 1;

    int temp[20] = {12,20,25,35,7,
                    18,9,33,17,15,
                    14,16,29,21,11,
                    23,6,22,28,3};
    for (int i = 0; i < 20; i++)
        root = insertBtNode(root,temp[i]);

    printBtNode(root,0);
    return 0;
}

得到結果爲

PS E:\Code\AlgC> gcc .\bTree.c
PS E:\Code\AlgC> .\a.exe      
...
the 0th has 2 keys:12,20,
the 1th has 2 keys:7,10,
the 2th has 2 keys:3,6,
the 2th has 1 keys:9,
the 2th has 1 keys:11,
the 1th has 1 keys:17,
the 2th has 3 keys:14,15,16,
the 2th has 1 keys:18,
the 1th has 2 keys:25,33,
the 2th has 3 keys:21,22,23,
the 2th has 2 keys:28,29,
the 2th has 1 keys:35,

其樹圖爲

在這裏插入圖片描述

可見實現了B樹的基本特徵.

刪除節點

根據以往的經驗,刪除節點往往比插入節點更復雜,B樹也不列外。對於B樹來說,被刪除的節點可分爲兩類,一類是末代節點,另一類爲非末代節點。

對於非末代節點來說,只需像以前一樣,通過交換待刪除節點鍵值與子節點鍵值,從而使得刪除指針下移,直到刪除指針抵達末代節點,所以最終我們只需考慮末代節點的情況即可。

如果待刪除的末代節點有多於一個鍵值,那麼直接刪除即可。如果末代節點只有一個鍵,那麼則需要查看其兄弟節點,如果其兄弟節點的鍵數多於1,則只需將刪除指針轉向其兄弟節點,然後刪除即可。

如果其兄弟節點只有一個鍵值,那麼需要考慮合併這兩個節點。

//數組arr中有n個元素,刪除數組中第i個元素
void deleteArray(int* arr, int n,int i){
    for (int j = i; j < n; j++)
        arr[j] = arr[j+1];
}

//B樹刪除操作
//root爲根節點,dNode爲待刪除節點,nKey爲待刪除鍵值的序號
btNode* deleteBtNode(btNode* root, btNode* dNode, int nkey){
    //當被刪除節點不是葉節點,則刪除指針下移
    if(dNode->isLeaf==0){
        int dChild = dNode->nKeys<M ? nkey+1:nkey;//被刪除的孩子
        int dKey = dNode->nKeys<M ?
             0 : dNode->child[dChild]->nKeys-1;
        dNode->key[nkey] = dNode->child[dChild]->key[dKey];
        root = deleteBtNode(root, dNode->child[dChild],dKey);
    }else{
        if(dNode->nKeys>1){
            deleteArray(dNode->key,dNode->nKeys,nkey);
            dNode->nKeys--;
        }else{
            btNode* pNode = dNode->father;
            int i=0;
            while (i<pNode->nKeys&&pNode->key[i]<dNode->key[nkey])
                i++;    //i爲待刪除節點的序號

            if(i<pNode->nKeys/2){    //當i爲左邊的節點時,考慮i右側的孩子
                btNode* bNode = pNode->child[i+1];
                //當dNode右側兄弟有多於一個節點時,則取之首節點
                if(pNode->child[i+1]->nKeys>1){
                    dNode->key[0] = pNode->key[i];
                    pNode->key[i]=bNode->key[0];
                    deleteArray(bNode->key,bNode->nKeys,0);
                    dNode->nKeys--;
                }else{//當dNode左側的兄弟也只有一個節點時,則採取合併操作
                    for(int j=bNode->nKeys-1; j>0;j--)
                        bNode->key[j]=bNode->key[j-1];
                    bNode->key[0] = pNode->key[i];
                    bNode->nKeys++;
                    deleteArray(pNode->key,pNode->nKeys,i);
                    pNode->nKeys--;
                }
            }else{
                btNode* bNode = pNode->child[i-1];
                if(bNode->nKeys>1){
                    dNode->key[0] = pNode->key[i-1];
                    pNode->key[i-1] = bNode->key[bNode->nKeys-1];
                    bNode->nKeys--;//刪除最後一個節點
                }else{//當dNode左側的兄弟也只有一個節點時,則採取合併操作
                    bNode->key[bNode->nKeys]=pNode->key[i-1];
                    bNode->nKeys++;
                    deleteArray(pNode->key,pNode->nKeys,i-1);
                    pNode->nKeys--;
                }
            }
        }
    }
    return root;
}

驗證一下

int main(){
    btNode *root = (btNode*)malloc(sizeof(btNode));
    root->isLeaf=1;
    root->key[0] = 10;
    root->father=NULL;
    root->nKeys = 1;

    int temp[20] = {12,20,25,35,7,
                    18,9,33,17,15,
                    14,16,29,21,11,
                    23,6,22,28};
    for (int i = 0; i < 20; i++)
        root = insertBtNode(root,temp[i]);
    
    printBtNode(root,0);
    
    bKeyNode delNode = searchBtNode(root, 18);
    printf("-----");
    printf("the %d key was searched from %d keys\n",delNode.key,delNode.node->nKeys);
    
    root = deleteBtNode(root, delNode.node,delNode.key);
    printBtNode(root,0);

    return 0;
}

結果爲

PS E:\Code\AlgC> gcc .\bTree.c
PS E:\Code\AlgC> .\a.exe      
...//和之前相同
-----the 0 key was searched from 1 keys
the 0th has 2 keys:12,20,
the 1th has 2 keys:7,10,
the 2th has 2 keys:3,6,
the 2th has 1 keys:9,
the 2th has 1 keys:11,
the 1th has 1 keys:16,
the 2th has 2 keys:14,15,
the 2th has 1 keys:17,
the 1th has 2 keys:25,33,
the 2th has 3 keys:21,22,23,
the 2th has 2 keys:28,29,
the 2th has 1 keys:35,

畫圖表示爲

在這裏插入圖片描述

發佈了59 篇原創文章 · 獲贊 66 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章