2017-04-09 1 views
0

"PrintIt"응용 프로그램을 작성하여 "51850 Kianna Squares, Terre Haute | 552.531.3674"형식의 데이터 파일을로드하라는 메시지가 표시됩니다. Gislason Kenna "를 BST로 보낸 다음 BST를 탐색하고 이름순으로 전화 목록을 인쇄하십시오. 나는 일반 BST에 대한 코드를 받았고 현재 형식으로 전체 행을 저장하는 자체 Record 데이터 형식을 만들었습니다. 저는 라인을 파싱하고 getName() 메소드의 라인에서 이름을 추출하는 메소드를 만들었습니다. 내 BST가 getName() 메소드를 사용하여 각 노드가 이름을 비교하여 삽입되고 이름의 사전 순으로 인쇄되도록하는 방법을 알아내는 데 어려움이 있습니다.내 데이터 형식과 그 방법으로 채워진 이진 검색 트리를 인쇄하는 방법

그런 다음 20 개의 다른 이름으로 된 20 개의 항목 쿼리 파일을 읽도록 요청 받았습니다. 그런 다음 쿼리 파일의 각 이름을 비교하고 같은 이름의 Record 개체를 검색해야합니다. 레코드 개체는 "51850 Kianna Squares, Terre Haute | 552.531.3674 | Gislason Kenna"의 원래 형식으로 전체 라인을 보유하므로 각 이름을 레코드 개체의 getName() 메서드와 비교해야하지만 다시 수고. 검색 및 정렬을 위해 getName() 메소드를 구현하는 바이너리 검색 트리를 얻으려면 어떻게해야합니까?

내 레코드 클래스 :

public class NodeInfo implements Comparable<NodeInfo> 
{ 
String line; 
String name; 

public String getName(String lineTwo) 
{ 
    String split = "\\|"; 
    String segments[] = lineTwo.split(split); 
    String name = segments[segments.length -1]; 
    return name; 
} 
public void setName(String name) 
{ 
    this.name = name; 
} 
public NodeInfo(String record) 
{ 
    this.line = record; 
} 
public String getLine() 
{ 
    return line; 
} 
public int compareTo(NodeInfo other) 
{ 
    String x = this.line; 
    String y = other.line; 
    return x.compareTo(y); 
} 
public String toString() 
{ 
    return line; 
} 

} 

내 이진 검색 트리 클래스 :

public class BinarySearchTree<NodeInfo extends Comparable<? super NodeInfo>> extends BinaryTree<NodeInfo> 
{ 
public void insert (NodeInfo d) 
{ 
    if (root == null) 
     root = new BinaryTreeNode<NodeInfo> (d, null, null); 
    else 
     insert (d, root); 
} 
public void insert (NodeInfo d, BinaryTreeNode<NodeInfo> node) 
{ 
    if (d.compareTo (node.data) <= 0) 
    { 
     if (node.left == null) 
      node.left = new BinaryTreeNode<NodeInfo> (d, null, null); 
     else 
      insert (d, node.left); 
    } 
    else 
    { 
     if (node.right == null) 
      node.right = new BinaryTreeNode<NodeInfo> (d, null, null); 
     else 
      insert (d, node.right); 
    } 
} 

public BinaryTreeNode<NodeInfo> find (NodeInfo d) 
{ 
    if (root == null) 
     return null; 
    else 
     return find (d, root); 
} 
public BinaryTreeNode<NodeInfo> find (NodeInfo d, BinaryTreeNode<NodeInfo> node) 
{ 
    if (d.compareTo (node.data) == 0) 
     return node; 
    else if (d.compareTo (node.data) < 0) 
     return (node.left == null) ? null : find (d, node.left); 
    else 
     return (node.right == null) ? null : find (d, node.right); 
} 

내 이진 트리 노드 클래스 :

public class BinaryTreeNode<NodeInfo> 
{ 
NodeInfo data; 
BinaryTreeNode<NodeInfo> left; 
BinaryTreeNode<NodeInfo> right; 

public BinaryTreeNode (NodeInfo d, BinaryTreeNode<NodeInfo> l, BinaryTreeNode<NodeInfo> r) 
{ 
    data = d; 
    left = l; 
    right = r; 
} 

BinaryTreeNode<NodeInfo> getLeft() 
{ 
    return left; 
} 
BinaryTreeNode<NodeInfo> getRight() 
{ 
    return right; 
} 
} 

내 이진 트리 클래스 :

public class BinaryTree<NodeInfo> { 
BinaryTreeNode<NodeInfo> root; 

public BinaryTree() { 
    root = null; 
} 
public void visit(BinaryTreeNode<NodeInfo> node) { 
    System.out.println(node.data); 
} 
public void inOrder() { 
    inOrder(root); 
} 

public void inOrder(BinaryTreeNode<NodeInfo> node) { 
    if (node != null) { 
     inOrder(node.getLeft()); 
     visit(node); 
     inOrder(node.getRight()); 
    } 
} 
} 

그리고 마지막으로 내 주요 방법 지금까지 :

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class ReadFile { 

public static void main(String[] args) { 

    Scanner scanOne = new Scanner(System.in); 
    ArrayList<NodeInfo> holder = new ArrayList<>(); 

    try { 

     Scanner scan = new Scanner(System.in); 


     File file = new File("testdata"); 

     scan = new Scanner(file); 

     while(scan.hasNextLine()) 
     { 

      String line = scan.nextLine(); 

      NodeInfo info = new NodeInfo(line); 
      holder.add(info); 


     } 
     scan.close(); 

    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 


    BinarySearchTree<NodeInfo> directory = new BinarySearchTree<>(); 

    for(int i = 0; i < holder.size(); i++) 
    { 
     NodeInfo one = holder.get(i); 
     String line = one.getLine(); 



     directory.insert(one); 

    } 

    directory.inOrder(); 

} 
} 
+0

중요한 문제는 사용자의 BST가 * line *에 의해 정렬되어서는 안되며, 의미가없는 * name *입니다. 또한 매번 구문 분석하는 대신 처음부터 세 개의 필드를 사용하여 개체를 설정하는 것이 좋습니다. – RealSkeptic

답변

0

귀하의 BST 검색 차례로 두 항목을 비교하는 라인 속성을 사용하여 정렬을위한 nodeinfo의의 compareTo 메소드를 호출한다. 비교를 위해 속성 행 대신 속성 이름을 compareTo로 사용하십시오. 빠른 스캔을 작성하면 NodeInfo 인스턴스의 setName에 대한 호출이 발견되지 않습니다. 따라서 이름 속성을 사용하는 대신 검색 부분에 추가하거나 getTo에 대한 호출을 compareTo 메소드에 의지하십시오.

public class NodeInfo implements Comparable<NodeInfo> 
{ 
... 

public int compareTo(NodeInfo other) 
{ 
    String x = getName(this.line); 
    String y = getName(other.line); 
    return x.compareTo(y); 
} 
... 
} 
+0

정말 고마워요! 나는 당신의 조언을 사용하여 일할 수있었습니다 :) –

관련 문제