顺序查找:
优点:插入快
缺点:查找效率低O(n)
最大查找长度:
平均查找长度:
二分查找:
优点:效率高O(log2n)
缺点:插入、删除效率低;查找时需要排序
最大查找长度:
平均查找长度:
索引查找(分块查找):集结了以上两种的优点,避开了其缺点
优点:插入比较容易
缺点:(预留空间会造成)空间浪费;建立索引表麻烦
平均查找长度:ASL = Lm+Ln(块长度+索引表长度)
最优查找长度:√n + 1 (此时每一块的长度为√n)
二叉排序树(二叉搜索树)
一、查找
1、查找某个值:
struct bitnode{
int data;
bitnode *lch, *rch;
}bitnode, *bitree;
bitnode* search(bitree t, int k){// 递归版
if(!t) return NULL;
else if(k < t->data) return search(t->lch, k);
else if(k > t->data) return search(t->rch, k);
else return t;
}
bitnode* search(bitree t, int k){// 循环版
while(t){
if(k > t->data) t = t->rch;
else if(k < t->data) t = t->lch;
else return t;
}
return t;
}
2、查找最大、最小值:
struct bitnode{
int data;
bitnode *lch, *rch;
}bitnode, *bitree;
bitnode* findmin(bitree t){// 查找最小值 递归版
if(!t) return NULL;
else if(!t->lch) return t;
else return findmin(t->lch);
}
bitnode* findmax(bitree t){// 查找最大值 循环版
if(t){
while(t->rch){
t = t->rch;
}
}
return t;
}
二、插入
struct bitnode{
int data;
bitnode *lch, *rch;
}bitnode, *bitree;
bitnode* insert(bitree t, int k){
if(!t){
t = new bitnode;
t->data = k;
t->lch = t->rch = NULL;
}
else if(k < t->data){
t->lch = insert(t->lch, k);
}
else if(k > t->data){
t->rch = insert(t->rch, k);
}
return t;
}
三、删除
struct bitnode{
int data;
bitnode *lch, *rch;
}bitnode, *bitree;
// 课本算法9.7&&9.8
bitnode* Delete(bitree t, int k){
if(!t) return NULL;
else if(k < t->data){
t->lch = Delete(t->lch, k);
}
else if(k > t->data){
t->rch = Delete(t->rch, k);
}
else{// k == t->data
bitnode *tmp;
if(t->lch && t->rch){// 有两棵子树
tmp = findmax(t->lch);// 前面的查找最大值函数
t->data = tmp->data;
Delete(t->lch, tmp->data);
}
else {// 有一棵子树或没有子树(为叶子结点)
// 一棵子树已经包括为叶子结点的情况~
tmp = t;
if(!t->lch) t = t->rch;
else if(!t->rch) t = t->lch;
}
free(tmp);
}
return t;
}
平衡二叉树
ll、lr、rr、rl
对ll:右旋 lr
P242、243是B-树
哈希查找
希望ASL = 0
对数字的关键字的构造方法:除留余数法(其他不重要)