问题
I have a list of nodes, where each of the nodes belong to one or multiple trees. (they do not necessarily share a common ancestor)
I want to sort the nodes in the same order I would find them when doing a Depth First Search.
Let say I have a predicate for sorting tree roots together, and another predicate to sort children of a common parent together. Each node have a Parent accessor, and a children enumerator. I want to avoid using the Children enumeration for performance reasons (if possible).
What can be the pseudo code for a predicate to pass to a sort function (the predicate would return a boolean value if node 1 is less than node 2).
回答1:
I think, you need to store helpful information in nodes, so predicate can choose preceding node from pair of unconnected nodes.
Here's my (may be not very clever or, even, working) attempt:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
*/
public class SortingTree {
private static class Node implements Comparable<Node> {
private final String data;
private Node p, l, r;
private int ordinal = 0;
public Node(String data) {
this.data = data;
}
public Node setLeft(Node n) {
n.ordinal = ordinal + 1;
if (ordinal == 0)
n.ordinal = 2;
else
n.ordinal = ordinal + 2;
n.p = this;
return n;
}
public Node setRight(Node n) {
if (ordinal == 0)
n.ordinal = 1;
else
n.ordinal = ordinal + 4;
n.p = this;
return n;
}
public String toString() {
return data;
}
public int compareTo(Node o) {
// check if one of args is root
if (p == null && o.p != null) return -1;
if (p != null && o.p == null) return 1;
if (p == null && o.p == null) return 0;
// check if one of args is left subtree, while other is right
if (ordinal % 2 == 0 && o.ordinal % 2 == 1) return -1;
if (ordinal % 2 == 1 && o.ordinal % 2 == 0) return 1;
// if ordinals are the same, first element is the one which parent have bigger ordinal
if (ordinal == o.ordinal) {
return o.p.ordinal - p.ordinal;
}
return ordinal - o.ordinal;
}
}
public static void main(String[] args) {
List<Node> nodes = new ArrayList<Node>();
Node root = new Node("root"); nodes.add(root);
Node left = root.setLeft(new Node("A")); nodes.add(left);
Node leftLeft = left.setLeft(new Node("C")); nodes.add(leftLeft); nodes.add(leftLeft.setLeft(new Node("D")));
nodes.add(left.setRight(new Node("E")));
Node right = root.setRight(new Node("B")); nodes.add(right);
nodes.add(right.setLeft(new Node("F"))); nodes.add(right.setRight(new Node("G")));
Collections.sort(nodes);
System.out.println(nodes);
}
}
回答2:
I found an easy working solution:
For a node, have a function that returns the path from the root. For example, in a file system, the path for a file could be : c:\directory\file.txt, where "C:", "directory" and "file.txt" are the parent nodes.
The predicate simply need to compare the paths together, as a simple string comparison would do. The path does not need to be a string, the path comparison function need to compare path elements one by one starting at the root, and return as soon a path element is different.
The resulting sort is the same as a Depth First Search.
来源:https://stackoverflow.com/questions/4805454/sort-predicate-to-have-nodes-sorted-in-depth-first-search-order