1

ツリー セットの挿入メソッドで 2 つのオブジェクトを比較する必要があります。しかし、 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;
}


}
4

2 に答える 2

0

以下のコードを参照してください

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;
    }
}
于 2013-10-18T16:43:43.610 に答える