排序父子关系

时间:2015-10-21 06:43:35

标签: java sorting tree

我遇到一些问题,为我的班级 Page 找出一个简单的compareTo方法:

  • Page通过getParent() - nullable
  • 引用父页面
  • Page有一个变量order,它只确定同一层次结构级别中的顺序

我喜欢按以下方式排序:

  1. ParentA
  2. SubParentOfA1
  3. SubParentOfA2
  4. ParentB
  5. ParentC
  6. SubParentOfC1
  7. SubParentOfC2
  8. 我的compareTo需要如何解决这个问题?我尝试了以下内容,目前只能正确排序相同级别。

    我的方法:

    @Override
    public int compareTo(@NonNull Object o) {
        int compare = 0;
        if (o instanceof Page) {
            Page other = (Page) o;
            if (other.parent != null) {
                compare = this.compareTo(other.parent);
            } else if (parent != null) {
                compare = this.parent.compareTo(other);
            }
            if (compare == 0) {
                compare = Integers.compareTo(this.order, other.order);
            }
        }
        return compare;
    }
    

    编辑:我的网页类:

    class Page{
        int order;
        Page parent;
    }
    

2 个答案:

答案 0 :(得分:3)

您可能有一种检测页面深度的方法

解决方案是:

public class Page {
    private int order;
    private Page parent;

    public Page() {
    }

    public Page(int order, Page parent) {
        this.order = order;
        this.parent = parent;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public Page getParent() {
        return parent;
    }

    public void setParent(Page parent) {
        this.parent = parent;
    }


    public int compareTo(Object o){
        int compare = 0;
        if (o instanceof Page) {
            Page other = (Page) o;
            if(this.depth() == other.depth()){
                if(Objects.equals(this.getParent(), other.getParent())){
                    return Integer.compare( other.getOrder(),this.getOrder());
                }
                else{
                    return this.getParent().compareTo(other.getParent());
                }
            }
            else{
                return Integer.compare(other.depth() , this.depth());
            }
        }
        return compare;
    }

    public int depth(){
        if(this.parent == null)return 0;
        else return 1+this.parent.depth();
    }

    public static void main(String[] args){
        Page a = new Page(1, null);
        Page a_1 = new Page(1, a);
        Page a_2 = new Page(2, a);
        Page a_1_1 = new Page(1, a_1);
        Page a_1_2 = new Page(2, a_1);
        Page a_2_1 = new Page(1, a_2);
        Page a_2_2 = new Page(2, a_2);

        System.out.println(a_1.compareTo(a_1_1));
        //1 because a_1 has higher depth
        System.out.println(a_1.compareTo(a_2));
        //1 because a_1 has smaller order
        System.out.println(a_1_2.compareTo(a_1_1));
        //-1 because a_1_2 has higher order
        System.out.println(a_2_1.compareTo(a_1_1));
        //-1 because a_2_1 parent is a_2 and a_1_1 parent is a_1
    }
}

答案 1 :(得分:2)

您可以简单地依赖order链,而不是进行复杂的计算。我使用String来存储每个“节点”的链:

class Page implements Comparable<Page> {
    String name;
    int order;
    Page parent;
    String key;

    Page() {
        name = "root";
        order  = 0;
        parent = null;
        key    = "";
    }

    Page(String name, int order, Page parent) {
        this.name   = name;
        this.order  = order;
        this.parent = parent;
        key = parent.key + (char)order;
    }

    @Override
    public int compareTo(Page o) {
        return key.compareTo(o.key);
    }

    @Override
    public String toString() {
        return name;
    }
}

public static void main(String[] args) {
    Page root = new Page();

    Page b    = new Page("b"  , 2, root);
    Page b1   = new Page("b.1", 1, b);
    Page b3   = new Page("b.3", 3, b);
    Page b2   = new Page("b.2", 2, b);

    Page a    = new Page("a"  , 1, root);
    Page a2   = new Page("a.2", 2, a);
    Page a1   = new Page("a.1", 1, a);

    List<Page> pages = Arrays.asList(root, a, a1, a2, b, b1, b2, b3);
    System.out.println(pages);

    Collections.shuffle(pages);
    System.out.println(pages);

    Collections.sort(pages);
    System.out.println(pages);
}

这里的诀窍是使用String作为可排序的short数组。该数组是order的复合,形成从根到节点的路径。 root的密钥/ ID为[]a来自rootorder 1为[1]的密钥/ ID。< / p>

这里是key / id矩阵:

┌──────┬────────┬───────┬────────────┬─────┐
│ Page │ Parent │ Order │ Parent Key │ Key │
├──────┼────────┼───────┼────────────┼─────┤
│ root │ -      │ -     │ -          │     │
│ a    │ root   │ 1     │            │ 1   │
│ a.1  │ a      │ 1     │ 1          │ 1 1 │
│ a.2  │ a      │ 2     │ 1          │ 1 2 │
│ b    │ root   │ 2     │            │ 2   │
│ b.1  │ b      │ 1     │ 2          │ 2 1 │
│ b.2  │ b      │ 2     │ 2          │ 2 2 │
│ b.3  │ b      │ 3     │ 2          │ 2 3 │
└──────┴────────┴───────┴────────────┴─────┘

以下是另一棵树(括号中的键)

的示例
root      (     )
├ z       (1    )
│ ├ a     (1 1  )
│ │ └ y   (1 1 1)
│ └ b     (1 2  )
│   └ x   (1 2 1)
└ c       (2    )
  ├ w     (2 1  )
  └ d     (2 2  )