java - 我将如何搜索我的二叉树以找到目标?
问题描述
我已经在这段代码上工作了一段时间,但没有运气。我知道我需要通过这种方法传递我的树,但我不确定如何做到这一点。
我认为我的算法是正确的,但无论我输入什么,它都会一直告诉我“null”。任何反馈都会有所帮助,我认为它会一直返回 null,因为我不知道如何通过该方法传递树。
下面,我的 find2 方法应该使用我的 add2 方法创建的树来查找选定的节点。这也包括我的主要方法。
这是代码:
public boolean add2(E item) {
root = add2(root, item);
return addReturn;
}
private Node<E> add2(Node<E> localRoot, E item){
if(localRoot == null) {
//Adding item if tree is empty...
addReturn = true;
return new Node<>(item);
} else if (item.compareTo(localRoot.data) == 0) {
//Returning item if equal to root...
addReturn = false;
return localRoot;
} else if (item.compareTo(localRoot.data) < 0) {
//Adding item to right side of tree if less than the root...
localRoot.right = add(localRoot.right, item);
return localRoot;
} else {
//Adding item to left side of tree if grater than the root...
localRoot.left = add(localRoot.left, item);
return localRoot;
}
}
//wrapper method
public E find2(E target) {
return find2(root, target);
}
//recursive method
private E find2(Node<E> localRoot, E target) {
if(localRoot == null) {
return null;
}
int compResult2 = target.compareTo(localRoot.data);
if(compResult2 == 0) {
return localRoot.data;
} else if (compResult2 < 0) {
return find2(localRoot.right, target);
} else {
return find2(localRoot.left, target);
}
}
public class BinarySearchTreeTest {
public static void main(String[] args) {
//Object creation
BinarySearchTree<Integer> tree = new BinarySearchTree<>();
//Tree building...
tree.add2(50);
tree.add2(70);
tree.add2(80);
tree.add2(60);
tree.add2(30);
tree.add2(40);
tree.add2(20);
//Output tree
System.out.println("\nThe built binary search tree:");
System.out.println("\n" + tree.toString());
System.out.println("\nSearch the node with data 60: " + tree.find2(60));
System.out.println("Search the node with data 65: " + tree.find2(65));
System.out.println("Search the node with data 20: " + tree.find2(20));
System.out.println("Search the node with data 25: " + tree.find2(25));
}
}
解决方案
I have tried to my local, change in add2
method call to add2
not add
static class BinarySearchTree<Item extends Comparable> {
Node root;
boolean addReturn;
class Node {
Item data;
Node left;
Node right;
public Node(Item e) {
data = e;
}
}
public boolean add2(Item item) {
root = add2(root, item);
return addReturn;
}
private Node add2(Node localRoot, Item item) {
if (localRoot == null) {
// Adding item if tree is empty...
addReturn = true;
return new Node(item);
} else if (item.compareTo(localRoot.data) == 0) {
// Returning item if equal to root...
addReturn = false;
return localRoot;
} else if (item.compareTo(localRoot.data) < 0) {
// Adding item to right side of tree if less than the root...
localRoot.right = add2(localRoot.right, item);
return localRoot;
} else {
// Adding item to left side of tree if grater than the root...
localRoot.left = add2(localRoot.left, item);
return localRoot;
}
}
// wrapper method
public Item find2(Item target) {
return find2(root, target);
}
// recursive method
private Item find2(Node localRoot, Item target) {
if (localRoot == null) {
return null;
}
int compResult2 = target.compareTo(localRoot.data);
if (compResult2 == 0) {
return localRoot.data;
} else if (compResult2 < 0) {
return find2(localRoot.right, target);
} else {
return find2(localRoot.left, target);
}
}
}
public static void main(String[] args) {
// Object creation
BinarySearchTree<Integer> tree = new BinarySearchTree<>();
// Tree building...
tree.add2(50);
tree.add2(70);
tree.add2(80);
tree.add2(60);
tree.add2(30);
tree.add2(40);
tree.add2(20);
// Output tree
System.out.println("\nThe built binary search tree:");
System.out.println("\n" + tree.toString());
System.out.println("\nSearch the node with data 60: " + tree.find2(60));
System.out.println("Search the node with data 65: " + tree.find2(65));
System.out.println("Search the node with data 20: " + tree.find2(20));
System.out.println("Search the node with data 25: " + tree.find2(25));
}
, And gives me the output:
Search the node with data 60: 60
Search the node with data 65: null
Search the node with data 20: 20
Search the node with data 25: null
推荐阅读
- react-native - React Native Expo 不加载字体
- python - 如何在python中使用glob指定目录?
- c# - 更改字典成员的类型
- reactjs - 如何从另一个组件访问一个组件的状态?
- r - 250x250 后 R 错误中的大矩阵求逆
- sql - 哪一行用于使用连接表中的值更新父表?
- node.js - NodeJS:使用 NoSQL 数据库处理事务?
- javascript - 为什么 Sharepoint 在执行 javascript 函数后会自行刷新?
- java - “&sub”的 esapi 验证器正则表达式问题
- django - 如何在 Django 中为 TabularInline 管理表单集创建自定义模板?