2010-11-22 7 views
0

나는 모든 잎이 색인을 가진 나무가있다, 나무가 rescursively 양식 데이터베이스를 lodaded, 데이터베이스는 색인에 의해 나무를 주문할 것이다. 먼저 인덱스로 정렬 된 루트 노드를 가져옵니다. 이제 사용자가 위/아래 화살표 아이콘을 눌러 이러한 인덱스를 정렬 할 수있는 방법을 구현해야합니다. 사용자가 아래로 누를 때 색인은 자신의 색인 아래에있는 색인을 가져와야하며 위쪽 화살표를 누르면 반대 방향으로 이동해야합니다. 이런 종류의 기능을 구현하는 가장 좋은 방법이 무엇인지 모르겠습니다.재귀 트리 인덱스의 순서?

+0

모든 노드에 색인 또는 잎만 있습니까? 인덱스 생성 방법 –

+0

모든 노드에는 색인이 있으며 모든 노드는 동일한 데이터베이스 테이블에 있습니다. 색인은 모든 색인의 가장 높은 색인으로 작성됩니다. – newbie

답변

1

질문이 약간 모호하기 때문에이 대답은 데이터베이스 항목에 관해서 당신이 무엇을 하는지를 알고 있다고 가정합니다 (그렇지 않을 경우 Java 용 최대 절전 모드를 권장합니다). 다음 코드는 몇 가지 아이디어를 제공하기위한 것입니다. 솔루션 구현

//If I have understood your question, you want two nodes to swap position in the tree structure 
public static swapNode(Node parent, Node child) 
{ 
    Long superId = parent.getParentId(); 
    child.parentId(superId); 
    parent.setParentId(child.getId()); 
    child.setId(parentId); 
    //update children lists of parent and child 
    //update parent ids of children lists 

    //save changes to database 
} 

//create tree structure from database. Assumes nodes have been loaded from a database 
//table where each row represents a node with a parent id column the root node which has parent id null) 
//invoke this with all nodes and null for parentId argument 
public static List<Node> createNodeTree(List<Node> allNodes, Long parentId) 
{ 
    List<Node> treeList = new ArrayList<Node>(); 
    for(Node node : nodes) 
    { 
     if(parentIdMatches(node, parentId)) 
     { 
      node.setChildren(createNodeTree(allNodes, node.getId())); 
      treeList.add(node); 
     } 
    } 
    return treeList; 
} 

private static boolean parentIdMatches(Node node, Long parentId) 
{ 
    return (parentId != null && parentId.equals(node.getParentId())) 
     || (parentId == null && node.getParentId() == null); 
} 

//The objects loaded from the database should implement this interface 
public interface Node 
{ 
    void setParentId(Long id); 
    Long getParentId(); 
    Long getId(); 
    List<Node> getChildren(); 
    void setChildren(List<Node> nodes); 
}