Download presentation
Presentation is loading. Please wait.
1
Podcast Ch20b Title: TreeMap Design
Description: Entry as an inner class; TreeMap design Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp
2
Entry as an Inner-Class Within TreeMap
public class TreeMap<K,V> implements OrderedMap<K,V> { . . . // declares a binary search tree node object private static class Entry<K,V> implements Map.Entry<K,V> // node data K key; V value; // child links and link to the node's parent Entry<K,V> left, right, parent;
3
Entry as an Inner-Class Within TreeMap (cont)
// constructor that initializes the value // and parent fields and sets the link // fields left and right to null public Entry(K key, V value, Entry<K,V> parent) { this.key = key; this.value = value; left = null; right = null; this.parent = parent; } // returns the key public K getKey() { return key; } // returns the value associated with the key public V getValue() { return value; }
4
Entry as an Inner-Class Within TreeMap (concluded)
// updates the value currently associated // with the key with a new one and returns // the original value public V setValue(V value) { V oldValue = this.value; this.value = value; return oldValue; } // used by TreeMap toString() to list the // pair as "key=value" public String toString() { return key + "=" + value; }
5
Student Question There are methods to getValue, setValue, and getKey
Carefully explain why isn’t there a method to setKey?
6
TreeMap Class Design The TreeMap class builds a binary search tree of Entry objects. Variables - The private instance variable root is a reference to an Entry node that defines the search tree. Two integer variables mapSize and modCount maintain the size of the collection and the number of tree modifications (insertions and deletions).
7
TreeMap Class Design (continued)
The class constructor creates an empty map and toString() displays the entries as comma-separated elements enclosed in braces ("{", "}"). Each element has the form "key=value". The other methods implement OrderedMap interface operations.
8
TreeMap Class Design (cont)
The figure shows a UML diagram illustrating the design of the TreeMap class. The inner classes IteratorImpl, KeyIterator, and EntryIterator are involved with the collection views of a map.
9
TreeMap Class – Basic Class Structure
public class TreeMap<K,V> implements OrderedMap<K,V> { // root defines the search tree of Entry nodes private Entry<K,V> root; // size of the map private int mapSize; // modCount maintains a record of changes // to the map for iterators private int modCount;
10
TreeMap Class – Basic Class Structure (concluded)
// constructor creates an empty map public TreeMap() { root = null; mapSize = 0; modCount = 0; } < Methods defined by the OrderedMap interface > . . . // declares a binary search tree node object private static class Entry<K,V> implements Map.Entry<K,V> { }
11
Student Question Why are there so many (four!) inner classes?
I can understand the keyIterator and the entryIterator, but what is the iteratorImpl? Why isn’t there a value iterator?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.