1. 程式人生 > >3階B-樹

3階B-樹

資料結構與演算法分析——c語言描述 第四章樹 B-樹

2016-04-14建立:

好久沒更新部落格,這7天斷斷續續寫B樹,學彙編,學計算機組成原理。

B樹好難啊,還沒寫完。只寫了25%。。。

插入剩下兩種情況沒寫:

1.祖父未滿,父親滿,兒子滿。

2.祖父滿,父親滿,兒子滿。

想不到怎麼寫。這兩個情況有兩種相同的地方,把父親拆成兩個。

父親拆成兩個可以這樣寫:像父親未滿,兒子滿那樣,新建一個兒子,移動兒子間的元素,插入元素,父親拆成兩個(更新兒子數量,兒子指標,更新索引)。

接下來

對於第一個情況:

告訴祖父要新建兒子(這裡又要像伸展樹那樣用結構了,告訴祖父要增加兒子,要包括新兒子在兒子分支的左邊還是右邊,和新兒子的指標),祖父要更新兒子指標,更新數量,索引。

對於第二個情況:

祖父滿了。拆成兩個祖父,告訴曾祖父多了一個兒子。這個情況和第二個情況一樣。

2016.4.15更新

這個B樹8天。終於把插入寫完了。寫這個B樹是真的真的要狗帶了。寫得我很煩躁,感到智商非常不夠用。不吐槽了。說一說收穫。

寫程式碼是用腦子想出來的,不是一腦門光寫,不斷測試,不斷改,不斷重複迴圈。這個辦法雖然很快來結果,但是效率很低,並且只能應付簡單的程式設計,一遇到難的就傻了。整天用除錯跟蹤和printfh輸出除錯結果,這樣效率非常低非常低。

昨天和今天我意識到這個問題。我先關了電腦。在幾張紙上不斷寫出所有情景下插入的情況,非常詳細的。然後從這麼多過程中提取相同的過程,做成7,8個私有函式,這些函式(過程)都是經常需要用到的。我手寫了這些函式的宣告表,想該傳入什麼引數,返回型別。

寫完後稍微改改,沒怎麼除錯。別怕,想出來,深度思考是痛苦的,但這是人和動物的區別。

有一個地方感覺處理不是很好。因為樹的結構經常更改,所以insert要像伸展樹呼叫internalinsert,介面的insert是用於假如insert插入多了一個兄弟,然後要構建兩者的父親。我就是複製了相同的程式碼。更改了返回新兒子的部分。不知道有沒有更優雅的程式碼組織方式。大神請指教!

2016.4.19更新

刪除很快就寫完了,和插入差不多。程式碼是想出來的,不是不停debug弄出來的。看著曾經的日誌還寫著“不停debug是一個不停的思考和創作過程”。。。確實這樣,但沒必要這麼折騰,況且這方法對簡單的程式設計才有效,難的話連續弄幾天也弄不出來。正確的姿勢是先想過程,從全域性的角度想想需要定義哪些函式。

到了學期中就是事多。。。。加上自己確實是懶了。不能再這樣下去。

btree.h

typedef int ElementType;

#ifndef _B_Tree_h
#define _B_Tree_h

struct BtreeNode;
typedef struct BtreeNode* PtrToNode;
typedef struct BtreeNode* Btree;

Btree createBtree();
void makeEmpty(Btree t);
PtrToNode find(ElementType X, Btree t);
Btree insert(ElementType X, Btree t);
Btree Delete(ElementType X, Btree t);
void Dir(Btree t);
#endif




btree.c

#include"btree.h"
#include<stdlib.h>
#include"fatal.h"
struct BtreeNode;

#define M 3


struct BtreeNode {
	int type;
	int sonNum;//兒子數量,或當前擁有資料的數量(最底層)
	PtrToNode PtrToSon[M];
	ElementType elem[M];
};

static int binarySearch(PtrToNode bottomNode, ElementType X) {//對分搜尋
	if (bottomNode->type != 2)
		Error("ERROR!");
	ElementType* arr = bottomNode->elem;
	int n = bottomNode->sonNum;

	if (n > 0) {
		int low = 0;
		int high = n - 1;
		while (low <= high) {
			int mid = (high + low) / 2;
			if (X < arr[mid]) {
				high = mid - 1;
			}
			else if (X > arr[mid]) {
				low = mid + 1;
			}
			else
				return mid;
		}
		return -1;
	}
	return -1;
}

