Find (i): Binary lookup and two-fork find tree

Source: Internet
Author: User
Tags comparable

Two-point Search

The binary lookup principle is simple:
In an ordered array (this article discusses ascending, descending)

Starting from the element in the middle of the array, if A[MID] is larger than the found element key, then find it in a[0] to a[mid-1] and vice versa in a[mid++] to a[a.lenth-1].

From this point of view, the meaning of recursion is very strong ah, of course, can also be used in a non-recursive way, more efficient, meaning that binary lookup is relatively simple, directly on the code:

Define a lookup abstract base class:

 Public Abstract class searchbase {    public integer[] A = {0, 1, 2, 3, 4, 5, 6, 7, 8};          // Public character[] A = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I '};         Public abstract <T> Integer search (comparable<t> key);}

Two-point lookup code:

 Public classBinarySearchextendsSearchbase {/*(non-javadoc) * @see search.searchbase#search (java.lang.Comparable)*/@SuppressWarnings ("Unchecked") @Override Public<T> Integer Search (comparable<t>key) {        //TODO auto-generated Method StubInteger Low = 0; Integer High= A.length-1; Integer Mid= (low + high)/2;  while(Low <=High ) {            if(Key.compareto ((T) a[mid]) = = 0) {                returnmid; } Else if(Key.compareto ((T) a[mid]) > 0) { low= ++mid; } Else if(Key.compareto ((T) A[mid]) < 0) { high= --mid; } Mid= (low + high)/2; }                return-1; } @SuppressWarnings ("Unchecked")     Public<T> Integer searchrecursion (comparable<t>Key,integer Low,integer High) {        if(Low >High )return-1; Integer Mid= (low + high)/2; if(Key.compareto ((T) a[mid]) > 0)            returnSearchrecursion (key,++Mid,high); Else if(Key.compareto ((T) A[mid]) < 0)            returnSearchrecursion (Key,low,--mid); Else            returnmid; }         Public Static voidMain (string[] args) {BinarySearch BinarySearch=NewBinarySearch (); System.out.println (Binarysearch.searchrecursion (0,0,binarysearch.a.length-1)); }    }

For the lookup, another two indicators are more important, one is the efficiency of inserting new elements, one is the efficiency of the search:

Binary search average insertion efficiency: O (N/2)

Binary find average search efficiency: O (LgN)

Binary search Tree

Binary search tree Simply put, it is a binary tree:

Build process:

left dial hand tree all nodes small root node, right subtree all nodes greater than equals root node (can in turn, define themselves)

Lookup process:

the middle sequence traversal , if found on the return.

in fact, binary search tree can also be used to sort, and heap sort very much like .

When constructing a binary search tree, the shape of the tree is very important and directly affects the efficiency of the search, such as:

As the leftmost graph, the best case is a fully binary tree , so the search is the most efficient.

As the rightmost graph, the worst-case scenario degrades into a linked list , which is the least efficient.

Directly on the code implementation:

 Public classBinarysearchtreeextendsSearchbase {classNode<t> {                 PublicNode (comparable<t> value,node<t> leftchile,node<t>rightchild) {             This. Value =value;  This. Leftchild =Leftchile;  This. Rightchild =Rightchild; } Comparable<T> value =NULL; Node<T> Leftchild =NULL; Node<T> Rightchild =NULL; } @Override Public<T> Integer Search (comparable<t>key) {        //TODO auto-generated Method Stub        return NULL; } @SuppressWarnings ("Unchecked")     Public<T> T Search (comparable<t> key,node<t>root) {        //TODO auto-generated Method Stubnode<t> node =Root;  while(Node! =NULL) {            if(Key.compareto ((T) Node.value) < 0) {node=Node.leftchild; } Else if(Key.compareto ((T) node.value) > 0) {node=Node.rightchild; } Else {                 Break; }        }                if(node = =NULL)            return NULL; Else             return(T) Node.value; }    //add an element to the tree@SuppressWarnings ("Unchecked")     Public<T> node<t> addtree (comparable<t> value,node<t>node) {        if(node = =NULL)            return NewNode<t> (Value,NULL,NULL); if(Node.value.compareTo ((T) value) > 0) Node.leftchild=Addtree (value, node.leftchild); ElseNode.rightchild=Addtree (value, node.rightchild); returnnode; }            //traversing a tree, outputting an ordered sequence     Public<T>voidTraverseTree (node<t>node) {        if(node = =NULL)            return;        TraverseTree (Node.leftchild);        System.out.print (Node.value);    TraverseTree (Node.rightchild); }         Public Static voidMain (string[] args) {Binarysearchtree binarysearchtree=NewBinarysearchtree (); Integer[] B= {1,4,2,6,7,0,3}; Binarysearchtree.node<Integer> root = Binarysearchtree.NewNode<integer> (B[0],NULL,NULL);  for(inti=1;i<b.length;i++) {root=Binarysearchtree.addtree (b[i],root);        } binarysearchtree.traversetree (root);                System.out.println (); Integer result= Binarysearchtree.search (1, Root); System.out.println ("Result:" +result); }}

Binary search tree average insertion efficiency: 1.39LgN

Binary search Tree Average search efficiency: 1.39LgN

Find (i): Binary lookup and two-fork find tree

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.