질문이 약간 모호하기 때문에이 대답은 데이터베이스 항목에 관해서 당신이 무엇을 하는지를 알고 있다고 가정합니다 (그렇지 않을 경우 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);
}
모든 노드에 색인 또는 잎만 있습니까? 인덱스 생성 방법 –
모든 노드에는 색인이 있으며 모든 노드는 동일한 데이터베이스 테이블에 있습니다. 색인은 모든 색인의 가장 높은 색인으로 작성됩니다. – newbie