static int getSonBranch(PtrToNode Node, ElementType X) {
	ElementType* arr = Node->elem;
	int n = Node->sonNum;
	if (n == 1)
		return 0;
	else if (n == 2) {
		if (X < arr[0])
			return 0;
		else
			return 1;
	}
	else if (n >= 2) {
		int low = 0;
		int high = n - 1 - 1;//第一個分支沒有索引
		while (low <= high) {
			int mid = (high + low) / 2;
			if (X < arr[mid]) {
				high = mid - 1;
			}
			else if (X > arr[mid]) {
				low = mid + 1;
			}
			else
				return mid + 1;//第一個分支沒有索引
		}

		return high + 1;//退出迴圈,此時high在左,low在右,X位於high和low的值之間
	}
	else {
		Error("GET SON BRANCH ERROR");
	}
}

static void binaryInsert(ElementType arr[], int n, ElementType X) {
	int low = 0;
	int high = n - 1;
	while (low <= high) {

		int mid = (low + high) / 2;
		if (arr[mid] < X) {
			low = mid + 1;
		}
		else if (arr[mid] > X)
			high = mid - 1;
	}

	while (arr[low] < X&& low < n) {
		low++;
	}
	for (int i = n; i > low; i--) {
		arr[i] = arr[i - 1];
	}
	arr[low] = X;
}

static void binaryInsertForBottomNode(PtrToNode bottomNode, ElementType X) {//對分插入
	if (bottomNode->type != 2)
		Error("eror!");
	ElementType* arr = bottomNode->elem;
	int n = bottomNode->sonNum;

	if (n > 0) {
		binaryInsert(arr, n, X);
	}
	else
		arr[0] = X;
	bottomNode->sonNum++;
}

static PtrToNode allocNode(int type) {//0為內部節點 1為葉子 2為底層節點
	PtrToNode p = malloc(sizeof(struct BtreeNode));
	if (p == NULL)
		Error("OUT OF SPACE!!");
	p->type = type;
	p->sonNum = 0;
	return p;
}


static void updateIndex(PtrToNode p) {
	if (p->sonNum >= 2) {
		for (int i = 0; i < p->sonNum - 1; i++) {
			PtrToNode son = p->PtrToSon[i + 1];
			while (son->type != 2) {
				son = son->PtrToSon[0];
			}
			p->elem[i] = son->elem[0];
		}
	}
}

static PtrToNode insertAndSplitBottomNode(PtrToNode b1, ElementType X) {
	if (b1->sonNum != 3)
		Error("ERROR!");
	PtrToNode b2 = allocNode(2);
	ElementType tempElem[4];
	for (int i = 0; i < 3; i++)
		tempElem[i] = b1->elem[i];
	binaryInsert(tempElem, 3, X);

	for (int i = 0; i < 2; i++)
		b1->elem[i] = tempElem[i];
	for (int i = 0; i < 2; i++)
		b2->elem[i] = tempElem[i + 2];
	b1->sonNum = b2->sonNum = 2;
	return b2;
}

static PtrToNode split(Btree father, PtrToNode newSon, int sonBranch) {
	PtrToNode allSon[4];
	int i, j;
	for (i = 0, j = 0; j <= sonBranch; i++, j++)
		allSon[i] = father->PtrToSon[j];
	allSon[i++] = newSon;
	for (; j < father->sonNum; i++, j++)
		allSon[i] = father->PtrToSon[j];

	PtrToNode newFater = allocNode(father->type);
	father->sonNum = 2;
	father->PtrToSon[0] = allSon[0];
	father->PtrToSon[1] = allSon[1];
	updateIndex(father);

	newFater->sonNum = 2;
	newFater->PtrToSon[0] = allSon[2];
	newFater->PtrToSon[1] = allSon[3];
	updateIndex(newFater);
	return newFater;
}

