写了个Java的查找二叉树,用递归做的,不用递归的还没弄出来.先贴一下.回头再研究. BTreeTest.java: public class BTreeTest{ class Node{ Node left, right; int data; Node(int newData){ left = null; right = null; data = newData; } } Node root = null; int count = 0; boolean canFind = false; pub
package com.hanqi; import java.util.*; public class Test5 { public static void main(String[] args) { // TODO 自动生成的方法存根 //数组的二分查找法 //前提:数组要排好序 //1.随机生成生成数组 Random r1 = new Random(); int[] array = new int[10]; for (int i = 0; i < array.length; i++) { /
//二分查找法.必须有前提:数组中的元素要有序. public static int halfSeach_2(int[] arr,int key){ int min,max,mid; min = ; max = arr.length-; mid = (max+min)>>; //(max+min)/2; while(arr[mid]!=key){ if(key>arr[mid]){ min = mid + ; } else if(key<arr[mid]) max = mid -
最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3:找10.则返回-4(-1-3) 实现如下: public class Sulution1 { public static void main(String[] args) { System.out.println(findBySep(2, new int[]{0,2,4,6,9})); } pub