【问题标题】:How would i compare two objects in custom tree set implementation?我将如何比较自定义树集实现中的两个对象?
【发布时间】:2013-10-18 16:42:18
【问题描述】:

我需要在树集的插入方法中比较两个对象。但我无法弄清楚在哪里以及如何实现 Comparable 或 Comparator。我的代码如下所示:

这是我为二叉树创建的节点。

Node.java

public class Node {

private Object data;
private Node left, right;

//initial case when a Node of a binary tree gets created. both left and right subtrees point to   null
public Node (){

    left = right = null;
}

public Object getData() {
    return data;
}

public void setData(Object data) {
    this.data = data;
}

public Node getLeft() {
    return left;
}

public void setLeft(Node left) {
    this.left = left;
}

public Node getRight() {
    return right;
}

public void setRight(Node right) {
    this.right = right;
}

}

这是我的 MyBinaryTree 类,我需要在其中实现插入方法:

MyBinaryTree.java

public class MyBinaryTree implements Comparable<Node> {

Node root;

public MyBinaryTree(){
    root = null;
}

void insert(Object x){

    Node newrec = new Node();  //Node constructor gets called and sets up a root node with empty
    //subtrees
    newrec.setData(x);

    if(root == null){
        root = newrec;
    }
    else{
        Node a,b;
        a = b = root;
        while(a!=null){
            b=a;   
            if( ( newrec.getData() ).compareTo( a.getData() ) ) {

我被困在这里了!我将如何使用 Comparable 比较这些对象?

            }
        }

    }

}

void inorder(Node root){

}

@Override
public int compareTo(Node o) {
    // TODO Auto-generated method stub
    int i = (o.)
    return 0;
}


}

【问题讨论】:

    标签: java collections binary-search-tree comparator comparable


    【解决方案1】:

    您需要能够比较的不仅仅是节点,还有这些节点中包含的数据。这意味着您的 Node 要么需要限制为采用 Comparable, 的对象,要么您的树需要采用可用于比较它们的 Comparator

    如果你真的想支持两者,那么到时候做比较,如果提供了Comparator,使用它的比较方法,否则将数据转换为Comparable&lt;? super E&gt;,其中E是Node的类型数据(见下文),然后使用其compareTo 方法。

    这让我想到了下一点。您的 Node 类可能不应该简单地包含 Object 作为其数据,而是声明为 Node&lt;E&gt; implements Comparable&lt;Node&lt;E&gt;&gt;, 然后您的树可以声明为 MyBinaryTree&lt;E&gt;. 我还将更改 Node 的构造函数以获取数据作为参数,而不是在创建后立即调用 setter。您没有理由想要创建一个没有数据的Node

    我强烈建议查看 JDK 附带的 java.util 包中的一些通用集合的源代码。特别是,我参考了TreeMap.java 的来源,以了解它们如何处理 Comparable 和 non-Comparable 元素,因为没有以要求元素为 Comparable 的方式声明该类。 (如果不是,并且没有Comparator,ClassCastException,他们会尝试将对象转换为Comparable&lt;? super K&gt;。)看看他们如何实现类似的代码将对您有很大帮助。您可能还想查看 Java 泛型。

    【讨论】:

      【解决方案2】:

      请参考以下代码

      package com.example.treeset;
      
      import java.util.Comparator;
      import java.util.TreeSet;
      
      public class MyCompUser {
      
          public static void main(String a[]){
              //By using name comparator (String comparison)
              TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
              nameComp.add(new Empl("Ram",3000));
              nameComp.add(new Empl("John",6000));
              nameComp.add(new Empl("Crish",2000));
              nameComp.add(new Empl("Tom",2400));
              for(Empl e:nameComp){
                  System.out.println(e);
              }
              System.out.println("===========================");
              //By using salary comparator (int comparison)
              TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
              salComp.add(new Empl("Ram",3000));
              salComp.add(new Empl("John",6000));
              salComp.add(new Empl("Crish",2000));
              salComp.add(new Empl("Tom",2400));
              for(Empl e:salComp){
                  System.out.println(e);
              }
          }
      }
      
      class MyNameComp implements Comparator<Empl>{
      
          @Override
          public int compare(Empl e1, Empl e2) {
              return e1.getName().compareTo(e2.getName());
          }
      }   
      
      class MySalaryComp implements Comparator<Empl>{
      
          @Override
          public int compare(Empl e1, Empl e2) {
              if(e1.getSalary() > e2.getSalary()){
                  return 1;
              } else {
                  return -1;
              }
          }
      }
      
      class Empl{
      
          private String name;
          private int salary;
      
          public Empl(String n, int s){
              this.name = n;
              this.salary = s;
          }
      
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public int getSalary() {
              return salary;
          }
          public void setSalary(int salary) {
              this.salary = salary;
          }
          public String toString(){
              return "Name: "+this.name+"-- Salary: "+this.salary;
          }
      }
      

      【讨论】:

      • 你应该比“请参考下面的代码”更好地描述这个问题
      猜你喜欢
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多