[英]Using an AVL tree in java
I have the AVLNode and AVLTree classes, i have the methods to remove and insert nodes and i have a print method. 我有AVLNode和AVLTree类,我有删除和插入节点的方法,我有一个打印方法。 I want to use these methods to create a AVL tree.
我想使用这些方法来创建AVL树。 On input i want to write "Add x" and "Remove x".
在输入时我想写“添加x”和“删除x”。 I wrote this but when i print nothing shows
我写了这个,但是当我打印什么节目
public static void main(String[] args) throws IOException {
int i;
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(scanner.readLine());
String[] words = new String[n];
AVLTree<Integer> t = new AVLTree<Integer>();
for (i = 0; i < n; i++) {
String splitn = scanner.readLine();
words[i] = (splitn.split(" ")[0]);
int M = Integer.parseInt(splitn.split(" ")[1]);
if (words[i] == "Add") {
t.insert(M);
}
if (words[i] == "Remove") {
t.remove(M);
}
}
t.print();
}
Change: 更改:
if (words[i] == "Add")
to: 至:
if (words[i].equals("Add"))
And similarly for the "Remove"
case. 同样对于
"Remove"
案例。 The equals
method will compare the strings character by character, but the ==
operator just checks whether the two strings are the same object in memory . equals
方法将逐个字符地比较字符串,但==
运算符只检查两个字符串是否是内存中的同一个对象 。 So, the reason nothing prints is that nothing is being added or removed in the first place! 因此,没有打印的原因是首先没有添加或删除任何内容!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.