2011-11-01 5 views
0

나는 정렬되지 않은 숫자 목록이있는 txt 파일이 있습니다. 번호 목록을 가져 와서 배열을 만들었습니다. 그래서 지금 나는 그 숫자들로 나무를 만들고 그것들을 왼쪽과 오른쪽에 기초하여 정렬하려고 노력하고 있습니다. 지금은 인쇄하지만 정렬은 안됩니다. 나는 나무가 바르게 만들어지지 않고 있다고 확신하지만 그것을 고치는 방법을 모르겠습니다. 나는 또한 모든 중복을 추적하고 싶다. 다른 말로하면 나는 어떤 복제물도 인쇄하고 싶지는 않지만 그 대신에 얼마나 많은 사본이 있는지 추적 할 수 있습니다. 도움이나 조언을 주시면 감사하겠습니다. 미리 감사드립니다. - 메서드 배열에 숫자 배열을 전달합니다. dealArray()는 int로 변환합니다. 거기에서 그 #가 findDuplicate()에 전달되었는데, 나는 그것을 수행해야하는지 확신 할 수 없다.트리 만들기 및 숫자 정렬

BigTree 등급 :

 public class bigTree { 
int data; int frequency; 
bigTree Left, Right; 


public bigTree makeTree(int x) { 
    bigTree p; 
    p = new bigTree(); 
    p.data = x; 
    p.Left = null; 
    p.Right = null; 
    return p; 
} 


public void setLeft(bigTree t, int x) { 

    if (t.Left != null) { 
     System.out.println("Error"); 
    } 
    else { 
     bigTree q; 
     q = t.Left; 
     q = makeTree(x); 
    } 

} 


public void setRight(bigTree t, int x) { 

    if (t.Right != null) { 
     System.out.println("Error"); 
    } else { 
     bigTree q; 
     q = t.Right; 
     q = makeTree(x); 
    } 
} 

public void findDuplicate(int number) { 
    bigTree tree, p, q; 
    frequency = 0; 

    tree = makeTree(number); 
     //while (//there are still #'s in the list) { //1st while() 
      p = tree; 
      q = tree; 

      while (number != p.data && q != null) { //2nd while() 
       p = q; 

       if (number < p.data) { 
        q = q.Left; 
       } else { 
        q = q.Right; 
       } 
      } //end of 2nd while() 

      if (number == p.data) { 
       Sort(p); 
       //System.out.println(p.data); 
       frequency++; 
      } 
      else { 
       if (number < p.data) { 
        setLeft(p,number); 
       } 
       else { 
        setRight(p, number); 
       } 

      } 
    //} // End of 1st while() 

} 

public void Sort(bigTree t) { 
    if (t.Left != null) { 
     Sort(t.Left); 
    } 
    System.out.println(t.data); 
    if (t.Right != null) { 
     Sort(t.Right); 
    } 
    //Possible print 
    } 

public void dealArray(String[] x) { 
    int convert; 
    for (int i = 0; i < x.length; i++){ 
     convert = Integer.parseInt(x[i]); 

     findDuplicate(convert); 
     // System.out.println(x[i]); 
    } 
} 
+0

'코드에서 Dumbass'를 int로 다시 변경 // ?? –

+0

오, 오 하하, 이전에 어떤 이유로 나는 이름이 아닌 숫자의 목록이라고 생각하여 모든 것을 문자열로 만들었습니다. 미안한데 그 – TMan

+2

이제 몇 분 동안 귀하의 코드를 들여다 보았습니다. 그러나 나는 당신이 성취하고자하는 것을 얻지 못한다고 고백해야합니다. 예를 들어'sort' 메쏘드 (소문자 메쏘드/필드 이름을 사용하고 대문자 클래스 이름을 사용하십시오)는 트리 인쇄를 제외하고는 아무것도하지 않습니다. 그리고'findDuplicate' 안에는 감각적이지 않은 많은 것을합니다 (새로운 트리를 만들고 그것을 가로 지르며, 거의 비어 있습니다). 코드를 정리하고 알고리즘을 작동 시키십시오 (적어도 코드 내부에 있지 않으면 머리를 쓰십시오). – Howard

답변

1

오히려 미리 중복을 발견하여 트리를 구축하는 동안 그들을 발견하는 것보다.

귀하의 나무는 아마도 다음과 같은 노드로 구성해야합니다

public class Node { 
    public int number; 
    public int count; 
    public Node left; 
    public Node right; 
} 

이진 검색 트리를 구축 (백만 튜토리얼이에있다). 새 요소를 추가하는 동안 해당 요소가 이미 있으면 해당 노드의 개수를 증가시킵니다.

그런 다음 인쇄 예약 주문 탐색의 문제이다

+0

그게 내가 findDuplicate()에서하고있는 것이 아니다. – TMan

+0

@Tman : findDuplicate() 메소드가 필요하다. 아키텍처를 변경하면 문제가 극적으로 단순해진다. – Kane

1

이 작업을 수행하는 방법에 대한 힌트 아래의 구현을 사용 (다시 말하지만,이에 백만 튜토리얼이있다). 죄송합니다. 현재 코드를 포맷하고 불필요한 메소드를 제거 할 수 없습니다.

import java.util.*; 

public class TreeNode implements Iterator , Iterable{ 

public Object value; 

public TreeNode prev; 

public TreeNode left; 

public TreeNode right; 



private TreeNode w; 



public TreeNode(){ 

} 

public TreeNode(Object value){ 

    this.value= value; 

} 



public Object[] toArray(){ 

    Object[] a= new Object[this.size()]; 

    int i= 0; 

    for(Object o : this){ 

     a[i]= o; 

     i++; 

    } 

    return a; 

} 



public TreeNode delete(Object o){ 

    TreeNode z= this.findNode(o); 

    if(z==null){ 

     return this; 

    } 



    if(z.left != null && z.right != null){ 

     TreeNode z1= z.right.minNode(); 

     z.value= z1.value; 

     z= z1; 

    } 



    if(z.left == null && z.right == null){ 

     TreeNode y= z.prev; 

     if(y==null){ 

      return null; 

     } 

     if(y.right == z){ 

      y.right= null; 

     }else{ 

      y.left= null; 

     } 

     return this; 

    } 





    if(z.left == null || z.right == null){ 

     TreeNode y; 

     if(z.left != null){ 

      y= z.left; 

     }else{ 

      y= z.right; 

     } 

     if(z.prev == null){ 

      y.prev= null; 

      return y; 

     }else{ 

      y.prev= z.prev; 

      if(z.prev.left == z){ 

       z.prev.left= y; 

      }else{ 

       z.prev.right= y; 

      } 

     } 

    } 

    return this; 

} 



public boolean hasNext(){ 

    return w != null; 

} 



public Object next(){ 

    Object d= w.value; 

    w= w.nextNode(); 

    return d; 

} 



public void remove(){ 

} 



public Iterator iterator(){ 

    w= this.minNode(); 

    return this; 

} 



public Object min(){ 

    if(this.left == null){ 

     return this.value; 

    } 

    return this.left.min(); 

} 



public TreeNode minNode(){ 

    if(this.left == null){ 

     return this; 

    } 

    return this.left.minNode(); 

} 



public Object max(){ 

    if(this.right == null){ 

     return this.value; 

    } 

    return this.right.max(); 

} 



public void print(){ 

    System.out.println(this.value); 

    if(left != null){ 

     this.left.print(); 

    } 

    if(right != null){ 

     this.right.print(); 

    }  

} 



public void printSort(){ 

    if(left != null){ 

     this.left.printSort(); 

    } 

    System.out.println(this.value); 

    if(right != null){ 

     this.right.printSort(); 

    }  

} 



public static String intervals(int n, int p){ 

    String s= "                 "; 

    return s.substring(0, n*p); 

} 



public void printTree(int p){ 

    printTree0(1, p); 

} 



public void printTree0(int d, int p){ 

    if(this.right != null){ 

     this.right.printTree0(d+1, p); 

    } 

    System.out.println(intervals(d, p) + this.value); 

    if(this.left != null){ 

     this.left.printTree0(d+1, p); 

    } 

} 



public boolean add(Object o){ 

    if(this.value.equals(o)){ 

     return false; 

    } 

    if(((Comparable)this.value).compareTo(o) > 0){ //left 

     if(left != null){ 

      left.add(o); 

     }else{ 

      left= new TreeNode(o); 

      left.prev= this; 

     } 

    }else{ // right 

     if(right != null){ 

      right.add(o); 

     }else{ 

      right= new TreeNode(o); 

      right.prev= this; 

     }  

    } 

    return true; 

} 



public void addBalanced(Object o){ 

    int l= rang(this.left); 

    int r= rang(this.right); 

    boolean ldir= true; 

    if(l == r){ 

     int ls= size(this.left); 

     int rs= size(this.right); 

     if(ls > rs){ 

      ldir= false; 

     } 

    }else{ 

     ldir= l <= r; 

    } 

    if(ldir){ 

     if(this.left==null){ 

      this.left= new TreeNode(o); 

     }else{ 

      this.left.addBalanced(o); 

     } 

    }else{ 

     if(this.right==null){ 

      this.right= new TreeNode(o); 

     }else{ 

      this.right.addBalanced(o); 

     }   

    }  

} 



public TreeNode nextNode(){ 

    if(this.right != null){ 

     return this.right.minNode(); 

    } 

    TreeNode t1= this; 

    TreeNode t2= this.prev; 

    while(t2!=null && t2.right==t1){ 

     t1= t2; 

     t2= t2.prev; 

    } 

    return t2; 

} 



public TreeNode findNode(Object o){ 

    if(this.value.equals(o)){ 

     return this; 

    } 

    if(((Comparable)this.value).compareTo(o) > 0){ //left 

     if(left != null){ 

      return left.findNode(o); 

     } 

    }else{ // right 

     if(right != null){ 

      return right.findNode(o); 

     }  

    } 

    return null; 

} 



    public int size(){ 

    int n= 1; 

    if(this.left != null){ 

     n= n + this.left.size(); 

    } 

    if(this.right != null){ 

     n= n + this.right.size(); 

    } 

    return n; 

} 



public static int size(TreeNode t){ 

    if(t==null){ 

     return 0; 

    } 

    return 1 + TreeNode.size(t.left) + TreeNode.size(t.right); 

} 



public int rang(){ 

    int l= 0; 

    int r= 0; 

    if(left!=null){ 

     l= left.rang(); 

    } 

    if(right!=null){ 

     r= right.rang(); 

    } 

    return 1 +((l > r) ? l : r) ; 

} 



public static int rang(TreeNode t){ 

    if(t==null){ 

     return 0; 

    } 

    int l= rang(t.left); 

    int r= rang(t.right); 

    return 1 +((l > r) ? l : r) ; 

} 



public String toString(){ 

    return "(" + value + " " + left + " " + right + ")"; 

} 

/*

public String toString(){ 

    String s= "(" + this.value + " "; 



    if(this.left == null && this.right == null){ 

     return s + ")"; 

    } 



    if(this.left==null){ 

     s= s + "()"; 

    }else{ 

     s= s + this.left; 

    } 



    if(this.right==null){ 

     s= s + "()"; 

    }else{ 

     s= s + this.right; 

    } 



    return s + ")"; 

} 

*/

}