对谓词进行排序,使节点按“深度优先搜索”顺序排序

时间:2011-01-26 14:11:06

标签: algorithm sorting tree predicate depth-first-search

我有一个节点列表,其中每个节点属于一个或多个树。 (他们不一定有共同的祖先)

我想按照我在深度优先搜索时找到的顺序对节点进行排序。

假设我有一个用于将树根排序在一起的谓词,以及另一个用于将共同父项的子项排序在一起的谓词。每个节点都有一个Parent访问器和一个子枚举器。出于性能原因,我想避免使用Children枚举(如果可能的话)。

谓词传递给排序函数的伪代码是什么(如果节点1小于节点2,则谓词将返回布尔值。)

2 个答案:

答案 0 :(得分:0)

我认为,您需要在节点中存储有用的信息,因此谓词可以从一对未连接的节点中选择前一个节点。

这是我的(可能不是很聪明,甚至是工作)尝试:

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);
    }
}

答案 1 :(得分:0)

我找到了一个简单易用的解决方案:

对于节点,具有从根返回路径的函数。例如,在文件系统中,文件的路径可以是:c:\ directory \ file.txt,其中“C:”,“directory”和“file.txt”是父节点。

谓词只需将路径进行比较,就像简单的字符串比较一样。路径不需要是字符串,路径比较函数需要从根开始逐个比较路径元素,并在路径元素不同时立即返回。

生成的排序与深度优先搜索相同。