我想知道为什么我在此代码中使用方法containsData
和containsData2
获得相同的结果。
package rr.fe.op.lab.proc;
class TreeNode {
TreeNode left;
TreeNode right;
String data;
}
class TreeProgram {
public static void main(String[] args) {
TreeNode node = null;
node = insert(node, "Han");
node = insert(node, "Luke");
node = insert(node, "Leia");
node = insert(node, "Padme");
node = insert(node, "Vader");
node = insert(node, "Yoda");
System.out.println("Writing tree inorder:");
writeTree(node);
node = reverseTreeOrder(node);
System.out.println("Writing reversed tree inorder:");
writeTree(node);
int size = sizeOfTree(node);
System.out.println("Number of nodes in tree is "+size+".");
boolean found = containsData(node, "Padme");
System.out.println("Searched element is found: "+found);
boolean found1 = containsData2(node, "Padme");
System.out.println("Searched element is found: "+found);
}
static boolean containsData(TreeNode treeRoot, String data) {
if(treeRoot == null)
return false;
else if(data.compareTo(treeRoot.data) == 0)
return true;
else if(data.compareTo(treeRoot.data) < 1)
return containsData(treeRoot.left,data);
else
return containsData(treeRoot.right,data);
}
static int sizeOfTree(TreeNode treeRoot) {
if(treeRoot == null)
return 0;
else
return 1 + sizeOfTree(treeRoot.right) + sizeOfTree(treeRoot.left);
}
static TreeNode insert(TreeNode treeRoot, String data) {
if(treeRoot == null){
TreeNode newnode = new TreeNode();
newnode.data = data;
newnode.left = null;
newnode.right = null;
return newnode;
}
else if (data.compareTo(treeRoot.data) < 1)
treeRoot.left = insert(treeRoot.left,data);
else
treeRoot.right = insert(treeRoot.right,data);
return treeRoot;
}
static void writeTree(TreeNode treeRoot) {
if(treeRoot != null){
writeTree(treeRoot.left);
System.out.println(treeRoot.data);
writeTree(treeRoot.right);
}
}
static TreeNode reverseTreeOrder(TreeNode treeRoot) {
if(treeRoot == null)
return null;
TreeNode node = new TreeNode();
node = treeRoot.left;
treeRoot.left = treeRoot.right;
treeRoot.right = node;
reverseTreeOrder(treeRoot.left);
reverseTreeOrder(treeRoot.right);
return treeRoot;
}
static boolean containsData2(TreeNode treeRoot,String data){
if (treeRoot == null) {
return false;
}
if (treeRoot.data == data){
return true;
} else {
return containsData2(treeRoot.left,data) || containsData2(treeRoot.right,data);
}
}
}
我知道在反转树之前,方法containsData
工作正常。当我倒转树时,它无法正常工作。我编写了一个方法containsData2
,并认为该方法可以找到树是否反转的元素。当然,复杂性会更高。但是,使用containsData2
,我得到与containsData
相同的结果,即false
。我做错了什么?
答案 0 :(得分:2)
主要问题是你在print语句中输入了错误的变量:
boolean found1 = containsData2(node, "Padme");
System.out.println("Searched element is found: "+found);
这应该是:
boolean found1 = containsData2(node, "Padme");
System.out.println("Searched element is found: "+found1);
另一个重要问题是,您正在尝试使用==
来比较字符串,这通常不会提供您想要的结果。在这种特殊情况下它可以工作,因为你只使用文字字符串。比较在这里完成:
if (treeRoot.data == data){
return true;
} else {
return containsData2(treeRoot.left,data) || containsData2(treeRoot.right,data);
}
相反,请使用equals
方法比较您的字符串,如下所示:
if (treeRoot.data.equals(data)){
return true;
} else {
return containsData2(treeRoot.left,data) || containsData2(treeRoot.right,data);
}
或者,如果您想进一步简化代码:
return treeRoot.data.equals(data) ||
containsData2(treeRoot.left,data) || containsData2(treeRoot.right,data);
有关比较字符串的详细信息,请参阅this question。
答案 1 :(得分:1)
你误解了递归。你现在在做什么,搜索树:
<韩寒(他不是帕德梅,让我们挖掘更多) - &gt;最后,你得到&#34;错误&#34; boolean,&#34;这不是您要查找的节点?&#34;
您应该尝试找到解决此问题的另一种方法。 目前,当你找到一个合适的节点时,你应该尝试打破递归。最简单的方法是创建一个全局变量,然后设置
done = true;
找到Padme时,然后在打印结果时: System.out.println(&#34;找到搜索元素:&#34; +完成);