Package cpsc331.collections
Class BSTDictionary<K extends java.lang.Comparable<K>,V>
- java.lang.Object
-
- cpsc331.collections.BSTDictionary<K,V>
-
- All Implemented Interfaces:
Dictionary<K,V>
,Mapping<K,V>
public class BSTDictionary<K extends java.lang.Comparable<K>,V> extends java.lang.Object implements Dictionary<K,V>
Provides an implementation of a Dictionary using a binary search tree.
-
-
Constructor Summary
Constructors Constructor Description BSTDictionary()
Constructs an empty BSTDictionary
Preconditions: None
Postcondition: An empty BSTDictionary (satisfying the above BSTDictionary Invariant) has been created.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
change(K k, V v, cpsc331.collections.BSTDictionary.BSTNode x)
V
get(K key)
Returns the value associated with a given key, throwing a NoSuchElementException if no value is currently associated with the given key.void
remove(K k)
Removes the ordered pair with a given input key k, returning a NoSuchElementException and leaving the Mapping unchanged if no such ordered pair exists.void
set(K k, V v)
Sets the value associated with an input key to be an input value — replacing the value formerly associated with this key if one already exists.
-
-
-
Method Detail
-
get
public V get(K key) throws java.lang.NullPointerException, java.util.NoSuchElementException
Description copied from interface:Mapping
Returns the value associated with a given key, throwing a NoSuchElementException if no value is currently associated with the given key.- Specified by:
get
in interfaceMapping<K extends java.lang.Comparable<K>,V>
- Parameters:
key
- the key whose value is to be returned- Returns:
- the value of this key
- Throws:
java.lang.NullPointerException
- if the input key is nulljava.util.NoSuchElementException
- if the key is not null, but no value is defined for this key
Precondition:
- The Mapping Invariant is satisfied.
- A value k with type K has been provided as input.
- The Mapping Invariant is satisfied.
- This Mapping has not been changed.
- If k is null then a NullPointerException is thrown.
- Otherwise (that is, if k is not null), if this Mapping includes some ordered pair whose first entry is the input key k, then the value v that is the second entry of this ordered pair (that is, the value associated with k) is returned as output. A NoSuchElementException is thrown otherwise.
-
set
public void set(K k, V v) throws java.lang.NullPointerException
Description copied from interface:Mapping
Sets the value associated with an input key to be an input value — replacing the value formerly associated with this key if one already exists.- Specified by:
set
in interfaceMapping<K extends java.lang.Comparable<K>,V>
- Parameters:
k
- the key for which a value is to be definedv
- the value that is to be defined to this key- Throws:
java.lang.NullPointerException
- if k is null
Precondition:
- The Mapping Invariant is satisfied.
- A value k with type K and v with type V are provided as input.
- The Mapping Invariant is satisfied.
- If k is null then a NullPointerException is thrown.
- Otherwise (that is, if k is not null), if this Mapping included an ordered pair whose first entry is the input value k, then this is replaced by the ordered pair (k, v). Otherwise a new ordered pair (k, v) is added. No other changes to this Mapping are made.
-
remove
public void remove(K k) throws java.lang.NullPointerException, java.util.NoSuchElementException
Description copied from interface:Mapping
Removes the ordered pair with a given input key k, returning a NoSuchElementException and leaving the Mapping unchanged if no such ordered pair exists.- Specified by:
remove
in interfaceMapping<K extends java.lang.Comparable<K>,V>
- Parameters:
k
- the key for which a value is to be undefined- Throws:
java.lang.NullPointerException
- if k is nulljava.util.NoSuchElementException
- if no value was defined for this key
Precondition:
- The Dictionary Invariant is satisfied.
- A value k with type K is provided as input.
- The Mapping Invariant is satisfied.
- If k is null then a NullPointerException is thrown.
- Otherwise (that is, if k is not null) if this Mapping includes an ordered pair (k, v) whose first entry is the input key k then this ordered pair is removed; no other changes are made. Otherwise a NoSuchElementException is thrown and this Mapping is not changed at all.
-
-