Java 二叉树包含返回后无止境的值方法循环
我有一个二叉树和一个有效的addValue()方法。我想检查树是否包含以根开始的整数值。 现在我已经尝试了多段代码,从while循环到递归。输出对我来说真的很奇怪Java 二叉树包含返回后无止境的值方法循环,java,binary-search-tree,equals,Java,Binary Search Tree,Equals,我有一个二叉树和一个有效的addValue()方法。我想检查树是否包含以根开始的整数值。 现在我已经尝试了多段代码,从while循环到递归。输出对我来说真的很奇怪 public Boolean containsValue(Integer value) throws IllegalArgumentException { if (value == null) { throw new IllegalArgumentException("Value ist NULL :(");
public Boolean containsValue(Integer value) throws IllegalArgumentException {
if (value == null) {
throw new IllegalArgumentException("Value ist NULL :(");
}
return root != null && contains(root, value);
}
private boolean contains(Node node, Integer value) {
System.out.println(node.getValue());
Node tempNode = root;
while (true) {
if (value.equals(tempNode.getValue())){
return true;
}
if (value.compareTo(tempNode.getValue()) < 0) {
if (tempNode.getLeft() == null) {
return false;
} else
tempNode = tempNode.getLeft();
} else {
if (tempNode.getRight() == null) {
return false;
} else
tempNode = tempNode.getRight();
}
}
}
测试无休止地运行,并产生如下输出:
一,
7.
6.
2.
3.
26
11
20
1.
7.
6.
2.
3.
1.
7.
6.
2.
3.
26
11
20
12
1.
7.
6.
2.
3.
26
11
20
12
-342450606
-342450606
-1945094521
-1947498669
-1955603908
-2006690011
-2104529452
-2112214429
-2142578581
-2145673805
-2146602265
-2147111222
-2147367018
为什么会这样?我使用了一种非递归方法,但它对我来说也是如此
public boolean addValue(Integer value) throws IllegalArgumentException {
// Hinweis: Löschen Sie die folgende Code-Zeile und implementieren Sie
// die AVL-Einfüge-Operation. Sie können auf die Variable root von
// AbstractSortedTreeImpl zugreifen, um die Knoten des Baumes
// entsprechend anzupassen. Die Methode addValue der
// AbstractSortedTreeImpl kann Ihnen helfen, den Umgang mit der Variable
// root besser zu verstehen.
if (value == null) {
throw new IllegalArgumentException("Value ist NULL");
}
if (root == null) {
root = new Node(value);
return true;
}
Node actNode = root;
boolean posFound = false;
while (!posFound) {
if (value.equals(actNode.getValue()))
return false;
if (value.compareTo(actNode.getValue()) < 0) {
if (actNode.getLeft() == null) {
posFound = true;
actNode.setLeft(new Node(value));
//checkAVL(actNode);
} else
actNode = actNode.getLeft();
} else {
if (actNode.getRight() == null) {
posFound = true;
actNode.setRight(new Node(value));
//checkAVL(actNode);
} else
actNode = actNode.getRight();
}
}
return true;
}
public boolean addValue(整数值)抛出IllegalArgumentException{
//Hinweis:Löschen是一个折叠式代码集和实现者
//这是一个简单的操作。你可以使用变量根von
//简单的祖格里芬,嗯,是鲍姆斯家族的成员
//entsprechend anzupassen.Die方法附加值der
//抽象分类简化kann Ihnen helfen,den Umgang mit der变量
//根贝塞尔祖维斯特恩。
如果(值==null){
抛出新的IllegalArgumentException(“值为NULL”);
}
if(root==null){
根=新节点(值);
返回true;
}
节点actNode=根;
布尔posFound=false;
而(!posFound){
if(value.equals(actNode.getValue()))
返回false;
if(value.compareTo(actNode.getValue())<0){
if(actNode.getLeft()==null){
posFound=true;
setLeft(新节点(值));
//checkAVL(actNode);
}否则
actNode=actNode.getLeft();
}否则{
if(actNode.getRight()==null){
posFound=true;
setRight(新节点(值));
//checkAVL(actNode);
}否则
actNode=actNode.getRight();
}
}
返回true;
}
在“包含逻辑”中,似乎错误,您还应该在“结束逻辑”中添加空检查。对于二进制搜索,如果值较小,则必须将目标指向左侧;如果值较大,则必须将目标指向右侧
// Base Cases: root is null or key is present at root
if (root==null || root.key==key)
return root;
// val is greater than root's key
if (root.key > key)
return search(root.left, key);
// val is less than root's key
return search(root.right, key);
从我自己的实现中迭代
public boolean search(V value) {
TreeNode<V> localRoot = root;
TreeNode<V> node = search(localRoot, value);
if (node != null) {
return true;
}
return false;
}
private TreeNode<V> search(TreeNode<V> root, V value) {
int cmp;
while (root != null) {
cmp = getCompareValue(value, root.getValue());
if (cmp == 0) {
return root;
} else if (cmp < 0) {
root = root.getLeftChield();
} else {
root = root.getRightChield();
}
}
return null;
}
公共布尔搜索(V值){
TreeNode localRoot=root;
TreeNode节点=搜索(localRoot,值);
如果(节点!=null){
返回true;
}
返回false;
}
私有树节点搜索(树节点根,V值){
int-cmp;
while(root!=null){
cmp=getCompareValue(value,root.getValue());
如果(cmp==0){
返回根;
}否则如果(cmp<0){
root=root.getLeftChield();
}否则{
root=root.getRightChield();
}
}
返回null;
}
很难说没有看到二叉树的实现。也许addValue不能正常工作。您还需要显示节点的代码,我可以理解为什么在负值之前将代码增加到12
。您看到的负值可能是二叉树实现中的错误。首先,请更改与方法名相同的变量名。(contains)。二叉树是一个有序的树,那么为什么要在contains()
@Debu二叉树中同时搜索左右节点呢?不必排序,这只意味着每个节点最多有两个子节点。你说得对,我的逻辑似乎错了。我将添加我以前的方法,该方法与您的方法类似,但产生相同的输出问题仍然相同,即使找到了节点,输出仍将继续:/请从方法返回节点并检查null。布尔值似乎太复杂。
public boolean search(V value) {
TreeNode<V> localRoot = root;
TreeNode<V> node = search(localRoot, value);
if (node != null) {
return true;
}
return false;
}
private TreeNode<V> search(TreeNode<V> root, V value) {
int cmp;
while (root != null) {
cmp = getCompareValue(value, root.getValue());
if (cmp == 0) {
return root;
} else if (cmp < 0) {
root = root.getLeftChield();
} else {
root = root.getRightChield();
}
}
return null;
}