Static table lookups include: sequential table lookup, ordered table lookup, static tree table lookup, Index Table lookup
The specific principle is not described here, see Min "Data Structure".
1. Sequential Table Lookup
//sequencetablesearch.c#include <stdio.h>#include <stdlib.h>#include <string.h>typedef CharKeyType;//define keyword typestypedef struct{KeyType key;} Elemtype;//define element type//define the sequential storage structure of the static lookup tabletypedef struct{Elemtype *elem;intLength;} sstable;//This function finds the data element in the sequential table that the keyword equals key. intSearch_seq (sstable ST, KeyType key) {//If found, the function value is the position of the element in the table, otherwise 0 is returned intI st.elem[0].key = key;//No. 0 element to be a sentinel for(i = st.length;strcmp(St.elem[i].key, key); i++);//From the back forward to find returni;}
2. Order Table Lookup
An ordered table lookup can be found in several ways, including: binary lookup, Fibonacci lookup, and interpolation lookup .
Binary lookups are most common.
//binarysearch.c#include <stdio.h>#include <stdlib.h>#include <string.h>typedef CharKeyType;//define keyword typestypedef struct{KeyType key;} Elemtype;//define element type//define the sequential storage structure of the static lookup tabletypedef struct{Elemtype *elem;intLength;} sstable;//This function binary find the data element whose keyword equals key in the ordered table StintSearch_bin (sstable ST, KeyType key) {//If found, the function returns the position of the element in the table. Otherwise returns 0 intLow =1, High=st.length,mid; while(Low<=high) {mid = (low + high)/2;if(!strcmp(Key,st.elem[mid].key)) {returnMid }Else if(strcmp(Key,st.elem[mid].key) <0) {High = mid-1; }Else{Low = mid +1; } }return 0;}
3. Static Tree Table Lookup
Static tree tables can be: static optimal search tree (static Optimal search trees), suboptimal find tree (nearly Optimal search trees)
This gives the establishment of the sub-optimal search tree.
//createsecondoptimalbitree.c#include <stdio.h>#include <stdlib.h>#include <math.h>typedef CharKeyType;//define keyword typestypedef struct{KeyType key;} Elemtype;//define element typetypedef structbitnode{elemtype data;structBitnode *lchild, *rchild;} Bitnode, *bitree;//data FieldintIintMinintDw//(read this code must look at the textbook Algorithm principle description)//This function creates a suboptimal find treevoidCreatesecondoptimalbitree (Bitree T, Elemtype r[],floatSw[],intLowintHigh) {//By ordered table R[low...high] and its cumulative weight table SW (where sw[0]==0) recursively constructs suboptimal lookup treei = low; Min =ABS(Sw[high]-sw[low]); DW = Sw[high] + sw[low-1]; for(intj = low+1; J <=high; J + +) {//Select the minimum pi value if(ABS(dw-sw[j]-sw[j-1]) <min) {i = j; Min =ABS(Dw-sw[j]-sw[j-1]); }} T = (bitree)malloc(sizeof(Bitnode)); T->data = R[i];//Generate node (first generation root) if(i = = low) T->lchild = NULL;//Shora ElseCreatesecondoptimalbitree (T->lchild, R, SW, Low, I-1);//Construct left sub-tree if(i = = high) T->rchild = NULL;//Right sub-tree empty ElseCreatesecondoptimalbitree (T->rchild, R, SW, i +1, high);//Construct right sub-tree}
4. Index Table Lookup
See the textbook.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Find (a) static table lookup