net.sourceforge.cilib.type.types.container
Class BinaryTree<E extends Comparable<? super E> & Cloneable>

java.lang.Object
  extended by net.sourceforge.cilib.type.types.container.AbstractTree<E>
      extended by net.sourceforge.cilib.type.types.container.BinaryTree<E>
Type Parameters:
E - The Comparable and Cloneable type.
All Implemented Interfaces:
Serializable, Iterable<E>, StructuredType<E>, Tree<E>, Randomizable, Type, Cloneable

public class BinaryTree<E extends Comparable<? super E> & Cloneable>
extends AbstractTree<E>

Implementation of a BinaryTree.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class net.sourceforge.cilib.type.types.container.AbstractTree
AbstractTree.TreeIterator
 
Field Summary
 
Fields inherited from class net.sourceforge.cilib.type.types.container.AbstractTree
key
 
Constructor Summary
BinaryTree()
          Create a new instance of BinaryTree.
BinaryTree(BinaryTree<E> copy)
          Copy constructor.
BinaryTree(E element)
          Create a new instance of BinaryTree with the provided parameter as the value of the key.
BinaryTree(E key, BinaryTree<E> left, BinaryTree<E> right)
          Create an instance of BinaryTree with the provided tree instances as the left and right children, maintaining the provided key value.
 
Method Summary
 boolean add(E element)
          Convenience method.
 boolean addSubTree(Tree<E> subTree)
          Add the provided subtree to the current Tree.
 void clear()
          Clear all contained object instances from the current Structure.
 boolean contains(E element)
          Determine if element is contained within the Structure.
 void depthFirstTraversal(PrePostVisitor<E> visitor)
          Perform a depth first traversal of the current Tree node, executing the operation stored within the provided Visitor instance.
 boolean equals(Object obj)
          Compare the specified object with this type for equality.
 BinaryTree<E> getClone()
          Create a cloned copy of the current object and return it.
 int getDegree()
          Get the degree of the current Tree.
 Tree<E> getSubTree(E element)
          Get the subtree with the specified key value.
 Tree<E> getSubTree(int index)
          Return the subTree with the node value of element.
 int hashCode()
          Returns the hash code value for this list.
 boolean isLeaf()
          Determine if the current Tree node is a leaf node in the current structure.
 boolean remove(E element)
          Remove the first element found within the Structure.
 E remove(int index)
          Remove the object at the specified index.
 Tree<E> removeSubTree(E element)
          Remove the subtree with the given key value.
 Tree<E> removeSubTree(int index)
          Remove the subtree at the specified index within the current Tree.
 String toString()
          
 
Methods inherited from class net.sourceforge.cilib.type.types.container.AbstractTree
accept, addAll, breadthFirstTraversal, getKey, getRepresentation, isEmpty, iterator, randomize, removeAll, setKey, size
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryTree

public BinaryTree()
Create a new instance of BinaryTree.


BinaryTree

public BinaryTree(E element)
Create a new instance of BinaryTree with the provided parameter as the value of the key.

Parameters:
element - The value of the key that the BinaryTree should maintain.

BinaryTree

public BinaryTree(E key,
                  BinaryTree<E> left,
                  BinaryTree<E> right)
Create an instance of BinaryTree with the provided tree instances as the left and right children, maintaining the provided key value.

Parameters:
key - The value of the tree to maintain.
left - The left child.
right - The right child.

BinaryTree

public BinaryTree(BinaryTree<E> copy)
Copy constructor. Create a copy of the provided instance. This copy is a deep copy of the provided instance.

Parameters:
copy - The instance to copy.
Method Detail

getClone

public BinaryTree<E> getClone()
Create a cloned copy of the current object and return it. In general the created copy will be a deep copy of the provided instance. As a result this operation an be quite expensive if used incorrectly.

