vertex.java

来自「Network数据结构与算法分析中 图的实现 相当全面 请需要的朋友们下载」· Java 代码 · 共 113 行

JAVA
113
字号
public class Vertex implements Comparable {

    String vertexString;


    public Vertex() {

    } // default constructor
    

    public Vertex (String s) {

        vertexString = s;

    } // default constructor


    public String toString() {

        return vertexString;

    } // method toString


    public boolean equals (Object v) {

        return vertexString.equals (((Vertex)v).vertexString);

    } // method equals


    public int compareTo (Object v) {

        return vertexString.compareTo (((Vertex)v).vertexString);

    } // method compareTo


    public int hashCode() {

        return vertexString.hashCode();

    } // method hashCode


} // class Vertex


class EdgeTriple implements Comparable {

    Vertex from,
           to;

    double weight;


    // Postcondition: this EdgeTriple has been initialized from v1, v2 and weight.
    public EdgeTriple (Vertex v1, Vertex v2, double weight) {

        from = v1;
        to = v2;
        this.weight = weight;

    } // default constructor


    // Postcondition: the from vertex in this EdgeTriple has been returned.
    public Vertex getFromVertex() {

        return from;

    } // method getFromVertex


    // Postcondition: the "to" vertex in this EdgeTriple has been returned.
    public Vertex getToVertex() {

        return to;

    } // method getToVertex


    // Postcondition: the weight in this EdgeTriple has been returned.
    public double getWeight() {

        return weight;

    } // method getWeight


    // Postcondition: an int <, = or > 0 has been returned, depending on
    //                whether this EdgeTriple's weight is <, = or > edge's weight.
    public int compareTo (Object edge) {

        Double thisWeight = new Double(weight);
	      Double otherWeight = new Double(((EdgeTriple)edge).getWeight());
        return thisWeight.compareTo(otherWeight);

    } // method compareTo


    // Postcondition: the String corresponding to this EdgeTriple
    //                has been returned.
    public String toString() {

        return from.toString() + "  " + to.toString() + String.valueOf (weight);

    } // method toString

} // class EdgeTriple


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?