package edu.princeton.cs.algs4.growingtree.framework; /* * @(#)TreeHead.java * * Last Modified: 9/15/01 */ import java.awt.*; import java.util.*; /** * The TreeHead interface extends the Tree interface. The TreeHead is used as the top null node * of any given Tree. This implementation makes many necessary methods easier to perform with a null * head node. The head node must implement all pertinent methods defined in the interface.

* * Generally, the TreeHead extends from the Tree in most cases and simply includes more defined methods. * The constructor generally constructs the Tree super construction first and then proceeds to the Head * defined methods. Consequently, the TreeHead is simply an extended Tree with null information. * * @author Corey Sanders * @version 1.2 9/15/01 */ public interface TreeHead

extends Tree

{ /** * String representing the waiting action of rotateUp. Requires an accompanying node. */ public static final String ROTATE_UP = "Rotate Up"; /** * String representing the waiting action of rotateToTop. Requires an accompanying node. */ public static final String ROTATE_TO_TOP_NODE = "Rotate To Top"; /** * String representing the waiting action of rotateUpDouble. Requires an accompanying node. */ public static final String ROTATE_UP_DOUBLE = "Rotate Up Double"; /** * String representing the waiting action of splay. Requires an accompanying node. */ public static final String SPLAY_NODE = "Splay"; /** * String representing the waiting action of remove. Requires an accompanying node. */ public static final String REMOVE_NODE = "Remove Node"; /** * String representing the waiting action of insert. Requires an accompanying node. */ public static final String INSERT_NODE = "Insert Node"; /** * String representing the waiting action of search. Requires an accompanying node. */ public static final String SEARCH = "Search"; /** * String representing the waiting action of select. Requires an accompanying node. */ public static final String SELECT = "Select"; /** * String representing the waiting action of swap. Requires an accompanying node. */ public static final String SWAP = "Swap"; /** * String representing the waiting action of freeze */ public static final String FREEZE = "Freeze"; /** * String representing the waiting action of partition. Requires an accompanying NodeAndKey object (Inner class of BSTTreeHead). */ public static final String PARTITION_NODE = "Partition Node"; /** * String representing the waiting action of balance. Requires an accompanying NodeAndKey object (Inner class of BSTTreeHead). */ public static final String BALANCE_NODE = "Balance Node"; /** * String representing the waiting action of traverse. Requires an accompanying Integer object. */ public static final String TRAVERSE = "Traverse Tree"; /** * String representing the waiting action of changeDisplay. */ public static final String CHANGE_DISPLAY = "Change Display"; /** * Traverse type preorder. */ public static final int PREORDER_TRAVERSAL = 1; /** * Traverse type inorder. */ public static final int INORDER_TRAVERSAL = 2; /** * Traverse type postorder. */ public static final int POSTORDER_TRAVERSAL = 3; /** * Traverse type levelorder. */ public static final int LEVELORDER_TRAVERSAL = 4; /** * Returns true if the is empty, indicating no Child node, and a level of 0. * * @return true if the TreeHead is empty. */ public boolean isTreeEmpty(); /** * Resets the lowest level of the Tree. */ public void resetTreeLevel(); /** * Gets the lowest level of the Tree. A level of 0 indicates an empty tree. * * @return integer level set for the Tree. */ public int getTreeLevel(); /** * Fixes the lowest level of the Tree. */ public void fixLevel(); /** * Sets the child of the TreeHead. The child is the beginning of the Tree nodes. * * @param child Tree, beginning the Tree nodes. */ public void setChild(Tree

child); /** * Gets the child of the TreeHead. The child is the beginning of the Tree nodes. * * @param child Tree, beginning the Tree nodes. */ public Tree

getChild(); /** * Adds an TreeMessageListener from the tree, according to * the TreeMessageListener interface and the TreeMessageEvent. * * @param l the listener added recieves the TreeMessageEvents occuring. */ public void addTreeMessageListener(TreeMessageListener l); /** * Removes an TreeMessageListener from the tree, according to * the TreeMessageListener interface and the TreeMessageEvent. * * @param l the listener removed from recieving the TreeMessageEvents occuring. */ public void removeTreeMessageListener(TreeMessageListener l); /** * Adds the comaparable object keyInsert to the tree using its natural ordering . * The value, valueInsert is added with the key to the node. * * @param keyInsert comparable object which is added to the tree. * @param valueInsert Object that accompanies keyInsert in the node. * * @return boolean true if this collection changed as a result of the call. * * @throws ClassCastException key cannot be compared with the keys * currently in the map. * @throws NullPointerException key is null. */ public boolean insert(KeyType keyInsert, Object valueInsert) throws ClassCastException, NullPointerException; /** * Removes the given node from the tree. The accompanying value is also * removed from the tree and the node is deleted. * * @param node the Tree node to be removed from the tree. * * @throws NullPointerException node is null */ public void remove(Tree

node) throws NullPointerException; /** * Removes the comaparable object keyRemove from the BSTTree using its natural ordering . * If the method is successful in removing the element, true is returned. * * @param keyRemove comparable object which is removed from the tree. * * @return boolean true if this collection changed as a result of the call. * * @throws ClassCastException key cannot be compared with the keys * currently in the map. * @throws NullPointerException key is null. */ public boolean remove(KeyType keyRemove) throws ClassCastException, NullPointerException; /** * Searches for the comaparable object in the Tree using its natural ordering . * If the method is successful in finding the element, the item is returned. Otherwise, null is * returned. * * @param keySearch comparable object which is search for within the tree. * * @return Tree element which matches the keySearch or null of the key is not present within the * tree. * * @throws ClassCastException key cannot be compared with the keys * currently in the map. * @throws NullPointerException key is null. */ public Tree

search(KeyType keySearch) throws ClassCastException, NullPointerException; /** * Partitions from the given node the keySelect value. Replacing the reference in the * Tree to the given node, to the keySelectth item. * * @param node Tree which the partition occurs at. * @param keySelect integer key selecting the count item. * * @return Tree that is the reference to the newly partitioned tree. */ public Tree

partition(Tree

node, int keySelect); /** * Selects the kth smallest item in the Tree using its natural ordering from the given node. * If the method is successful in finding the element, the item is returned. Otherwise, null is * returned if the integer is greater than the size. * * @param node Tree which the partition occurs at. * @param keySelect integer key selecting the count item. * * @return Tree element which matches the kth element or null if k > size. */ public Tree

select(Tree

node, int keySelect); /** * Balances the entire tree. * */ public void balanceTree(); /** * Balances the tree, from the node downward. * * @param node the Tree node from which the balance occurs */ public void balance(Tree

node); /** * Returns the number of objects in the entire tree. * * @return the number of objects in the entire tree. */ public int size(); /** * Clears the tree completely, removing all references to all nodes and all * values return to the default. */ public void clear(); /** * Acts according to the String action passed. The method generally accompanies * a WaitingActionList which keeps the list of actions, and calls the method when * instructed to call the next action. * * @param action String action representing the next action for the TreeHead. * @param elements elemetns to which the action could be occuring, depending on the type of action. */ public void waitingAction(String action, ActionElementType

element); }