ch16 - Chapter Sixteen: Advanced Data Structures Chapter...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter Sixteen: Advanced Data Structures Chapter Goals To learn about the set and map data types To understand the implementation of hash tables To be able to program hash functions To learn about binary trees To be able to use tree sets and tree maps To become familiar with the heap data structure To learn how to implement the priority queue data type To understand how to use heaps for sorting Sets Set: unordered collection of distinct elements Elements can be added, located, and removed Sets don't have duplicates A Set of Printers Fundamental Operations on a Set Adding an element Adding an element has no effect if the element is already in the set Removing an element Attempting to remove an element that isn't in the set is silently ignored Containment testing (does the set contain a given object?) Listing all elements (in arbitrary order) Sets We could use a linked list to implement a set Adding, removing, and containment testing would be relatively slow There are data structures that can handle these operations much more quickly Hash tables Trees Standard Java library provides set implementations based on both data structures HashSet TreeSet Both of these data structures implement the Set interface Set Classes and Interface in the Standard Library Iterator Use an iterator to visit all elements in a set A set iterator does not visit the elements in the order in which they were inserted An element can not be added to a set at an iterator position A set element can be removed at an iterator position Code for Creating and Using a Hash Set //Creating a hash set Set<String> names = new HashSet<String>(); //Adding an element names.add("Romeo"); //Removing an element names.remove("Juliet"); //Is element in set if (names.contains("Juliet") { . . .} Listing All Elements with an Iterator Iterator<String> iter = names.iterator(); while (iter.hasNext()) { String name = iter.next(); Do something with name } // Or, using the "for each" loop for (String name : names) { Do something with name } ch16/set/SetDemo.java Continued 01: import java.util.HashSet; 02: import java.util.Scanner; 03: import java.util.Set; 04: 05: 06: /** 07: This program demonstrates a set of strings. The user 08: can add and remove strings. 09: */ 10: public class SetDemo 11: { 12: public static void main(String args) 13: { 14: Set<String> names = new HashSet<String>(); 15: Scanner in = new Scanner(System.in); 16: 17: boolean done = false ; 18: while (!done) 19: { 20: System.out.print( "Add name, Q when done: " ); 21: String input = in.next(); ch16/set/SetDemo.java (cont.) 22: if (input.equalsIgnoreCase( "Q" )) 23: done = true ; 24: else 25: { 26: names.add(input); 27: print(names); 28: } 29: } 30: 31: done = false ; 32: while (!done) 33: { 34: System.out.print( "Remove name, Q when done: " ); 35: String input = in.next(); 36: if (input.equalsIgnoreCase((input....
View Full Document

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.

Page1 / 164

ch16 - Chapter Sixteen: Advanced Data Structures Chapter...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online