這次做的是資料結構的乙個抽象性實驗,我選擇的是b樹的基本操作。
編譯環境是:vs 2015
btree.h
#include#include#include#define true 1
#define false 0
#define overflow -1
#define ok 1
#define error 0
#define m 20//定義階數最大值
typedef int keytype;
typedef int status;
typedef struct record;
typedef struct btnodebtnode,*btree; //b樹結點和b樹型別
typedef struct result,*resultptr; //b樹的查詢結果型別
//介面定義
void creatbtree(btree&t, int n, int m);
/*初始條件:初始化關鍵字個數n大於等於0,b樹的階數m大於3小於等於20
操作結果:構建一顆階數為m,含有n個關鍵字的b樹
*/void searchbtree(btree t, int k, result &r);
/*初始條件:樹t存在
操作結果:在m階b數t上查詢關鍵字k,返回p
*/void insertbtree(btree &t, int k, btree q, int i,int m);
/*初始條件:樹t存在
操作結果:在b樹t上結點p->pt的key[i]和key[i+1]之間插入關鍵字k
*/void deletebtree(btree p, int i, int m, btree &t);
/*初始條件:b樹上p結點存在
操作結果:刪除b樹t上結點p->pt的關鍵字k
*/void printbtree(btree t);
/*初始條件:樹t存在
操作結果:中序遍歷b樹
*/void destroybtree(btree t);
/*初始條件:樹t存在
操作結果:銷毀b樹
*/int menu();
/*輸出選擇選單
*/
operationdefine.cpp
#include "btree.h"
void creatbtree(btree &t, int n, int m)
printf("建立b樹成功!\n");}}
void printbtree(btree t)
printbtree(t->ptr[i - 1]);}}
int search(btree p, int k)
void searchbtree(btree t, int k, result &r)
}if (1 == found)
else
}void split(btree &q, int s, btree &ap)
ap->keynum = n - s;
ap->parent = q->parent;
for (i = 0; i <= n - s; i++)
q->keynum = s - 1;//q結點的前一半保留,修改keynum
}void newroot(btree &t, btree p, int x, btree ap)
void insert(btree &q, int i, int x, btree ap)
q->key[i] = x;//賦值
q->ptr[i] = ap;
if (ap != null) ap->parent = q;
q->keynum++;//關鍵字數+1
}void insertbtree(btree &t, int k, btree q, int i, int m)
else neednewroot = 1;
}}//while
if (1 == neednewroot)//t是空樹或者根結點已**為q和ap結點
newroot(t, q, x, ap);//生成含資訊(q,x,ap)的新的根結點t}}
void successor(btree &p, int i)
void remove(btree &p, int i)
p->keynum--;
}void restore(btree &p, int i, int m, btree &t)
else if (r == ap->keynum)
else
if (r > 0 && lc != null && (lc->keynum > (m - 1) / 2))
p->key[1] = ap->key[r];//父親插入到結點
p->ptr[1] = p->ptr[0];
p->ptr[0] = lc->ptr[lc->keynum];
if (null != p->ptr[0])//修改p中的子女的父結點為p
p->ptr[0]->parent = p;
ap->key[r] = lc->key[lc->keynum];//左兄弟上移到父親位置
lc->keynum--;
finished = 1;
break;
}else if (ap->keynum > r&&rc != null && (rc->keynum > (m - 1) / 2))
ap->key[r] = rc->key[1];//右兄弟上移到父親位置
rc->ptr[0] = rc->ptr[1];
for (j = 1; j < rc->keynum; j++)
rc->keynum--;
finished = 1;
break;
}r = 0;
while (ap->ptr[r] != p) r++;//重新確定p在ap子樹的位置
if (r > 0 && (ap->ptr[r - 1]->keynum <= (m - 1) / 2))
p->key[1] = ap->key[r];//父結點的關鍵字與p合併
p->ptr[1] = p->ptr[0];//從左兄弟右移乙個指標
ap->ptr[r + 1] = lc;
for (j = 1; j <= lc->keynum + p->keynum; j++)
if (p->ptr[0])
}lc->keynum = lc->keynum + p->keynum;//合併後的關鍵字個數
ap->keynum--;
pr = p;
free(pr);//釋放p結點空間
pr = null;
p = lc;
}else
for (j = 1; j <= p->keynum; j++)
rc->ptr[0] = p->ptr[0];
if (p->ptr[0])
}for (j = r; j < ap->keynum; j++)
ap->keynum--;//父結點的關鍵字個數減1
pr = p;
free(pr);//釋放p結點空間
pr = null;
p = rc;
}ap = ap->parent;
if (p->parent->keynum >= (m - 1) / 2 || (null == ap&&p->parent->keynum > 0))
else if (null == ap)
p = p->parent;}}
void deletebtree(btree p, int i, int m, btree &t)
else
}void destroybtree(btree t)
destroybtree(t->ptr[i - 1]);}}
int menu() while (choice<0||choice>6);//避免非法輸入
return choice;
}
btree_test.cpp
#include#include"btree.h"
int main()
else
break;
case 3:
printf("請輸入要插入的關鍵字k:\n");
scanf_s("%d", &k);
searchbtree(t, k, r);
insertbtree(t, k, (&r)->pt, (&r)->i, m);
printf("插入成功!\n");
break;
case 4:
printf("請輸入要刪除b樹t上的關鍵字:\n");
scanf_s("%d", &i);
searchbtree(t, i, r);
deletebtree(r.pt, r.i, m, t);
printf("刪除成功!\n");
break;
case 5:
printf("此時的b樹序列為:\n");
printbtree(t);
printf("\n");
break;
case 6:
destroybtree(t);
printf("銷毀成功!\n");
break;
default:;}}
}while (choice > 0 && choice < 7);
return 0;
}
用C語言實現程式的多型性
多型 polymorphism 一詞最初 於希臘語 polumorphos,含義是具有多種形式或形態的情形。在程式設計領域,乙個廣泛認可的定義是 一種將不同的特殊行為和單個泛化記號相關聯的能力 然而在人們的直觀感覺中,多型的含義大約等同於 同乙個方法對於不同型別的輸入引數均能做出正確的處理過程,並給...
資料結構 B樹 B 樹詳解和C語言實現
b 樹c語言實現 結點插入 什麼是b樹 為什麼要用b樹 b樹的性質 我們按照如下性質,來定義一棵非空的m階b樹 m 2,2階b樹相當於二叉平衡查詢樹 每個非葉子節點中存放若干關鍵字資料,並且有若干指向兒子節點的指標。指標數目 關鍵字數目 1 根節點有最少1個,最多m 1個關鍵字,最少2個,最多m個子...
用C語言實現哈夫曼樹
哈夫曼樹c語言實現 include include typedef struct huffmannode huffmannode typedef struct heapnode heapnode 全域性變數 int heapsize 堆大小 int num 記錄字元數量 heapnode heap ...