Chapter 20

Advanced Data Structures


Chapter Goals

Sets

Fundamental Operations on a Set

Sets

Iterator

Set Classes and Interface in the Standard Library

Set Classes and Interface in the Standard Library

Code for Creating and Using a HashSet

Listing All Elements with an Iterator

Iterator iter = names.iterator();
while (iter.hasNext())
{
   String name = (String) iter.next();
   //do something here
}

File SetTest.java

Maps

Maps

An Example of a Map

A Map

Code for Creating and Using a HashMap

Code for Creating and Using a HashMap

 

Printing key/value Pairs

Set keySet = favoriteColors.keySet();
Iterator iter = keySet.iterator();
while (iter.hasNext())
{
   Object key = iter.next();
   Object value = favoriteColors.getkey();
   System.out.println(key + "->" + value);
}

Map Classes and Interface in the Standard Library

Map Classes and Interface in the Standard Library

File MapTest.java

Hash Tables

Sample Strings and Their Hash Codes

Sample Strings and Their Hash Codes

Simplistic Implementation of a Hash Table

Simplistic Implementation of a Hash Table

Simplistic Implementation of a Hash Table

Problems with Simplistic Implementation

Solutions

Hash Table with Linked Lists to Store Elements with Same Hash Code

Hash Table with Linked Lists

Algorithm for Finding an Object x in a Hash Table

Hash Tables

File HashSet.java

File SetTest.java

Hash Function

Computing Hash Codes

A hashCode method for the Coin class

A hashCode method for the Coin class

class Coin
{
   public int hashCode()
   {
      int h1 = name.hashCode();
      int h2 = new Double(value).hashCode();
      final int HASH_MULTIPLIER = 29;
      int h = HASH_MULTIPLIER * h1 + h2:
      return h
   }
   . . .
}

Creating Hash Codes for your classes

Creating Hash Codes for your classes

HashMap

File Coin.java

File HashCodeTest.java

Binary Search Trees

A Binary Search Tree

Binary Search Tree

A Binary Tree That Is Not a Binary Search Tree

Binary Tree That Is Not a Binary Search Tree

Implementing Tree Classes

 

Public interface for Tree and Node classes

class Tree
{
   public Tree() { . . . }
   public void insert(Comparable obj) { . . . }
   public void print() { . . . }

   private Node root;

   private class Node
   {
      public void insertNode(Node newNode) { . . . }
      public void printNodes() { . . . }

      public Comparable data;
      public Node left;
      public Node right;
   }
}

Insertion Algorithm

Insertion Algorithm - Tree class

class Tree
{
   . . .
   public void insert(Comparable obj)
   {
      Node newNode = new Node();
      newNode.data = obj;
      newNode.left = null;
      newNode.right = null;
      if (root == null) root = newNode;
      else root.insertNode(newNode);
   }
   . . .
}

Insertion Algorithm - Node class

private class Node
{
   public void insertNode(Node newNode)
   {
      if (newNode.data.compareTo(data) < 0)
      {
         if (left == null) left = newNode;
         else left.insertNode(newNode);
      }
      else
      {
         if (right == null) right = newNode;
         else right.insertNode(newNode);
      }
   }
   . . .
}

Binary Search Tree After Four Insertions

The tree before the insertion of node with data "Romeo"

Binary Search Tree After Four Insertions

Binary Search Tree After Five Insertions

The tree after the insertion of node with data "Romeo"

Binary Search Tree After Five Insertions

Algorithm for Printing Elements in Sorted Order

Tree Class Method to Implement Printing in Sorted Order

class Tree
{
   public void print()
{
if (root != null)
root.printNodes();
} . . . }

Node Class Method to Implement Printing in Sorted Order

private class Node
{
      public void printNodes()
{
if (left != null)
left.printNodes();
System.out.println(data);
if (right != null)
right.printNodes();
} . . . }

Binary Search Tree

File TreeTest.java

File Tree.java

TreeSet and HashSet

To use a TreeSet

To use a TreeMap

File TreeSetTest.java

This program constructs a TreeSet of Coins using the CoinComparator object