2013-03-31 4 views
0

AVLNode 및 AVLTree 클래스가 있고 노드를 제거하고 삽입하는 메서드가 있고 인쇄 메서드가 있습니다. 이러한 메서드를 사용하여 AVL 트리를 만들고 싶습니다. 입력에 나는 "x를 추가하십시오"와 "x를 제거하십시오"쓰고 싶다. 내가 쓴하지만 난 아무것도 인쇄하지 않을 때Java에서 AVL 트리 사용

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(); 

} 

답변

2

변경 보여줍니다

if (words[i].equals("Add")) 

와 유사하게 "Remove" 케이스 :

if (words[i] == "Add") 

에 있습니다. equals 메서드는 문자열을 문자별로 비교하지만 == 연산자는 두 개의 문자열이에있는 같은 개체인지 확인합니다. 그래서, 아무 것도 인쇄되지 않는 이유는 아무 것도 추가되거나 제거되지 않는다는 것입니다!

+0

아, 감사합니다! – 10001a

+2

' "덧셈".equals (words [i])'가 더 좋습니다 ('NullPointerException'의 위험이 없습니다) –