twothreecursoruos.java

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

JAVA
78
字号
/* TwoThreeCursorUos.java
 * ---------------------------------------------
 * Copyright (c) 2001 University of Saskatchewan
 * All Rights Reserved
 * --------------------------------------------- */
 
package dslib.dictionary.twothreetree;

import dslib.base.*;
import dslib.exception.NoCurrentItemUosException;

/**	A cursor for the two-three tree data structures. */
public class TwoThreeCursorUos implements KeyedCursorUos
{

	/** The current node*/
	protected TwoThreeLeafUos node;
	
	/**	Is the cursor after the last item?. */
	protected boolean after;
	
	/**	Create a new cursor. <br>
		Analysis: Time = O(1) 
		@param newItemKey The key of the current item 
		@param newItem The current item */
	public TwoThreeCursorUos(TwoThreeLeafUos leaf, boolean isAfter)
	{
		after = isAfter;
		node = leaf;
	}

	/**	Is there a current item?. <br>
		Analysis: Time = O(1) */
	public boolean itemExists()
	{
		return node!=null;
	}	

	/**	The key of the current item. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION: <br>
		<ul>
			itemExists()
		</ul> */	
	public Comparable itemKey() throws NoCurrentItemUosException
	{
		if (! itemExists())
			throw new NoCurrentItemUosException("No current item to delete.");
		return node.key();
	}
	
	/**	The current item. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION: <br>
		<ul>
			itemExists()
		</ul>*/
	public Object item() throws NoCurrentItemUosException
	{
		if (! itemExists())
			throw new NoCurrentItemUosException("No current item to delete.");
		return node; 
	}

	/**	The current key-item pair. <br>
		PRECONDITION: <br>
		<ul>
			itemExists()
		</ul> */
	public PairUos keyItemPair() throws NoCurrentItemUosException
	{
		if (! itemExists())
			throw new NoCurrentItemUosException("No current item to delete.");
		return new PairUos(itemKey(), item());
	}

}

⌨️ 快捷键说明

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