Search ---- Binary Search

Source: Internet
Author: User

1. Binary Search

The binary search method has a very important prerequisite: that is, the sequence to be searched must be sorted.

Assuming that the element sequence is in ascending order, the keywords recorded in the intermediate position of the sequence are compared with the search keywords. If the two are the same, the search is successful; otherwise, the intermediate position record is used to divide the sequence into the first and the last two subsequences. If the keyword of the intermediate position record is greater than the search keyword, the former subsequence is further searched. Otherwise, the latter subsequence is further searched. Repeat the above process until the matching record is found, the search is successful, and the index of the element in the sequence is returned, or until the subsequence does not exist, the search fails and-1 is returned.

 

Int find2 (int * array, int n, int val)
{
If (n <= 0)
{
Return-1;
}
 
Int begin = 0, end = n-1, mid;
While (begin <= end)
{
Mid = (begin + end)/2;
If (array [mid] = val)
Return mid;
Else if (array [mid]> val)
End = mid-1;
Else
Begin = mid + 1;
}
 
Return-1;
}
2. search using the Binary Search Tree

First, create a binary search tree. We know that the Left subtree has a smaller value than the root node, and the right subtree has a larger value than the root node, in addition, the elements obtained by the middle-order traversal of the binary search tree are sorted in order.

[Html]
// Binary query tree data structure
Typedef struct Btree
{
Int data;
Btree * left;
Btree * right;
} * PBTree;
 
// Create a binary search tree and return the root node of the tree
PBTree CreateBTree (int * array, int n)
{
PBTree root = new Btree;
Root-> data = array [0];
Root-> left = NULL;
Root-> right = NULL;
 
PBTree current, back, pNew;
For (int I = 1; I <n; I ++)
{
PNew = new Btree;
PNew-> data = array [I];
PNew-> left = pNew-> right = NULL;
Current = root;
While (current! = NULL) // find the proper Insert Location
{
Back = current;
If (current-> data> array [I])
Current = current-> left;
Else
Current = current-> right;
}
If (back-> data> array [I])
Back-> left = pNew;
Else
Back-> right = pNew;
}
 
Return root;
}
 
// Uses the binary search tree for recursive search
Bool find3 (PBTree root, int val)
{
If (root = NULL)
Return false;
If (root-> data = val)
Return true;
Else if (root-> data> val)
Return find3 (root-> left, val );
Else
Return find3 (root-> right, val );
}
3. Summary

Binary Search has very strict restrictions (the sequence must be ordered );

When the binary search tree is used, the "Ordered Tree" is automatically created (the sequential traversal result is ordered );

The establishment time of the binary search tree is not considered. The efficiency of the two trees is the same. They are all O (logn ).

Related Article

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.