static void insertPtrToSon(PtrToNode father, PtrToNode newSon, int sonBranch) {
	if (father->sonNum == 0) {
		father->PtrToSon[0] = newSon;
	}
	else {
		int i;
		for (i = father->sonNum; i > sonBranch + 1; i--) {
			father->PtrToSon[i] = father->PtrToSon[i - 1];
		}
		father->PtrToSon[sonBranch + 1] = newSon;
	}
	father->sonNum++;
}



Btree createBtree() {
	Btree t = malloc(sizeof(struct BtreeNode));
	if (t == NULL)
		Error("OUT OF MEMORY!");
	t->sonNum = 0;
	t->type = 1;//空樹,根節點也是樹葉
	return t;
}

void makeEmpty(Btree t) {
	if (t->type) {
		for (int i = 0; i < t->sonNum; i++)
			free(t->PtrToSon[i]);
		free(t);
	}
	else {
		for (int i = 0; i < t->sonNum; i++) {
			makeEmpty(t->PtrToSon[i]);
		}
		free(t);
	}
}

PtrToNode find(ElementType X, Btree t) {
	if (t->type == 0) {//內部節點
		int p = getSonBranch(t, X);
		return find(X, t->PtrToSon[p]);
	}
	else {
		int p;
		if (t->sonNum == 0)//剛建立樹的時候,空
			return NULL;
		else if (t->sonNum == 1) {
			p = binarySearch(t->PtrToSon[0], X);
			if (p == -1)
				return NULL;
			else
				return t->PtrToSon[0];
		}
		else {
			p = getSonBranch(t, X);//選擇兒子分支
			int tempCursor = binarySearch(t->PtrToSon[p], X);//在最底層中查詢
			if (tempCursor == -1)
				return NULL;
			return t->PtrToSon[p];
		}

	}
}




static void insertElem_LeafEmpty(ElementType X, Btree t) {
	PtrToNode newSon = allocNode(2);
	binaryInsertForBottomNode(newSon, X);
	insertPtrToSon(t, newSon, 0);
}

static void insertElem_LeafSonNotFull(ElementType X, Btree t, int sonChoice) {
	binaryInsertForBottomNode(t->PtrToSon[sonChoice], X);
	updateIndex(t);
}

static Btree insert_internal(ElementType X, Btree t);

static void insertElem_LeafNotFull_LeafSonFull(ElementType X, Btree t, int sonBranch) {
	PtrToNode newSon = insertAndSplitBottomNode(t->PtrToSon[sonBranch], X);
	insertPtrToSon(t, newSon, sonBranch);
	updateIndex(t);
}



static PtrToNode insertElem_LeafFull_LeafSonFull(ElementType X, Btree t, int sonBranch) {
	PtrToNode newSon = insertAndSplitBottomNode(t->PtrToSon[sonBranch], X);
	PtrToNode newFather = split(t, newSon, sonBranch);
	return newFather;
}



static Btree insert_internal(ElementType X, Btree t) {
	if (t->type == 0) {//非葉子
		int sonBranch = getSonBranch(t, X);
		PtrToNode newSon = insert_internal(X, t->PtrToSon[sonBranch]);
		if (newSon) {
			if (t->sonNum < M) {
				insertPtrToSon(t, newSon, sonBranch);
				updateIndex(t);
				return NULL;
			}
			else {
				PtrToNode newfather = split(t, newSon, sonBranch);
				return newfather;
			}
		}
		else {
			return NULL;
		}
	}
	else if (t->type == 1) {//葉子
		if (t->sonNum == 0) {
			insertElem_LeafEmpty(X, t);
			return NULL;
		}
		else {
			int sonBranch = getSonBranch(t, X);//選擇兒子分支
			int XCursor = binarySearch(t->PtrToSon[sonBranch], X);//在兒子中查詢
			if (XCursor == -1) {//不存在X
				if (t->PtrToSon[sonBranch]->sonNum < M) {//葉子的兒子數量未滿
					insertElem_LeafSonNotFull(X, t, sonBranch);
					return NULL;
				}
				else if (t->sonNum < M && t->PtrToSon[sonBranch]->sonNum == M) {//父親未滿,兒子滿
					insertElem_LeafNotFull_LeafSonFull(X, t, sonBranch);
					return NULL;
				}
				else {//父親滿,兒子滿
					PtrToNode newfather = insertElem_LeafFull_LeafSonFull(X, t, sonBranch);
					return newfather;
				}
			}
			else
				return NULL;//已存在X
		}
	}
	else {
		Error("type error");
	}
}

