六、查找
顺序查找
1 2 3 4 5 6
| int Search(int a[],int n,int k){ for(int i = 1;i <= n; ++i) if(a[i] == k) return 1; return 0; }
|
折半查找(二分查找)
时间复杂度:
1 2 3 4 5 6 7 8 9 10 11 12 13
| int BSearch(int a[],int l,int r,int k){ int mid; while(l < r){ mid = (l + r) / 2; if(a[mid] == k) return mid; if(a[mid] > k) r = mid - 1; else l = mid + 1; } return 0; }
|
分块查找
时间复杂度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| typedef struct{ int key; int low,high; }indexElem; indexElem index[MaxSize];
int Block_Search(indexElem index[],int k,int n,int arr[]){ int i = 0; int j,k; while((i < n) && index[i].key < k) i++; if(i >= n){ printf("\nNot found"); return 0; } j = index[i].low; while((j <= index[i].high) && arr[j] != k) j++; if(j > index[i].high) printf("\nNot found"); else return j; }
|
二叉排序树
BST树的查找(类比一下二分查找)
思想:
首先将给定的k值与二叉排序树的根结点的关键字进行比较,若相等,则查找成功;
① 给定的k值小时BST的根结点的关键字,继续在该结点的左子树上进行查找;
② 给定的k值大于BST的根结点的关键字,继续在该结点的右子树上进行查找。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| typedef struct BTNode{ int key; struct BTNode *lchild; struct BTNode *rchild; }BTNode;
BTNode *research(BTNode *t,int k){ if(t == NULL) return NULL; else{ if(t->key == k) return t; else if(k < t->key) return research(t->lchild, k); else return research(t->rchild, k); } }
|
插入关键字算法
在插入过程中如果待插入关键字已经存在,则返回0,代表插入不成功;
如果待插入关键字不存在,则插入,并返回1,代表插入成功。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| int Insert(BTNode *t,int key){ if(t == NULL){ t = new BTNode; t->key = key; t->lchild = t->rchild = NULL; return 1; } else{ if(key == t->key) return 0; else if(key < t->key) return Insert(t->lchild, key); else return Insert(t->rchild, key); } }
|
二叉排序树的构造算法
1 2 3 4 5
| void Create(BTNode *t,int arr[],int n){ for(int i = 0; i < n; i++) Insert(t,arr[i]); }
|