Binary lookup/Two-point lookup and three traversal modes for two-fork trees

Source: Internet
Author: User

Finding linear lookups with two points

1. Two points Search

PublicClassBinarySearch {/*** Two-point search *@paramData *@return*/Publicint BinarySearch (long[] Data,LongN) {//Left and right endint left =0;int right =data.length-1;//Intermediate elementint Mid=-1;while (Left<right) {//There are two cases of 1.left = right 2. Left>right DislocationAnti-wrap Code if (mid== (left+right)/2) break;//Find the middle element ordinal mid = (left+right)/2;if (data[mid]<n) {//Left= the left end of the right side of the mid right.Mid }Elseif (data[mid]>n) {//Right= the right endpoint right at the left of mid.Mid } }Prevent dislocation from occurringif (data[right]==N) {ReturnRight }Elseif (data[left]==n) {return Left;} else if (Data[mid]==n) { Span style= "COLOR: #0000ff" >return mid;} return-1;} public static void main (string[] args) {binarysearch bs=new BinarySearch (); long[] data={1,3,5,7,9}; System.out.println (Bs.binarysearch (data, 3      

2. How a binary tree is traversed

PublicClassNode {//Right and left node right =Null; Node left =Null;//data stored Long data =long.max_value; Public node (node-right, node-left, long data) { Super(); this.right = right ; This.left = left ; this.data = data;} Public Node (long data) { this.data=data;} Public  Node () {}}                
PublicClassBinarysortedtree {//root node =Null;PublicBoolean Add (LongData) {//Wrap Node n =NewNode (data);//Determine the root nodeif (root = =Null) root =NElse{//Node current =Root//Save parent NodeCurrentwhile (current! =Null) {parent =Currentif (Data >Current.data) {//Find vacant current in the left dial hand treeCurrent.left; }Else{//Right sub-tree find empty current =Current.right; } }//The parent doesn't know if the current is the last Zuozi or right subtree.if (Parent.data < data) {//The description was gone left subtree Parent.left =N }Elseif (Parent.data > Data) {//Description at that time walked the right sub-tree Parent.right =N } }ReturnTrue; }/*** Pre-order traversal * * Iterate through the left and right*/PublicvoidINDEXNUM1 (Node temp) {//Because the output root node is always traversed before the other nodes are output and then other nodes as child tree root nodesSystem.out.println (Temp.data);//Go left first.if (temp.left!=Null) {indexNum2 (temp.left);}//Then go to the right sub-treeif (temp.right!=Null) {indexNum2 (temp.right);}Return; }/*** Middle Sequence traversal*/PublicvoidINDEXNUM2 (Node temp) {if (temp.left!=Null) {indexNum2 (temp.left);}//Zuozi go to the head and run thisSystem.out.println (Temp.data);//Then go to the right sub-treeif (temp.right!=Null) {indexNum2 (temp.right);}Return; }/*** Post-sequential traversal * * Iterate through the left and right*/PublicvoidINDEXNUM3 (Node temp) {if (temp.left!=null// go right subtree if (temp.right!=< Span style= "COLOR: #0000ff" >null) {indexNum3 (temp.right);} System.out.println (Temp.data); return;} public static void main (string[] args) {binarysortedtree bst=new Binarysortedtree (); Bst.add (10       


If you can't figure out what's going on, that's the Order of the basics:

If (with left branch)

Indexnum (Temp.left);

SYSO//Iterate the first sentence of the left branch

if (with right branch)

Indexnum (Temp.right);

This is the convenience of sequencing.

Binary lookup/Two-point lookup and three traversal modes for two-fork trees

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.