edgeuos.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 51 行

JAVA
51
字号
/* EdgeUos.java
 * ---------------------------------------------
 * Copyright (c) 2001 University of Saskatchewan
 * All Rights Reserved
 * --------------------------------------------- */
 
package dslib.graph;

import dslib.base.*;
import dslib.exception.ItemNotFoundUosException;

/**	An edge of a graph that has firstItem and secondItem as its ends.  Method 'has' 
	tests whether a vertex is an end of the edge, and method 'other' returns the other end. */
public class EdgeUos extends PairUos
{
	/**	Construct a new Edge object. <br>
		Anaysis: Time = O(1) 
		@param v1 initial vertex of the new edge
		@param v2 terminal vertex of the new edge */
	public EdgeUos(VertexUos v1, VertexUos v2)
	{
		super(v1, v2);
	}

	/**	Determine if v is an item of the edge. <br>
		Analysis: Time = O(1) 
		@param v vertex to determin whether it belongs to the edge */
	public boolean has(VertexUos v)
	{
		return ((v == firstItem) || (v == secondItem));
	}

	/**	The other item of the edge. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION: <br>
		<ul>
			has(v) 
		</ul>
		@param v vertex adjacent vertex to v via this edge  */
	public VertexUos other(VertexUos v) throws ItemNotFoundUosException
	{
		if (!has(v))
			throw new ItemNotFoundUosException("Cannot return the other item since this vertex does not exist.");
	
		if (firstItem == v)
			return (VertexUos)secondItem;
		else // must have (secondItem == v)
			return (VertexUos)firstItem;
	}
}

⌨️ 快捷键说明

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