一道关于递归运用二叉查找法的JAVA题目

我的代码为:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassBinarySe... 我的代码为:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BinarySearchSuper extends JApplet implements ActionListener
{
JLabel enterLabel,resultLabel;
JTextField enterField,resultField;

int array[];

public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
enterLabel=new JLabel("Enter integer search key");
container.add(enterLabel);

enterField=new JTextField(10);
container.add(enterField);
enterField.addActionListener(this);

resultLabel=new JLabel("Result");
container.add(resultLabel);

resultField=new JTextField(20);
resultField.setEditable(false);
container.add(resultField);

array=new int[15];
for(int counter=0;counter<array.length;counter++)
array[counter]=2*counter;
}
public void actionPerformed(ActionEvent actionEvent)
{
String searchKey=actionEvent.getActionCommand();
int element=binarySearch(array,0,array.length-1,Integer.parseInt(searchKey));
if(element!=-1)
resultField.setText("Found value in element"+element);
else
resultField.setText("Value not found");
}
public int binarySearch(int array2[],int low,int high,int key)
{
int middle;
if(low<high)
{
middle=(low+high)/2;
if(key==array2[middle])
return middle;
else if(key<array2[middle])
binarySearch(array2,low,middle-1,key);
else
binarySearch(array2,middle+1,high,key);
}
return -1;

}
}
为什么运行以后 无论我输入什么数字它都显示未找到数据呢?
是不是我的查找方法有问题?
在递归调用的时候是不是存在什么漏洞?
请高人指点...
展开
 我来答
撒白风05F
2008-11-18 · TA获得超过1010个赞
知道小有建树答主
回答量:735
采纳率:0%
帮助的人:509万
展开全部
//不应该去改递归的返回值,只能在递归中找出答案
package com.image;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BinarySearchSuper extends JApplet implements ActionListener {
JLabel enterLabel, resultLabel;

JTextField enterField, resultField;

int array[];
int flag = -1;
public void init() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
enterLabel = new JLabel("Enter integer search key");
container.add(enterLabel);

enterField = new JTextField(10);
container.add(enterField);
enterField.addActionListener(this);

resultLabel = new JLabel("Result");
container.add(resultLabel);

resultField = new JTextField(20);
resultField.setEditable(false);
container.add(resultField);

array = new int[15];
for (int counter = 0; counter < array.length; counter++)
array[counter] = 2 * counter;
}

public void actionPerformed(ActionEvent actionEvent) {
String searchKey = actionEvent.getActionCommand();
binarySearch(array, 0, array.length - 1, Integer
.parseInt(searchKey));

if (flag != -1)
resultField.setText("Found value in element" + flag);
else
resultField.setText("Value not found");
}

public void binarySearch(int array2[], int low, int high, int key) {
flag = -1;
if (low < high) {
System.out.println(low+"............"+high);
int middle = (low + high) / 2;
if (key == array2[middle])
flag = middle;
else if (key < array2[middle])
binarySearch(array2, low, middle - 1, key);
else
binarySearch(array2, middle + 1, high, key);
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式