源**
分塊查詢
#include#define maxl 100
typedef int keytype;
typedef char infotype;
typedef struct
rectype;
void createlist(rectype r,keytype keys,int n)
}#define maxi 20
typedef struct
idxtype;
int idxsearch(idxtype i,int b,rectype r,int n,keytype k)
printf("比較%d次,在第%d塊中查詢元素%d\n",count1,low,k);
i=i[high+1].link;
printf("(2)在對應塊中順序查詢:\n");
while(i<=i[high+1].link+s-1)
printf("比較%d次,在順序表中查詢元素%d\n",count2,k);
if(i<=i[high+1].link+s-1)
return i+1;
else
return 0;
}int main()
; createlist(r,a,n);
i[0].key=14;i[0].link=0;
i[1].key=34;i[1].link=4;
i[2].key=66;i[2].link=10;
i[3].key=85;i[3].link=15;
i[4].key=100;i[4].link=20;
printf("關鍵字序列:");
for(i=0;i二叉排序樹基本運算
#include#include#include#define maxsize 100
typedef int keytype;
typedef char infotype;
typedef struct node
bstnode;
void dispbst(bstnode *b);
bool insertbst(bstnode *&bt,keytype k)
else if(k==bt->key)
return false;
else if(kkey)
return insertbst(bt->lchild,k);
else
return insertbst(bt->rchild,k);
}bstnode *createbst(keytype a,int n)
}void delete(bstnode *&p)
else if(p->lchild==null)
else delete1(p,p->lchild);
}bool deletebst(bstnode *&bt,keytype k) }}
void searchbst1(bstnode *bt,keytype k,keytype path,int i)
else }
int searchbst2(bstnode *bt,keytype k)
else if(kkey)
searchbst2(bt->lchild,k);
else
searchbst2(bt->rchild,k);
printf("%3d",bt->key);
}void dispbst(bstnode *bt) }}
keytype predt=-32767;
bool judgebst(bstnode *bt)
}void destroybst(bstnode *bt)
}int main()
,n=10;
printf("(1)建立一棵bst樹:");
printf("\n");
bt=createbst(a,n);
printf("(2)bst:");dispbst(bt);printf("\n");
printf("(3)bt%s\n",(judgebst(bt)?"是一棵bst":"不是一棵bst"));
printf("(4)查詢%d關鍵字(遞迴,順序):",k);searchbst1(bt,k,path,-1);
printf("(5)查詢%d關鍵字(非遞迴,逆序):",k);searchbst2(bt,k);
printf("\n(6)刪除操作:\n");
printf("原bst:");dispbst(bt);printf("\n");
printf("刪除結點4:");
deletebst(bt,4);dispbst(bt);printf("\n");
printf("刪除結點5:");
deletebst(bt,5);dispbst(bt);printf("\n");
printf("(7)銷毀bst\n");destroybst(bt);
system("paude");
return 0;
}
查詢 二叉排序樹
順序查詢 順序查詢又叫線性查詢,是最基本的查詢技術,它的查詢過程是 從表中第乙個記錄開始,逐個進行記錄的關鍵字和給定值比較,若某個記錄的關鍵字和給定值相等,則查詢成功,找到所查的記錄 如果知道最後乙個記錄,其關鍵字和給定值比較都不等時,則表中沒有所查的記錄,查詢不成功。順序查詢演算法實現 如下 順序...
查詢 二叉排序樹
動態查詢表 表結構本身是在查詢過程中動態生成的,即對於給定值key,若表中存在其關鍵值等於key的記錄,則查詢成功返回,否則插入關鍵字等於key的記錄。二叉排序樹或者是一顆空樹,或者是具有下列性質的二叉樹 1 若他的左子樹不為空,則左子樹上所有結點的值均小於它的根結點的值。2 若它的右子樹不空,則右...
二叉排序樹(二叉查詢樹)的基本操作
二叉排序樹的查詢屬於動態查詢的範疇,根據查詢過程中是否對錶進行修改,可以把查詢分為靜態查詢和動態查詢。動態查詢表的特點是 表結構本身是在查詢過程中動態生成的,即對於給定的key值,若表中存在其關鍵字等於key的記錄,則查詢成功並返回,否則插入關鍵字等於key的記錄。二叉排序樹或者是一顆空樹,或者是具...