2016-10-20 1 views
0

구현하려고하지만 추가 메소드가 제대로 작동하지 않습니다.Searchtree 추가 메소드가 올바르게 작동하지 않습니다.

팁이 있습니까? 나는 그 문제가 참고 문헌과 관련이 있다고 생각한다. 나는 아직 그것을 완전히 이해하지 못했다. 고맙습니다!

public class Tree { 요소 루트; 요소 도움말; 반복자

public void add(int v){ 
    boolean wahr = true; 
    Element helper = new Element(0); 


    if(root == null){ 
     root = new Element(v); 
     help = root; // help is for the first "root" and later for geteverything 

    } 
    else { 

     helper = help; //help is the first "root"; 
     while(wahr){ 


      if(v > helper.value){ 
       helper = helper.right; 
       if(helper == null){ 
        helper = new Element(v); 
        wahr= false; 
       } 
      } 
      else { 
       helper = helper.left; 
       if(helper == null){ 
        helper= new Element(v); 
        wahr =false; 
       } 
      } 

     } 
    } 

} 

public void geteverything(Element omg) { 


    System.out.println(omg.left.value); 

    if(omg != null){ 
     System.out.print("mal gucken wie oft"); 
     System.out.println(omg.left.value); 
     geteverything(help.left); 
     System.out.print(" "); 
     System.out.print(omg.right.value); 
     geteverything(omg.right); 
    } 


} 

} 같은 나를 위해 // 그것은

+0

* a serchtree ... – Nado

답변

0

그럼 당신은 가지 방법 자바 '포인터'일을 혼합하고 있습니다.

당신이 그것을 할 수 있습니다 방법 대신
x -> y -> (data) 

:

x -> (data) <- y 

직접 helper.left/right을 설정하려고하면 다음은이 같은 '점'을합니다 Element x = Element y와 다른 개체에 개체를 설정하면 내가 정확히 기억 새로운 요소로.

+0

고마워요! 하지만 도우미는 이미 널 (null)이되어서 도우미를 설정할 수 없기 때문에 일하지 않을 것입니다. 오른쪽/왼쪽 – Nado

관련 문제