Btree insert(ElementType X, Btree t) {
	if (t->type == 0) {//非葉子
		int sonBranch = getSonBranch(t, X);
		PtrToNode newSon = insert_internal(X, t->PtrToSon[sonBranch]);
		if (newSon) {
			if (t->sonNum < M) {
				insertPtrToSon(t, newSon, sonBranch);
				updateIndex(t);
				return t;
			}
			else {
				PtrToNode newfather = split(t, newSon, sonBranch);
				PtrToNode p = allocNode(0);
				insertPtrToSon(p, t, 0);
				insertPtrToSon(p, newfather, 0);
				updateIndex(p);
				return p;
			}
		}
		else {
			return t;
		}
			
	}
	else if (t->type == 1) {//葉子
		if (t->sonNum == 0) {
			insertElem_LeafEmpty(X, t);
			return t;
		}
		else {
			int sonBranch = getSonBranch(t, X);//選擇兒子分支
			int XCursor = binarySearch(t->PtrToSon[sonBranch], X);//在兒子中查詢
			if (XCursor == -1) {//不存在X
				if (t->PtrToSon[sonBranch]->sonNum < M) {//葉子的兒子數量未滿
					insertElem_LeafSonNotFull(X, t, sonBranch);
					return t;
				}
				else if (t->sonNum < M && t->PtrToSon[sonBranch]->sonNum == M) {//父親未滿,兒子滿
					insertElem_LeafNotFull_LeafSonFull(X, t, sonBranch);
					return t;
				}
				else {//父親滿,兒子滿
					PtrToNode newfather = insertElem_LeafFull_LeafSonFull(X, t, sonBranch);
					PtrToNode p = allocNode(0);
					insertPtrToSon(p, t, 0);
					insertPtrToSon(p, newfather, 0);
					updateIndex(p);
					return p;
				}
			}
			else
				return t;//已存在X
		}
	}
	else
		Error("type error!");
}



void Dir(Btree t) {
	if (t->type == 0) {
		printf("\n");
		for (int i = 0; i < t->sonNum; i++) {
			Dir(t->PtrToSon[i]);
		}
			
	}
	else if (t->type == 1) {
		
		for (int i = 0; i < t->sonNum; i++) {
			if(i==1||i==2)
			printf("%d: ", t->elem[i-1]);
			for (int j = 0; j < t->PtrToSon[i]->sonNum; j++) {
				printf("%d ", t->PtrToSon[i]->elem[j]);
			}
			printf("   ");
		}
		printf("\n");
	}
}







static void deleteAndFreePtrToSon(PtrToNode father, int sonBranch) {
	free(father->PtrToSon[sonBranch]);
	for (int i = sonBranch; i < father->sonNum - 1; i++) {
		father->PtrToSon[i] = father->PtrToSon[i + 1];
	}

	father->sonNum--;
}

static void deletePtrToSon(PtrToNode father, int sonBranch) {
	for (int i = sonBranch; i < father->sonNum - 1; i++) {
		father->PtrToSon[i] = father->PtrToSon[i + 1];
	}
	father->sonNum--;
}



static void binaryDeleteForBottomNode(PtrToNode bottomNode, ElementType X) {
	int p = binarySearch(bottomNode, X);
	if (p != -1) {
		for (int i = p; i < bottomNode->sonNum - 1; i++) {
			bottomNode->elem[i] = bottomNode->elem[i + 1];
		}
		bottomNode->sonNum--;
	}
}

