Two-point search and decision tree

Source: Internet
Author: User

Binary search is a relatively high efficiency search algorithm, but it relies on the array of ordered storage, the binary search process can be described by a binary tree: the current search interval in the middle of the node as the root, Zoozi and the right child table nodes as the root node of the Saozi right subtree. The resulting two-fork tree, called the description of the binary search treedecision Tree (decision trees) or comparison tree (comprision trees). The time complexity is O (logn).
the shape of the decision tree is only related to the number of table nodes n, regardless of the specific value.

The decision tree for the 10 nodes is as follows:


For this diagram, I can conclude that:
Minimum number of successful lookups: 1
Maximum number of successful lookups: 4
Average number of successful lookups: (1*1+2*2+3*4+4*3)/(1+2+4+3) =2.9=3 times;
Minimum number of unsuccessful lookups: 3
Maximum number of unsuccessful lookups: 4
Average number of unsuccessful lookups: (3*5+4*6)/(5+6) =39/11=4 times;
A binary lookup is a comparison between a given value K and a keyword that finds the root node of a decision tree. if equal, success; the root node is located on the left side of the roots node, and is larger than the root node. It is an ordered binary tree with a serial number n.
advantages and disadvantages of two-point search
Although binary lookups are highly efficient, you want to sort the tables by keyword. And the sort itself is a time-consuming operation.even the use of efficient sorting algorithms will cost O (NLGN) time. Binary lookup only applies to storage structures that are stored sequentially. To maintain the order of the table, it is necessary to move a large number of nodes to insert and delete in the sequential structure.

As a result, binary search is especially useful for linear tables that are rarely modified and often need to be found once created . For those linear tables that find little and often need to be changed, a chain list can be used as a storage structure for sequential lookups. but a linked list cannot be a binary lookup.

Binary Lookup Tree Code implementation:

#include <iostream> #include <list> #include <algorithm>using namespace  std;int two_find (int * Data,int n,int findnum) {int high=n-1;int low=0;int mid;while (low<=high) {mid= (Low+high)/2;if (data[mid]==findnum)/ /found; {return findnum;} else//not found; {if (FindNum < Data[mid])//The number to find is on the left side of the root node; high=mid-1;elselow=mid+1;}    } return-1;} int main () {int data[10]={1,2,3,4,5,6,7,8,9,11};int number=two_find (data,10,12); if (number<0) {cout<< "NO Exist "<<ENDL;} else{cout<<number<< "is exist!" <<endl;} System ("pause"); return 0;}



Two-point search and decision tree

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.