Tree Maps and Tree Sets The JCF Binary
Tree Maps and Tree Sets The JCF Binary Tree classes CS-2851 Dr. Mark L. Hornick 1
Map definition review l A map is a collection in which each Entry element has two parts: l l l a unique key part a value part (which may not be unique) Entry key value Each unique key “maps” to a corresponding value l Example: a map of students, in which each key is the (unique) student ID, and each (non-unique? ) value is a reference to a student object. CS-2851 Dr. Mark L. Hornick 2
Tree. Map : The JCF implementation of a Binary Tree l l Tree. Map stores a Map in a red-black tree, ordered by (unique) keys public class Tree. Map<K, V> implements Sorted. Map<K, V> extends Abstract. Map<K, V> Example: Tree. Map<Integer, Student> students; //Integer: unique student ID number //Student: student object CS-2851 Dr. Mark L. Hornick 3
JCF Tree. Map methods l Implements the Map interface l l e. g. no iterator() method But Map does define l l Put(k, v) – insert a key/value entry into the Binary Tree Remove(k) – remove the entry with the specified key contains. Key(k) – search for the entry with key k contains. Value(v) – search for an entry with value v l l Could be more than one entry; each with a unique key size(), equals(), clear() CS-2851 Dr. Mark L. Hornick 4
The put() method is used to insert elements public V put (K key, V value) /** * Ensures that there is an element in this Tree. Map object * with the specified key&value pair. If this Tree. Map * object had an element with the specified key before * this method was called, the previous value associated * with that key has been returned. Otherwise, null * has been returned. * The worst. Time (n) is O (log n). * * @param key – the specified key * @param value – the specified value * @return the previous value associated with key, if * there was such a mapping; otherwise, null. * */ CS-2851 Dr. Mark L. Hornick 5
The contains. Key() method determines if the Tree. Map contains an entry with a specified unique key public boolean contains. Key (Object key) /** * Determines if this Tree. Map object contains a mapping * with a specified key. * The worst. Time (n) is O (log n). WHY? ? ? * * @param key – the specified key * * @return true – if this Tree. Map object contains a * mapping with the specified key; otherwise, false. */ CS-2851 Dr. Mark L. Hornick 6
The contains. Value() method determines if the Tree. Map contains an entry with a specified value public boolean contains. Value (Object value) /** * Determines if this Tree. Map object contains a mapping * with a specified value. * The worst. Time (n) is O (n). WHY? ? ? * * @param value – the specified value * * @return true – if this Tree. Map object contains * at least one mapping with the specified value; * otherwise, false. */ CS-2851 Dr. Mark L. Hornick 7
Some other Map interface methods implemented in Tree. Map l public V remove (Object key) l l l public V get (Object key) l l Removes an entry with the specified key Returns the associated value Gets the associated value of an entry with the specified key public Set entry. Set() l Returns a Set object reference l Which can be iterated CS-2851 Dr. Mark L. Hornick 8
The two Tree. Map constructors (required by Sorted. Map) public Tree. Map( ) { // comparator = null; key class implements // Comparable interface } // default constructor public Tree. Map (Comparator<? super K> c) { comparator = c; // c implements Comparator interface } // one-parameter constructor CS-2851 Dr. Mark L. Hornick 9
The Comparator interface allows a user of a class to override how that class performs comparisons of keys Interface Comparator<T> { // Compares its two arguments for order. int compare(T o 1, T o 2); // Indicates whether some other object is "equal to" this Comparator boolean equals(Object obj); } l For example, the String objects can be ordered by the length of the string instead of lexicographically CS-2851 Dr. Mark L. Hornick 10
Tree Sets CS-2851 Dr. Mark L. Hornick 11
A Tree. Set is an ordered Collection in which duplicate elements are not allowed l The Tree. Set class has all of the methods in the Collection interface l l add, remove, size, contains, … plus to. String (inherited from Abstract. Collection) public class Tree. Set<E> extends Abstract. Set<E> implements Sorted. Set<E>, Cloneable, java. io. Serializable { CS-2851 Dr. Mark L. Hornick 12
The Tree. Set constructors public Tree. Set( ) // assumes elements ordered by // Comparable interface public Tree. Set (Comparator<? super E> c) // assumes elements ordered // by Comparator c public Tree. Set (Collection<? extends E> c) // copy constructor; assumes elements ordered // by Comparable interface CS-2851 Dr. Mark L. Hornick 13
The Tree. Set class is implemented with Tree. Map in which all of the values are the same l l l The Tree. Set elements are the keys in the underlying map A dummy object is associated with the value in the underlying map All the work is done by the underlying map /** * Initializes this Tree. Set object to be empty. public Tree. Set( ) { this (new Tree. Map<E, Object>( )); } // default constructor CS-2851 Dr. Mark L. Hornick 14
- Slides: 14