static int delete_internal(ElementType X, Btree t) {//返回型別0表示兒子夠,1表示兒子不夠
	if (t->type == 0) {//內部節點
		int p = getSonBranch(t, X);
		int isSon_NotEnough_grandson = delete_internal(X, t->PtrToSon[p]);
		if (isSon_NotEnough_grandson == 0)
			return 0;
		else {////
			int brother;
			if (p + 1 < t->sonNum) {
				brother = p + 1;
			}
			else {
				brother = p - 1;
			}
			if (t->PtrToSon[brother]->sonNum == 2) {
				if (p < brother) {
					insertPtrToSon(t->PtrToSon[brother], t->PtrToSon[p]->PtrToSon[0], -1);
				}
				else
					insertPtrToSon(t->PtrToSon[brother], t->PtrToSon[p]->PtrToSon[0], 1);
				updateIndex(t->PtrToSon[brother]);//這個順序一定要在前面,因為deleteAndFreePtrToSon移動了兒子指標,所以要先更新
				deleteAndFreePtrToSon(t, p);
				
				updateIndex(t);
				return t->sonNum == 1;
			}
			else if (t->PtrToSon[brother]->sonNum == 3) {
				if (p < brother) {
					insertPtrToSon(t->PtrToSon[p], t->PtrToSon[brother]->PtrToSon[0], 0);
					deletePtrToSon(t->PtrToSon[brother], 0);
				}
				else {
					insertPtrToSon(t->PtrToSon[p], t->PtrToSon[brother]->PtrToSon[2],-1);
					deletePtrToSon(t->PtrToSon[brother], 2);
				}
				updateIndex(t->PtrToSon[p]);
				updateIndex(t->PtrToSon[brother]);
				updateIndex(t);
				return 0;
			}
			else {
				Error("what the hell?");
				return 0;
			}
		}
	}
	else if (t->type == 1) {//葉子
		int p;
		if (t->sonNum == 0)//剛建立樹的時候,空
			return 0;
		if (t->sonNum == 1) {
			p = 0;
		}
		else {
			p = getSonBranch(t, X);//選擇兒子分支
		}
		binaryDeleteForBottomNode(t->PtrToSon[p], X);
		if (t->PtrToSon[p]->sonNum == 0) {
			deleteAndFreePtrToSon(t, p);
			return 0;
		}
		else if (t->PtrToSon[p]->sonNum == 1) {
			int  brother;
			if (p > 0) {
				brother = p - 1;
			}
			else if (p + 1 < t->sonNum) {
				brother = p + 1;
			}
			else {//只有一個兒子,無兄弟
				return 0;
			}
			if (t->PtrToSon[brother]->sonNum == 3) {
				if (brother < p) {
					t->PtrToSon[p]->elem[1] = t->PtrToSon[p]->elem[0];
					t->PtrToSon[p]->elem[0] = t->PtrToSon[brother]->elem[2];
					t->PtrToSon[p]->sonNum++;
					t->PtrToSon[brother]->sonNum--;
				}
				else {
					t->PtrToSon[p]->elem[1] = t->PtrToSon[brother]->elem[0];
					for (int i = 0; i < 2; i++) {
						t->PtrToSon[brother]->elem[i] = t->PtrToSon[brother]->elem[i + 1];
					}
					t->PtrToSon[p]->sonNum++;
					t->PtrToSon[brother]->sonNum--;
				}
				updateIndex(t);
				return 0;
			}
			else if (t->PtrToSon[brother]->sonNum == 2) {
				binaryInsertForBottomNode(t->PtrToSon[brother], t->PtrToSon[p]->elem[0]);
				deleteAndFreePtrToSon(t, p);
				updateIndex(t);
				return t->sonNum == 1;
			}
			else {
				Error("what the hell?");
				return 0;
			}
		}
		else//底層節點關鍵字數目為2
			return 0;
	}
	else {
		Error("what the hell?");
		return 0;
	}
}

Btree Delete(ElementType X, Btree t) {
	if (delete_internal(X, t)) {
		if (t->type == 0) {
			Btree newRoot = t->PtrToSon[0];
			free(t);
			return newRoot;
		}
	}
	updateIndex(t);
	return t;
}






main.c

#include"btree.h"
#include<stdio.h>
#include<stdlib.h>
#define M 3
struct BtreeNode {
	int type;
	int sonNum;//兒子數量,或當前擁有資料的數量(最底層)
	PtrToNode PtrToSon[M];
	ElementType elem[M];
};

int main() {
	Btree t = createBtree();
	for (int i = 0; i < 25; i++)
		t = insert(i, t);
	Dir(t);
	printf("\n");

	for (int i = 0; i < 12; i++)
		t = Delete(i, t);


	Dir(t);
}