2014-11-29 3 views
2

데이터 구조 및 알고리즘에 대한 연습으로 수행해야 할 큰 작업이 있으며이 트리 데이터 구조를 수정하여 사전 순으로 트리를 인쇄합니다. 그것이 거대하기 때문에 전체 업무. 나는 마지막 부분에 붙어있어 지정된 트리 데이터 구조를 수정하여 알파벳 순서로 트리를 인쇄하도록 요청합니다. 나는 며칠 동안 그 일에 매달 렸고 단순한 방법으로는 할 수 없었다. 도움이되면 감사 할 것입니다. 감사합니다. 내 의견은 어떻게 든 printTreeRecursive() 메서드를 수정해야한다는 것입니다.인쇄 트리 데이터 구조 알파벳순으로

예를 들어 현재의 데이터 구조는 다음과 같이 나무를 출력한다 :

c: d c b a 

(제 첨가 아이 마지막 인쇄). C는

경우 : 루트이며 dcba 그의 자녀

하지만이처럼 보이도록 수정하기로 메신저 :

public class SLLTree<E> implements Tree<E> { 

    // SLLNode is the implementation of the Node interface 
    class SLLNode<P> implements Node<P> { 

     // Holds the links to the needed nodes 
     SLLNode<P> parent, sibling, firstChild; 

     // Hold the data 
     P element; 

     public SLLNode(P o) { 
      element = o; 
      parent = sibling = firstChild = null; 
     } 

     public P getElement() { 
      return element; 
     } 

     public void setElement(P o) { 
      element = o; 
     } 

    } 

    protected SLLNode<E> root; 

    public SLLTree() { 
     root = null; 
    } 

    public Node<E> root() { 
     return root; 
    } 

    public Tree.Node<E> parent(Tree.Node<E> node) { 
     return ((SLLNode<E>) node).parent; 
    } 

    public int childCount(Tree.Node<E> node) { 
     SLLNode<E> tmp = ((SLLNode<E>) node).firstChild; 
     int num = 0; 
     while (tmp != null) { 
      tmp = tmp.sibling; 
      num++; 
     } 
     return num; 
    } 

    public void makeRoot(E elem) { 
     root = new SLLNode<E>(elem); 
    } 

    public Node<E> addChild(Node<E> node, E elem) { 
     SLLNode<E> tmp = new SLLNode<E>(elem); 
     SLLNode<E> curr = (SLLNode<E>) node; 
     tmp.sibling = curr.firstChild; 
     curr.firstChild = tmp; 
     tmp.parent = curr; 
     return tmp; 
    } 

    public void remove(Tree.Node<E> node) { 
     SLLNode<E> curr = (SLLNode<E>) node; 
     if (curr.parent != null) { 
      if (curr.parent.firstChild == curr) { 
       // The node is the first child of its parent 
       // Reconnect the parent to the next sibling 
       curr.parent.firstChild = curr.sibling; 
      } else { 
       // The node is not the first child of its parent 
       // Start from the first and search the node in the sibling list 
       // and remove it 
       SLLNode<E> tmp = curr.parent.firstChild; 
       while (tmp.sibling != curr) { 
        tmp = tmp.sibling; 
       } 
       tmp.sibling = curr.sibling; 
      } 
     } else { 
      root = null; 
     } 
    } 

    class SLLTreeIterator<T> implements Iterator<T> { 

     SLLNode<T> start, current; 

     public SLLTreeIterator(SLLNode<T> node) { 
      start = node; 
      current = node; 
     } 

     public boolean hasNext() { 
      return (current != null); 
     } 

     public T next() throws NoSuchElementException { 
      if (current != null) { 
       SLLNode<T> tmp = current; 
       current = current.sibling; 
       return tmp.getElement(); 
      } else { 
       throw new NoSuchElementException(); 
      } 
     } 

     public void remove() { 
      if (current != null) { 
       current = current.sibling; 
      } 
     } 
    } 

