【发布时间】:2020-04-30 01:01:41
【问题描述】:
谁能帮我在二叉树中进行区间搜索。 我知道如何检查树的左侧,但我在小树的右侧遇到了麻烦。 这是我现在的代码。
private boolean search(BSTNode r, int from,int till){
boolean found = false;
int arr[];
arr=new int[10];
int i=0;
while (r != null)
{
int rval = r.getData();
if (from < rval && till >rval) {
r = r.getLeft();
arr[i]=rval;
i++;
}else
r=r.getRight();
}
return found;
}
这是完整的 BSTNode 类。 From 和 until 它是区间的范围(从
class BSTNode
{
BSTNode left, right;
int data;
/* Constructor */
public BSTNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BSTNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BSTNode n)
{
right = n;
}
/* Function to get left node */
public BSTNode getLeft()
{
return left;
}
/* Function to get right node */
public BSTNode getRight()
{
return right;
}
【问题讨论】:
-
请输入
BSTNode的代码以及从哪里到哪里? -
@am0awad 我已经更新了代码。 Soo from 和 until 基本上是由 user(sc.nextInt) 导入的整数
-
还有样本输入,以跟踪