Specified by:
getClone in interface StructuredType<E extends Comparable<? super E> & Cloneable>
Specified by:
getClone in interface Tree<E extends Comparable<? super E> & Cloneable>
Specified by:
getClone in interface Type
Specified by:
getClone in interface Cloneable
Specified by:
getClone in class AbstractTree<E extends Comparable<? super E> & Cloneable>
Returns:
An exact clone of the current object instance.
See Also:
Object.clone()

equals

public boolean equals(Object obj)
Compare the specified object with this type for equality. Returns true if and only if the specified object is also an instance of the same type.

Specified by:
equals in interface Type
Specified by:
equals in class AbstractTree<E extends Comparable<? super E> & Cloneable>
Parameters:
obj - The object to compare.
Returns:
true if equality exists, false otherwise.
See Also:
Object.equals(Object)

hashCode

public int hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
  int hashCode = 7;
  Iterator<E> i = list.iterator();
  while (i.hasNext()) {
      E obj = i.next();
      hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  }
 
This ensures that type1.equals(type2) implies that type1.hashCode()==type2.hashCode() for any two types, type1 and type2, as required by the general contract of Object.hashCode().

Specified by:
hashCode in interface Type
Specified by:
hashCode in class AbstractTree<E extends Comparable<? super E> & Cloneable>
Returns:
the hash code value for this list
See Also:
Object.equals(Object), Type.equals(Object)

toString

public String toString()

Overrides:
toString in class Object

addSubTree

public boolean addSubTree(Tree<E> subTree)
Add the provided subtree to the current Tree. Addition is by default first the left branch subtree. If the left branch already has a defined tree attached, the right subtree is assigned.

Parameters:
subTree - The to add.
Returns:
true if the addition was successful, false otherwise

getSubTree

public Tree<E> getSubTree(E element)
Get the subtree with the specified key value.

Parameters:
element - The value of the key to lookup.
Returns:
A reference to the found Tree, otherwise null.

getSubTree

public Tree<E> getSubTree(int index)
Return the subTree with the node value of element. If such an subTree does not exist, an empty tree is returned.

Returns:
The subtree if found else an empty Tree object.

removeSubTree

public Tree<E> removeSubTree(E element)
Remove the subtree with the given key value.

Parameters:
element - The value of the key of the subtree to be removed.
Returns:
The instance that was removed, otherwise null.

removeSubTree

public Tree<E> removeSubTree(int index)
Remove the subtree at the specified index within the current Tree.

Parameters:
index - The index of the Tree to be removed.
Returns:
The instance that was removed, otherwise null.

add

public boolean add(E element)
Convenience method. Defers to BinaryTree#addSubtree(Tree).

Parameters:
element - The element to add to the current tree.
Returns:
The result of the addition from addSubTree(Tree).

clear

public void clear()
Clear all contained object instances from the current Structure.


contains

public boolean contains(E element)
Determine if element is contained within the Structure.

Parameters:
element - The object that is tested for containment.
Returns:
true if the Structure contains the object, false otherwise.

remove

public boolean remove(E element)
Remove the first element found within the Structure.

Parameters:
element - The object to remove
Returns:
true if the removal was successful, false otherwise.

remove

public E remove(int index)
Remove the object at the specified index.

Parameters:
index - The index at which an object is to be removed.
Returns:
The removed instance

depthFirstTraversal

public void depthFirstTraversal(PrePostVisitor<E> visitor)
Perform a depth first traversal of the current Tree node, executing the operation stored within the provided Visitor instance.

Specified by:
depthFirstTraversal in interface Tree<E extends Comparable<? super E> & Cloneable>
Overrides:
depthFirstTraversal in class AbstractTree<E extends Comparable<? super E> & Cloneable>
Parameters:
visitor - The visitor operation to execute at each node.

isLeaf

public boolean isLeaf()
Determine if the current Tree node is a leaf node in the current structure.

Returns:
true if it is a leaf node, false otherwise.

getDegree

public int getDegree()
Get the degree of the current Tree. The degree is an indication of the Trees branching factor and therefore the maximum number of children that it may contain.

Returns:
The branching factor / maximum allowable children.


Copyright © 2009 CIRG. All Rights Reserved.