Blog 6. Find the first chapter ⑦

 

A look-up table

Element Relationships: Discrete

Find index: keywords, represent data elements or records

Type: dynamic lookup table (look for the simultaneous insertion and deletion) and static lookup table

Data structures: linear table, tree table, hash table.

 

Second, based on the linear table (sequence table lists: sequential storage, chain stores: the storage structure) lookup method:

Sequential search

Advantages: the structure is suitable for sequential, chain structure.

Disadvantages: Find the average length of large, low search efficiency, not employed when n is large.

Example: sequence table algorithm

(1) data element type definitions:

typedef struct{
Keytype key;
Infotype info;
}Elemtype;

typedef struct{
Elemtype *p;
int length;
}SSTable;

p icon array:

(2) Step algorithm - Sentinel notation:

Advantages: eliminating step - "Search judged out of bounds."

int Search_Seq(SSTable ST,KeyType key){
    ST.p[0].key=key;
    for(i=ST.length;ST.p[i].key!=key;--i);
    return i;
}

Binary search

(1) algorithm steps

int Search_Bin(SSTable ST,KeyType key)
{
    low=1;
    high=ST.length;
    while(low<=high)
    {
    mid=(low+high)/2;
    if(key==ST.p[mid].key)
        return mid;    
    else if(key<ST.p[mid].key)
        high=mid-1;
    else low=mid+1;
    }
    return 0;
}    
    

 

Second, based on the tree table : binary sort tree, balanced binary tree, B- tree, B + tree

Binary sort tree : keywords than the size

 

Search algorithm:

BSTree SearchBS (BSTree T,Keytype key)
{
 if(T==NULL || key=->data.key) return T;
 else if(key<T->data.key)
 return SearchBST((T->lchild),key);
 return SearchBST((T->rchild),key);
}

插入算法:

BSTree InsertBST (BSTree &T,ElemType e)
{
    if(T==NULL)
        S=new BSTNode;
        S->data=e;
        S->lchild=S->rchild=NULL;
        T=S;
        else if(e.key=T->data.key) cout<<e.key;
//这一步是我自己看书的时候想把查找+插入结合在一起的步骤
    else if(e.key<T->data.key)
    return InsertBST((T->lchild),key);
        else if(e.key>T->data.key)
    return InserBtBST((T->rchild),key);
}

创建——从空树不断插入结点的过程

 

三、散列表(哈希)

区别:关键字和关键字地址存在函数关系。

数据结构:存储空间通常是一维数组,散列地址是数组的下标

研究问题:

1、如何构造散列函数

 

 除留余数法

该方法为最常用的构造哈希函数方法,对于散列表长为m的散列函数公式为

f(key) = key mod p (p<=m)

使用除留余数法的一个经验是,若散列表表长为m,通常p为小于或等于表长的最小质数。

2、处理冲突的方法

开放地址、链地址法

(1)散列表的查找

int SearchHash(HashTable HT,KeyType key)
{
    H0=H(key);
    if(HT[H0].key==NULLkey)
    return -1;//此时为空说明后面的元素也不能为key 
    else if(HT[H0].key==key)
    return H0;
    else
    {
        for(i=1;i<m;++i)
        {
            Hi=(H0+i)%m;
            if(HT.[Hi]==NULLkey)
            return -1;//此时为空说明后面的元素也不能为key 
            else if(HT[Hi].key==key)
            return Hi;
        }
    }
}

四、总结过去,计划未来

(1)本学期学的所有数据结构特别是树在这一章充分运用,在学习的过程中,发现了在树的递归上书写的技巧——停留在本层,逐层返回。!!!递归函数的返回值的类型尤其重要(过去容易忽略)。

(2)近期临近期末考,未来这段时间要多练习综合应用类的题目,复习以往的概念。

 

Guess you like

Origin www.cnblogs.com/gzq18/p/10964164.html