[英]Using an AVL tree in java
我有AVLNode和AVLTree類,我有刪除和插入節點的方法,我有一個打印方法。 我想使用這些方法來創建AVL樹。 在輸入時我想寫“添加x”和“刪除x”。 我寫了這個,但是當我打印什么節目
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();
}
更改:
if (words[i] == "Add")
至:
if (words[i].equals("Add"))
同樣對於"Remove"
案例。 equals
方法將逐個字符地比較字符串,但==
運算符只檢查兩個字符串是否是內存中的同一個對象 。 因此,沒有打印的原因是首先沒有添加或刪除任何內容!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.