cs-2851 dr. mark l. hornick 1 tree maps and tree sets the jcf binary tree classes

14
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

Upload: ruby-anthony

Post on 24-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

1

Tree Maps and Tree Sets

The JCF Binary Tree classes

Page 2: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

2

Map definition review

A map is a collection in which each Entry element has two parts: a unique key part a value part (which may not be unique)

Each unique key “maps” to a corresponding value 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.

key

value

Entry

Page 3: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

3

TreeMap :The JCF implementation of a Binary Tree

TreeMap stores a Map in a red-black tree, ordered by (unique) keyspublic class TreeMap<K, V>

implements SortedMap<K, V> extends AbstractMap<K, V> Example:

TreeMap<Integer, Student> students;//Integer: unique student ID number//Student: student object

Page 4: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

4

JCF TreeMap methods

Implements the Map interface e.g. no iterator() method

But Map does define Put(k,v) – insert a key/value entry into the Binary Tree Remove(k) – remove the entry with the specified key containsKey(k) – search for the entry with key k containsValue(v) – search for an entry with value v

Could be more than one entry; each with a unique key size(), equals(), clear()

Page 5: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

5

The put() method is used to insert elementspublic V put (K key, V value)/** * Ensures that there is an element in this TreeMap object * with the specified key&value pair. If this TreeMap * 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 worstTime (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. * */

Page 6: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

6

The containsKey() method determines if the TreeMap contains an entry with a specified unique keypublic boolean containsKey (Object key)/*** Determines if this TreeMap object contains a mapping * with a specified key.* The worstTime (n) is O (log n). WHY???** @param key – the specified key** @return true – if this TreeMap object contains a* mapping with the specified key; otherwise,

false.*/

Page 7: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

7

The containsValue() method determines if the TreeMap contains an entry with a specified value

public boolean containsValue (Object value) /*** Determines if this TreeMap object contains a mapping * with a specified value.* The worstTime (n) is O (n). WHY???** @param value – the specified value** @return true – if this TreeMap object contains * at least one mapping with the specified value;* otherwise, false.*/

Page 8: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

8

Some other Map interface methods implemented in TreeMap

public V remove (Object key) Removes an entry with the specified key Returns the associated value

public V get (Object key) Gets the associated value of an entry with the

specified key public Set entrySet()

Returns a Set object reference Which can be iterated

Page 9: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

9

The two TreeMap constructors(required by SortedMap)

public TreeMap( ) {

// comparator = null; key class implements // Comparable interface

} // default constructor

public TreeMap (Comparator<? super K> c){

comparator = c; // c implements Comparator interface

} // one-parameter constructor

Page 10: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

10

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 o1, T o2); // Indicates whether some other object is "equal

to" this Comparatorboolean equals(Object obj);}

For example, the String objects can be ordered by the length of the string instead of lexicographically

Page 11: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

11

Tree Sets

Page 12: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

12

A TreeSet is an ordered Collection in which duplicate elements are not allowed The TreeSet class has all of the methods in the

Collection interface add, remove, size, contains, … plus toString (inherited from AbstractCollection)

public class TreeSet<E>

extends AbstractSet<E>

implements SortedSet<E>, Cloneable,

java.io.Serializable

{

Page 13: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

13

The TreeSet constructors

public TreeSet( )// assumes elements ordered by // Comparable interface

public TreeSet (Comparator<? super E> c)// assumes elements ordered// by Comparator c

public TreeSet (Collection<? extends E> c)// copy constructor; assumes elements ordered// by Comparable interface

Page 14: CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes

CS-2851Dr. Mark L. Hornick

14

The TreeSet class is implemented with TreeMap in which all of the values are the same

The TreeSet 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 TreeSet object to be empty.public TreeSet( ) {

this (new TreeMap<E, Object>( ));} // default constructor