Data Structure exercise 20-search algorithms

Source: Internet
Author: User
Foreword search is divided into static search and dynamic search. Static search uses algorithms to search for a sequence or whether an element exists in a set without making any changes to the sequence or set. dynamic search refers to adding or deleting elements to or from a sequence or set after searching. References: Introduction to algorithms, Wikipedia, and July's blog classified static searches include binary searches, sequential index table searches, and Fiday wedge searches (omitted ), interpolation (omitted); dynamic search includes: Binary Search Tree (Binary sorting tree), balance tree (AVL), and B. Next, we will analyze binary search, index search, binary search tree, Balance Tree, and B tree. And provide relevant code verification. Binary Search

I won't talk much about the principle. Just go to the code!

void BinarySearch(int* Data,int n,int m,int key,int* loc){if(n>=m) throw "invalid argument";if(m-n==1) { if(Data[n]==key) { *loc=n; } if(Data[m]==key) {  *loc=m; } return ;}  int i=(m-n+1)/2;    if(key>Data[i])  {    BinarySearch( Data, i+1,m,key,loc);    }  if(key<Data[i])  {  BinarySearch(Data,n,i-1,key,loc);  }  if(key==Data[i])    *loc= i;}int _tmain(int argc, _TCHAR* argv[]){int a[5]={1,3,6,8,9};    int key=8;int i=-1;int *loc=&i; BinarySearch(a,0,4, key, loc);std::cout<<*loc;return 0;}

Complexity lg (n ).

It is recommended to write a July BULL: http://blog.csdn.net/v_july_v/article/details/7093204
The premise of binary search is that the order of the array has been sorted. Baidu once had an interview: This idea is used to find the smallest absolute value in an array.

 

Search an index ordered table

The general meaning is as follows: first block the array and then extract the maximum value. Similar to data, data is cleaned and decomposed first and separated by policy. The complexity is high.

 


Binary Search Tree

This is a kind of dynamic search tree. operations include: search, deletion, precursor, and successor. Similar to binary search, but its insertion and deletion hardly affect the overall structure of the tree, that is, the maintenance cost is very small. Therefore, it is often used for data search.

The average search and deletion complexity is an order of magnitude higher than that of lg (n. However, the shape of the BST (Binary Search Tree) is not unique, and the complexity may be O (N) (worst ). To prevent this situation, we introduce two variants of Binary Trees. AVL (balanced binary tree), and RBT (Red/black tree ).

AVL

Definition of such a tree: it is an empty tree or the absolute value of the height difference between the left and right trees cannot exceed 1, and both left and right trees are a balanced binary tree (http://baike.baidu.com/view/593144.htm ).

AVL maintains BF (balance factor) <= 1 based on binary trees. In this way, the deletion efficiency will not be unstable and can be maintained at LG (n ).

To balance maintenance costs and search and deletion efficiency, a balanced binary tree emerged. It has a color, but the layers of the tree on the whole are not as evenly distributed as aVL. Many containers in C ++ are RBT.

RBT

The maintenance cost of deletion is not as high as that of AVL. This is also the cause of unstable RBT search efficiency. The red-black tree also introduces a color, but its maintenance cost is only lg (n), which is not very high.

Therefore, it is a good algorithm to keep the search efficiency stable without increasing the maintenance cost. Its search and deletion complexity can be maintained at LG (n.

B tree, B + tree

We know that the efficiency of the red and black trees is still acceptable when the data volume is small. However, when the data volume is large and the data cannot be loaded into the memory, the problem arises. If we put the data on the hard disk and introduce the so-called secondary index, the hard disk I/O is very frequent, resulting in slow speed, because the hard disk query, wait, and data transmission time are not in the same order of magnitude as the memory read time, so for file systems like database indexes... we should try our best to improve the query and deletion speed. As a result, Tree B came into being, and no matter which big cow found it. Tree B tries to keep the root node in the memory and the other nodes in the hard disk. So why is I/O reduced? Let's take a look at the definition of Tree B: Reference introduction to algorithms.

It can be seen that when the order number of B is not only 2, this can greatly reduce the height, it will reduce the number of disk access, and the height of B is fixed,: log (Ceil (M/2) (n + 1)/2 ). (Remember ).

Another tree, B +, is introduced to further reduce access to I/O. B + is improved based on B. Internal nodes do not store keyword information, and all keyword information is placed in leaf nodes. Non-terminal nodes store the index information of keywords. There are two advantages:

1. A disk can contain more keywords, reducing access to I/O;

2. The search or deletion of each node is from the root node to the leaf node, which ensures stable access efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.