Package project5
Class BST<E extends Comparable<E>>
Object
BST<E>
- Type Parameters:
E
- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>
An implementation of a binary search tree. The elements are ordered using
their natural ordering.
This implementation provides guaranteed O(H) (H is the height of this tree
which could be as low as logN for balanced trees, but could be as large as N
for unbalanced trees) time cost for the basic operations (add
,
remove
and contains
).
This class implements many of the methods provided by the Java framework's
TreeSet
class.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds the specified element to this set if it is not already present.void
clear()
Removes all of the elements from this set.boolean
Returnstrue
if this set contains the specified element.boolean
Compares the specified object with this tree for equality.first()
Returns the first (smallest) element currently in this tree.get
(int index) Returns the element at the specified position in this tree.int
height()
Returns the height of this tree.boolean
isEmpty()
Returnstrue
if this set contains no elements.iterator()
Returns an iterator over the elements in this tree in ascending order.last()
Returns the last (largest) element currently in this tree.Returns an iterator over the elements in this tree in order of the postorder traversal.Returns an iterator over the elements in this tree in order of the preorder traversal.boolean
Removes the specified element from this tree if it is present.int
size()
Returns the number of elements in this tree.toString()
Returns a string representation of this tree.Produces tree like string representation of this tree.Methods inherited from class Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
BST
public BST()Constructs a new, empty tree, sorted according to the natural ordering of its elements. All elements inserted into the tree must implement the Comparable interface.
This operation should be O(1). -
BST
Constructs a new tree containing the elements in the specified collection array, sorted according to the natural ordering of its elements. All elements inserted into the tree must implement the Comparable interface.
This operation should be O(N logN) where N is the number of elements in the collection. This implies, that the tree that is constructed has to have the high that is approximately logN, not N.- Parameters:
collection
- collection whose elements will comprise the new tree- Throws:
NullPointerException
- if the specified collection is null
-
-
Method Details
-
add
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this tree if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false.
This operation should be O(H).- Parameters:
e
- element to be added to this set- Returns:
- true if this set did not already contain the specified element
- Throws:
NullPointerException
- if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
-
remove
Removes the specified element from this tree if it is present. More formally, removes an elemente
such thatObjects.equals(o, e)
, if this tree contains such an element. Returnstrue
if this tree contained the element (or equivalently, if this tree changed as a result of the call). (This tree will not contain the element once the call returns.)
This operation should be O(H).- Parameters:
o
- object to be removed from this set, if present- Returns:
true
if this set contained the specified element- Throws:
ClassCastException
- if the specified object cannot be compared with the elements currently in this treeNullPointerException
- if the specified element is null
-
clear
public void clear()Removes all of the elements from this set. The set will be empty after this call returns.
This operation should be O(1). -
contains
Returnstrue
if this set contains the specified element. More formally, returnstrue
if and only if this set contains an elemente
such thatObjects.equals(o, e)
.
This operation should be O(H).- Parameters:
o
- object to be checked for containment in this set- Returns:
true
if this set contains the specified element- Throws:
ClassCastException
- if the specified object cannot be compared with the elements currently in the setNullPointerException
- if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
-
size
public int size()Returns the number of elements in this tree.
This operation should be O(1).- Returns:
- the number of elements in this tree
-
isEmpty
public boolean isEmpty()Returnstrue
if this set contains no elements.
This operation should be O(1).- Returns:
true
if this set contains no elements
-
height
public int height()Returns the height of this tree. The height of a leaf is 1. The height of the tree is the height of its root node.
This operation should be O(1).- Returns:
- the height of this tree or zero if the tree is empty
-
iterator
Returns an iterator over the elements in this tree in ascending order.
This operation should be O(N).- Specified by:
iterator
in interfaceIterable<E extends Comparable<E>>
- Returns:
- an iterator over the elements in this set in ascending order
-
preorderIterator
Returns an iterator over the elements in this tree in order of the preorder traversal.
This operation should be O(N).- Returns:
- an iterator over the elements in this tree in order of the preorder traversal
-
postorderIterator
Returns an iterator over the elements in this tree in order of the postorder traversal.
This operation should be O(N).- Returns:
- an iterator over the elements in this tree in order of the postorder traversal
-
get
Returns the element at the specified position in this tree. The order of the indexed elements is the same as provided by this tree's iterator. The indexing is zero based (i.e., the smallest element in this tree is at index0
and the largest one is atindex size()-1)
.
This operation should be O(H).- Parameters:
index
- index of the element to return- Returns:
- the element at the specified position in this tree
- Throws:
IndexOutOfBoundsException
- if the index is out of range(index < 0 || index >= size())
-
first
Returns the first (smallest) element currently in this tree.
This operation should be O(H).- Returns:
- the first (smallest) element currently in this tree
- Throws:
NoSuchElementException
- if this set is empty
-
last
Returns the last (largest) element currently in this tree.
This operation should be O(H).- Returns:
- the last (largest) element currently in this tree
- Throws:
NoSuchElementException
- if this set is empty
-
equals
Compares the specified object with this tree for equality. Returnstrue
if the given object is also a tree, the two trees have the same size, and every member of the given tree is contained in this tree.
This operation should be O(N). -
toString
Returns a string representation of this tree. The string representation consists of a list of the tree's elements in the order they are returned by its iterator (inorder traversal), enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).
This operation should be O(N). -
toStringTreeFormat
Produces tree like string representation of this tree. Returns a string representation of this tree in a tree-like format. The string representation consists of a tree-like representation of this tree. Each node is shown in its own line with the indentation showing the depth of the node in this tree. The root is printed on the first line, followed by its left subtree, followed by its right subtree. For exampleK |--D |--B |--A |--null |--null |--null |--J |--null |--null |--P |--M |--L |--null |--null |--O |--N |--null |--null |--null |--null
is a string representation of this tree:K / \ D P / \ / B J M / / \ A L O / N
This operation should be O(N).- Returns:
- string containing tree-like representation of this tree.
-