{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch16 - Chapter Sixteen Advanced Data Structures Chapter...

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

View Full Document Right Arrow Icon
Chapter Sixteen: Advanced Data Structures
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
Sets Set: unordered collection of distinct elements Elements can be added, located, and removed Sets don't have duplicates
Background image of page 3

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

View Full Document Right Arrow Icon
A Set of Printers
Background image of page 4
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)
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
Set Classes and Interface in the Standard Library
Background image of page 7

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

View Full Document Right Arrow Icon
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
Background image of page 8
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") { . . .}
Background image of page 9

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

View Full Document Right Arrow Icon
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 }
Background image of page 10
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();
Background image of page 11

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

View Full Document Right Arrow Icon
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( "Q" )) 37: done = true ; 38: else 39: { 40: names.remove(input); 41: print(names); 42: } 43: } 44: } Continued
Background image of page 12
ch16/set/SetDemo.java (cont.) 45: 46: /** 47: Prints the contents of a set of strings.
Background image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}