Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Chapter 10: Data Structures II
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 16.  Data structures that take control of organizing elements  Elements not in fixed positions  Advantage – better performance Adding Removing.
Chapter 19 Java Data Structures
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
Java Collections Framework A presentation by Eric Fabricant.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
DataStructures1 Barb Ericson Georgia Tech July 2008.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
CHAPTER 20 Advanced Data Structures. CHAPTER GOALS To learn about set and map data types To understand the implementation of hash tables To be able to.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Search Trees Binary search trees allow for fast insertion and removal of elements They are specially designed for fast searching A binary tree consists.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 16 – Advanced Data Structures.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Binary Search Trees (BST) Let’s look at some pics …and some code.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
(c) University of Washington20-1 CSC 143 Java Trees.
Advanced Data Structures Advanced Programming ICOM 4015 Lecture 18 Reading: Java Concepts Chapter 21.
Slides by Donald W. Smith
Chapter 12 – Data Structures
Recursive Objects (Part 4)
Chapter 19 Java Data Structures
Chapter 16 – Advanced Data Structures
Week 6 - Wednesday CS221.
Section 8.1 Trees.
Chapter 10: Non-linear Data Structures
Binary Trees.
Lecture 12 CS203 1.
CSC 143 Java Trees.
Data Structures II AP Computer Science
Presentation transcript:

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

Department of Computer Science2 Lectures - Today - Collections Continued - Sets - Trees - Hashing

Department of Computer Science3 So far Arrays Arraylists LinkedList  Stacks  Queues Items are all placed at a position in the data structure  LinkedList has an iterator with an “add” method

Department of Computer Science4 Sets A set is a collection of items that does not permit duplicates  Adding an existing item has no effect  Removing an item not in the set has no effect Java has a Set interface with two implementations  HashSet (uses hashing to store items)  TreeSet (uses a tree to store items – Sorted)

Department of Computer Science5 Example import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetTest { public static void main(String[] args) { Set names = new HashSet (); names.add(“Harry”); // add lots more

Department of Computer Science6 Continued names.add("Jane"); names.add("Tom"); names.add("Richard"); names.add("Tom"); names.add("Barbara"); print(names); }

Department of Computer Science7 Printing private static void print(Set s) { System.out.print("{ "); for (String x : s) { System.out.print(x); System.out.print(" "); } System.out.println("}"); }

Department of Computer Science8 TreeSet Changing only two lines of code lets us have a Tree behind the scenes Note the order of items!

Department of Computer Science9 Maps Similar idea can be developed to make associations Two sets are kept in association Set of keys -> Set of Values Two keys may refer to same value Names and phone numbers Java has a Map Interface Two maps are implemented  HashMap  TreeMap

Department of Computer Science10 Test program for maps import java.util.HashMap; import java.util.Iterator; import java.util.Set; import java.util.Map; public class maps { public static void main(String[] args) {

Department of Computer Science11 Continued Map yellowpages = new HashMap (); yellowpages.put("Harry", "123456"); yellowpages.put("Jane", "012345"); yellowpages.put("Tom", "123458"); yellowpages.put("Richard", "123457"); yellowpages.put("Barbara", "123454"); yellowpages.put("Barbara", "987654"); // assignment

Department of Computer Science12 Printing out maps Set keySet = yellowpages.keySet(); for (String key : keySet) { String value = yellowpages.get(key); // value assoc with key System.out.println(key + "->" + value); }

Department of Computer Science13 What is hashing? Technique for finding elements without making a linear search through all elements Uses a hash function is a function that computes an integer value (called a hash code) from an object (different objects should have different hash codes) Object class has a hashCode method int h = x.hashCode();

Department of Computer Science14 Hash functions Should avoid collisions (two or more different objects with the same hash code) If you have your own objects you write:  public int hashCode( )  When adding x.equals(y) => x.hashCode() == y.hashCode (avoid duplicates) Eg for a circle so that circles of different radii are stored separately. Forgetting to implement hashCode means the one assoc with Object is used – not a good idea.

Department of Computer Science15 Example Consider Set circles = new HashSet (); circles.add(new Circle(5)); if (circles.contains(new Circle(5)) System.out.println(“Circle of radius 5 exists”);

Department of Computer Science16 Binary Search Trees Finding things is O(log N) with binary search With arrays insertion and deletion are O(N) Binary search trees are fast at everything Nodes of a binary search tree have two children Data values of all descendants to the left of any node are less than the data value stored at that node (similarly right and greater)

Department of Computer Science17 Tree of names For the list {Adam, Eve, Harry, Jane, Richard, Tom} Jane (root) – children Eve and Richard Eve – children Adam and Harry Richard – children Tom and null Adam, Harry and Tom are leaf nodes (both children null)

Department of Computer Science18 Implementation overview public class BinarySearchTree { // need reference to root (like first in LL) private node root; // inner class for node private class Node { public void addNode(Node newNode) { } public Comparable data; public Node left; public Node right; }

Department of Computer Science19 Tree Operations Tree Traversal (sorted order)  Print left subtree  Print data  Print right subtree Called Inorder traversal Preorder traversal (root, left, right) Postorder traversal (left, right, root) (3+4*5)

Department of Computer Science20 Trees and Recursion // Note this is a method of the Node inner class public void printNodes( ) { if (left != null) left.printNodes( ); System.out.println(data); if (right != null) right.printNodes( ); } // Method of BinarySearchTree public void print( ) { if (root != null) root.printNodes( ); }

Department of Computer Science21 HashSet or TreeSet If you have a good hash function for your objects then performance will be better than trees Hash sets are at the mercy of the hash function Iterators visit TreeSet data in sorted order (hash order is random) TreeSet requires objects to implement Comparable (or Comparator)

Department of Computer Science22 How to choose – ask questions How are elements accessed?  Doesn’t matter  By key (eg a/c #)(MAP)  Integer index(ARRAYLIST) Does data order matter?  Doesn’t matter  Must be sorted(TREESET)  Store in insert order(LL, ARRAYLIST)

Department of Computer Science23 More questions What operations need to be fast?  Doesn’t matter (small dataset)  Add/remove(LL)  Search(SET) (DM = ARRAYLIST) Hash or Tree?  Strings should be hashed  Own class need to check hashCode and equals  Trees may need a Comparator