    public Iterator<E> children(Tree.Node<E> node) { 
     return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild); 
    } 

    void printTreeRecursive(Node<E> node, int level) { 
     if (node == null) 
      return; 
     int i; 
     SLLNode<E> tmp; 

     for (i = 0; i < level; i++) 
      System.out.print(" "); 
     System.out.println(node.getElement().toString()); 
     tmp = ((SLLNode<E>) node).firstChild; 

     while (tmp != null) { 
      printTreeRecursive(tmp, level + 1); 
      tmp = tmp.sibling; 
     } 
    } 

    public void printTree() { 
     printTreeRecursive(root, 0); 
    } 

    public int countMaxChildren() { 
     return countMaxChildrenRecursive(root); 
    } 

    int countMaxChildrenRecursive(SLLNode<E> node) { 
     int t = childCount(node); 
     SLLNode<E> tmp = node.firstChild; 
     while (tmp != null) { 
      t = Math.max(t, countMaxChildrenRecursive(tmp)); 
      tmp = tmp.sibling; 
     } 
     return t; 
    } 

} 
: 여기
c: a b c d 

는 데이터 구조입니다
public interface Tree<E> { 
    // //////////Accessors //////////// 

    public Tree.Node<E> root(); 

    public Tree.Node<E> parent(Tree.Node<E> node); 

    public int childCount(Tree.Node<E> node); 

    // //////////Transformers //////////// 
    public void makeRoot(E elem); 

    public Tree.Node<E> addChild(Tree.Node<E> node, E elem); 

    public void remove(Tree.Node<E> node); 

    // //////////Iterator //////////// 
    public Iterator<E> children(Tree.Node<E> node); 

    // //////Inner interface for tree nodes //////// 
    public interface Node<E> { 

     public E getElement(); 

     public void setElement(E elem); 

    } 
} 
public class SLLTreeTest { 

public static void main(String[] args) { 

    Tree.Node<String> a, b, c, d; 

    SLLTree<String> t = new SLLTree<String>(); 

    t.makeRoot("C:"); 

    a = t.addChild(t.root, "Program files"); 
    b = t.addChild(a, "CodeBlocks"); 
    c = t.addChild(b, "codeblocks.dll"); 
    c = t.addChild(b, "codeblocks.exe"); 
    b = t.addChild(a, "Nodepad++"); 
    c = t.addChild(b, "langs.xml"); 
    d = c; 
    c = t.addChild(b, "readme.txt"); 
    c = t.addChild(b, "notepad++.exe"); 
    a = t.addChild(t.root, "Users"); 
    b = t.addChild(a, "Darko"); 
    c = t.addChild(b, "Desktop"); 
    c = t.addChild(b, "Downloads"); 
    c = t.addChild(b, "My Documents"); 
    c = t.addChild(b, "My Pictures"); 
    b = t.addChild(a, "Public"); 
    a = t.addChild(t.root, "Windows"); 
    b = t.addChild(a, "Media"); 

    t.printTree(); 

    t.remove(d); 
    t.printTree(); 

    System.out.println("The maximum number of children is " 
      + t.countMaxChildren()); 

} 

} 
+0

인쇄하기 전에 트리를 어떻게 초기화하는지 보여줄 수 있습니까? – August

+0

좋아, SLLTreeTest 예제 클래스를 추가하여 보여줄 것이다. – mouseepaad

+0

아무도 모르는 사이? :/ – mouseepaad

답변

0

내가 알기로는, 초기 제안은 Asker 및 다른 주석 작성자에게도 충분합니다. 그래서 이것은 학습 과제이기 때문에, 나는 코드를 답으로 쓰지 않을 것입니다 (나는 모든 재미를 보게 될 것입니다.). 우리가 Collection

  • 우리가 printTreeRecursive가 좋은 (너비 우선 이송을 사용해야 할 필요가

    • : 나는 솔루션에 연결되어야 도달하면, 생각의 과정에 도달하기 위해 몇 가지 중요한 체크 포인트를 공유합니다 예) 우리가 node에 도달 할 때마다 통과
    • 에 도달 할 열쇠로
    • 우리가, printTreeRecursivewhile주기 볼 필요가, 우리는 컬렉션에 노드를 insert sort한다
    • 탐색 후 을 반복하여 해당 요소를 인쇄하십시오.