Study Notes - Find

table of Contents

 

Sequential search

Binary search

Binary sort tree

Hash table


Sequential search

Algorithm thinking does not have to repeat them.

Success ASL = Σ (i = 1 ~ n) pi * ci, where ci is the need to find the number of comparison elements i, pi and generally 1 / n.

ASL unsuccessful = n, that is, through all the elements did not find the target keyword, the sequence that will add an element to determine whether the tail or prevent spillage, this time unsuccessful ASL = n + 1

Binary search

int Bsearch(int a[],int low,int high,int mid)
{
    int mid;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(a[mid]==key)
            return mid;
        else if(a[mid]<k)
            left=mid+1;
        else right=mid-1;
    }
    return 0;
}

Successful targeting keywords ASL = Σ layers / length of the array in the decision tree

ASL unsuccessful = Σ (number of layers leaf node +1) / the number of null pointer

Binary sort tree

Definition: If the left subtree is not empty, then the left subtree of the root node are smaller than a keyword; right subtree if not empty, then the right subtree of the root node is greater than keywords.

/*查找关键字的算法*/
BTNode BSTSearch(BTNode* bt,int key)
{
    if(bt==NULL)
        return NULL;
    else
    {
        if(bt->data==key)
            return bt;
        else if(bt->data<key)
            return BSTSearch(bt->lchild,key);
        else
            return BSTSearch(bt->rchild,key);
    }
}
/*插入关键字的算法*/(只能插入在叶子节点的左右孩子处,即bt==null处)
BTNode BSTInsert(BTNode* bt,int key)
{
    if(bt==NULL)
    {
        bt=(*BTNode)malloc(sizeof(BTNode));
        bt->lchild=NULL;
        bt->rchild=NULL;
        bt->data=key;
        return 1;
    }
    else
    {
        if(bt->data==key)    //关键字已存在,插入失败
            return 0;
        else if(bt->data<key)
            return BSTSearch(bt->lchild,key);
        else
            return BSTSearch(bt->rchild,key);
    }
}
/*创建排序树*/(就是反复调用插入关键字算法)
void CreateBST(BTNode *&bt,int key[],int n)
{
    bt=NULL;
    for(int i=0;i<n;i++)
        BSTInsert(bt,key[i]);
}

Delete binary sort tree

1) If the leaf node is removed, will not damage the binary tree characteristics, can be deleted directly.

2) If you remove the non-leaf nodes, but only the left subtree right subtree or only a node, then the node is removed and let the left / right child replace the removed node. (Inherited his father's position)

3) Deleting non-leaf nodes, and the nodes are children around, this can be converted into the case 1) or 2). Specific practices: to be abridged point along the p, step to the left to go visit the children left, then go straight to the right until the rightmost node r, if r is the leaf node according to 1) to delete, or follow 2) to delete (because r is the rightmost node, it is impossible to have the right child, the child about the situation there, that does not exist, is r p precursor). Of course, you can take the first step in the right access to the right child, and then has to go to find the most left node l left, l is p successor.

Balanced binary tree

Hash table

Hash function construction method used

① direct-addressable

H(key)=a*key+b

② digital analysis

Take a number of key bits Hash address

③ square reindeer method

After taking the square intermediate key address as few as Hash key = 32,32 ^ 2 = 1024, H (32) = 2

④ except I stay

H (key) = key mod p (p is a prime number less than or equal to the maximum length of the table)

Conflict resolution

Open-addressable

① linear detection method

Hi (key) = (H (key) + i) mod m (m is the length of the table)

② square detection method

Hi(key)=(H(key)±i^2) mod m

Chain address method

Hash table is stored in each unit head pointer synonymous single list, each list is recorded hung each other synonyms.

ASL success = Σ + for each keyword you want to access how many times to perform methods of conflict resolution can be found in the hash table / number of keywords

ASL unsuccessful = Σ hash table for each unit to be accessed, and executive methods of conflict resolution in order to access how many times to address a number of empty units / Hash functions can be mapped (not to be confused long table, Hash functions can be mapped address may not be able to cover all of the addresses in the table)

Means that, if a table cell is already empty, is the primary, if not empty, it detects its successor unit, if the unit is empty successor, is two times, or continue to follow-up detection unit, until it finds an empty unit, if the probe back four times to find an empty cell, (plus a start time that visited) is five times.

Published 35 original articles · won praise 2 · Views 1386

Guess you like

Origin blog.csdn.net/weixin_41001497/article/details/103017935