Data structure and algorithm learning note search

Posted by Zup on Tue, 01 Feb 2022 17:48:14 +0100

Data structure and algorithm learning notes (9) search

I Basic concepts of search

  • Lookup table

  • What is find

  • How to find

  • Purpose of search

  • Classification of lookup tables

  • Evaluation index of search algorithm

  • Research content of search process

II Lookup of linear table

1. Sequential search

Scope of application

  • Static lookup table represented by sequential table or linear linked list

  • Unordered between elements in the table

  • Representation of sequence table

    • Data element type definition

      typedef struct{
      	KeyType key; //Keyword domain
      	......	     //Other domains
      }ElemType;
      typedef struct{ //Sequence table structure type definition
      	ElemType *R;	//Table base address
      	int length;		//Table length
      	
      }SSTable; 
      SSTable ST; //Define sequence table ST
      

algorithm

basic form
int Search_Seq(SSTable ST,KeyType Key){
	//If the location information is returned successfully, otherwise 0 is returned
	for(i=ST.length;i>=1;--i)
	{
		if(ST.R[i].key==key)
			return i;
	}
	return 0;
}

Other forms of the algorithm:

  • This form of for loop needs a semicolon

Improved algorithm
  • Save the keyword key to be checked into the header and compare it one by one from the back to the front, so as to avoid checking whether the search is completed at each step in the search process and speed up the speed

    In this way, there is no need to judge whether it is out of bounds

    The loop body is empty. Don't forget the semicolon

  • When ST.length is large, this improvement can reduce the time required for a search by almost half

  • Algorithm efficiency analysis

    • Time complexity: O(n)

    • Space complexity: an auxiliary space -- O(1)

characteristic

  • advantage:

    The algorithm is simple, the logical order is not required, and different storage structures are applicable

  • Disadvantages:

    ASL is too long and time efficiency is too low

2. Half search (binary search)

  • Half find:

    Reduce the range of records to be checked by half each time

non-recursive algorithm

  • Example of search process:

  • Algorithm analysis (non recursive)

    int Search_Bin(SSTable ST, KeyType key) {
     low = 1;
     high = ST.length;		//Set interval initial value
     while(low <= high){
     	mid = (low + high)/2;
     	if(ST.R[mid].key == key) return mid;	//Find the element to be checked
     	else if(key < ST.R[mid].key) //Narrow search interval
     	{
     		high = mid -1; //Continue searching in the first half
     	}
     	else
     		low = mid + 1;	//Continue searching in the second half
     }
     	return 0; 	//There is no element to be checked in the sequence table
    }
    

recursive algorithm

int Search_Bin(SSTable ST,keyType key, int low ,int high){
	if(low>high)
		return 0;	//Return 0 not found
	mid = (low+high)/2;
	if(key==ST.elem[mid].key)
		return mid;
	else if(key<ST.elem[mid].key)
		return Search_Bin(ST,key,low,mid-1);	//First half search
	else
		return Search_Bin(ST,key,mid+1,high);	//Second half search
    return 0;
}

algorithm analysis

Decision tree

Advantages and disadvantages

  • advantage
    • Higher efficiency than sequential search
  • shortcoming
    • It is only applicable to ordered list and is limited to sequential storage structure (not applicable to linear linked list)

3. Block search (index search)

  • A very vivid example is
    • The English dictionary is divided into 26 letters

condition

performance analysis

  • Search efficiency

  • Advantages and disadvantages

4. Comparison of search methods

III Tree table lookup

  • The binary search efficiency of sequential table is high, but it is only applicable to sequential table

1. Binary sort tree

Binary sort tree definition

  • Binary sort tree is also called binary search tree and binary search tree

  • definition

    Is a recursive definition

  • example

The law of traversing binary sort tree in middle order

Binary sort tree lookup recursive algorithm

  • Storage structure of binary sort tree

    typedef struct{
    	KeyType key;		//Keyword item
    	InfoType otherinfo;	//Other data fields
    }ElemType;
    
    typedef struct BSTNode {
    	ElemType data;						//Data domain
    	struct BSTNode *lchild,*rchild;		//Left and right child pointers
    }BSTNode, *BSTree;
    
    BSTree T; //Define binary sort tree T
    
  • Algorithmic thought

  • Algorithm description

    BSTree SearchBST(BSTree T,KeyType key){
    	if((!T)||key==T->data.key) //The tree is empty or a direct return pointer was found
    		return T;
    	else if(key<T->data.key)
    		retrun SearchBST(T->lchild,key); //Continue searching in the left subtree
        else
        	return SearchBST(T->rchild,key); //Continue searching in the right subtree
    }
    
  • Find analysis

    • Average search length

    • How to improve the search efficiency of binary sort tree with unbalanced morphology

Binary sort tree operation - insert

  • The inserted element must be on the leaf node

Operation generation of binary sort tree

  • Sequences with different insertion order generate binary sort trees with different forms

Operation of binary sort tree - delete

Delete leaf node
  • Delete directly
  • Change the value of the corresponding pointer field in its parent node to null“
The deleted node has only left subtree or right subtree
  • Replace it with its left or right subtree
  • Change the value of the corresponding pointer field of its parent node to "point to the left or right subtree of the deleted node"“
The deleted node has both left and right subtrees

  • example

    Figure 3 can also be replaced with 65, but with 65, the height of the tree does not change

    You can reduce the height of the tree by changing 81

summary

2. Balanced binary tree

Balanced binary tree definition

  • Also known as AVL tree

  • The balanced binary tree first satisfies the binary sort tree

  • Balance factor

  • example

    ASL: average lookup length

Analysis and adjustment of unbalanced binary sort tree

Four types of balance adjustment

Grasp the principle of adjustment

LL type

  • example

RR type

LR type

RL type
  • example

IV Hash table lookup

1. Basic concepts

  • basic thought

    There is a corresponding relationship between the storage location of records and keywords – hash function

    Loc(i)=H(keyi)

  • example

    • How to find

2. Some terms

  • Hash method (hash method)

  • Hash function (hash function)

    Conversion function used in hash method

  • Hash table

  • conflict

    • example

  • synonym

    Multiple keywords with the same function value

3. Construction method of hash function

  • Factors considered in constructing hash function

  • Construct according to the characteristics of the element set

    • direct addressing

    • Division method

4. Methods of dealing with conflicts

Open address method

  • basic thought

    If there is a conflict, go to the next empty hash address, as long as the Hash list is large enough

    An empty hash address is always found and the data element is stored in the

  • common method

    • Linear detection method

    • Secondary detection method

      The incremental sequence is a quadratic sequence

    • Pseudo random detection method

Chain address method (zipper method)

  • Basic idea:

    Records with the same hash address are linked into a single linked list

    Set M single linked lists for M hash addresses, and then use an array to store the header pointers of m single linked lists to form a dynamic structure

  • For example:

  • Steps of establishing hash table by chain address method

  • Advantages of chain address method

5. Hash table lookup and performance analysis

  • Search process

  • example

Analysis of hash table lookup efficiency

6. Summary

Topics: Algorithm data structure