Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.

Slides:



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

Stacks, Queues, and Linked Lists
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.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CSSE221: Software Dev. Honors Day 14 Announcements Announcements Pass in Homework 5 now Pass in Homework 5 now Questions on Cars, Trucks, Trains? Questions.
Stacks, Queues, and Deques
Marc Smith and Jim Ten Eyck
Stacks, Queues, and Deques
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Information and Computer Sciences University of Hawaii, Manoa
Data structures and algorithms in the collection framework 1 Part 2.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Big Java Chapter 16.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Data structures and algorithms in the collection framework 1.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
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,
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Chapter 19 Java Data Structures
Chapter 17 Object-Oriented Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Arrays versus ArrayList
Data Structures II AP Computer Science
Workshop for CS-AP Teachers
Presentation transcript:

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures

Georgia Institute of Technology Learning Objectives Understand at the conceptual level –The need to group objects –Limitations of Arrays –Collections –Lists and Linked Lists –Sets and Maps –Stacks and Queues –Trees

Georgia Institute of Technology Grouping Objects We often group objects –A list of items to buy at a grocery store –Your friends names and phone numbers –Your homework for each class –A record of all of your ancestors –A sorted list of people in a class

Georgia Institute of Technology Array Limitations You can use arrays to store multiple objects –You need to know many items there will be You specify the size when you create an array Item[] shoppingList = new Item[10]; –What happens if the array runs out of space? If you try to add an element passed the last valid index you get –java.lang.ArrayIndexOutOfBoundsException You could create a bigger array You would have to copy all the elements from the old array to the new array –What if you don’t need all the space in an array?

Georgia Institute of Technology Collection Classes Java has collection classes to handle grouping objects –The classes don’t require you to know how many objects you will need to store The collections will grow and shrink as needed There are different types of collections depending on what you need –Keep the order of the objects - List –Make sure there are no duplicates – Set –Associate one object with another - Map

Georgia Institute of Technology Collection Exercise Look up the Collection Interface –How do you add objects to a collection? –Is there a way to add two collections together? –Is there a way to get an intersection of two collections? –Is there a way to remove an object from a collection? –How do you empty a collection? –Can you get an array from a collection?

Georgia Institute of Technology Collections hold object references When you add an object to a collection –You add a reference to the object Not a copy of the object –Many collections can hold references to the same object –Variables may also reference the same object Cheerios: Item Lettuce: Item Ham: Item Eggs: Item

Georgia Institute of Technology List and Set Interfaces and Classes > Collection > List > Set > SortedSet ArrayList Vector LinkedList TreeSet HashSet

Georgia Institute of Technology List We often keep ordered lists of things –“To do” list –People in a line –Parts A list has an order –First thing, second thing, third thing, etc. Lists may have duplicate items You can get, add, or remove an item anywhere in a list

Georgia Institute of Technology Java Lists The first index is 0 –The last valid index is list.size() – 1 ArrayList is a class that implements the List interface –Using an array and allows null values in the list Vector is an older class that also uses an array –It is like ArrayList but it is synchronized Linked list is a class that implement the List interface –Using a linked structure, not an array

Georgia Institute of Technology Linked List - java.util.LinkedList A linked list has nodes that contain data and a reference to the next node A doubly linked list has references to previous nodes as well SueMaryTashanull head SueMaryTashanull headtail null

Georgia Institute of Technology Ideas for Teaching Linked Lists Give random students a paper that tells them who the next and previous student is –Give one student the name of the first person in the list Walk through –adding a new student to the front of the list –getting the 5 th person in the list –removing the 3 rd person in the list –removing the 1 st person in the list

Georgia Institute of Technology Arrays versus Linked List A book is like an array –The pages are ordered sequentially –It is easy to find a particular page A magazine article is like a linked list –Has groups of pages and –a reference to the next group of pages A treasure hunt is like a linked list –You start with one clue that takes you to the location of the next clue

Georgia Institute of Technology ArrayList versus LinkedList If you need to access items randomly –Use an ArrayList Quick to access a random location Can be slower to add to and remove from –If it needs to create a new array and copy old items If you are doing lots of adding/removing from a list –Use a LinkedList Quick to add to or remove from Slow to do random access

Georgia Institute of Technology Using Iterator One way to access all elements of a List is to use a for loop and increment the index from 0 to < list.size() –Use the index to get items from the list item = (Item) itemList.get(index); Another approach is to use an iterator Iterator iterator = itemList.iterator(); while (iterator.hasNext()) item = (Item) iterator.next();

Georgia Institute of Technology Iterator Exercise Is it better to use an iterator or an index to get all of the elements –of an ArrayList? –of a LinkedList? What about if you want to access every other element –of an ArrayList? –of a LinkedList? Which should you use if you don’t know the implementing class?

Georgia Institute of Technology ListIterator Inherits from Iterator Adds –The ability to traverse a list in either direction –The ability to modify the list during iteration Add a new element before the current next element –public void add(Object obj); Change the last accessed element –public void set(Object obj);

Georgia Institute of Technology ListNode AP Class Has value and next fields –Can get and set the fields Has a constructor that take the value and next node Uses the keyword null to indicate the end of the linked list ListNode Object value ListNode next public Object getValue() public ListNode getNext() public void setValue(Object value) public void setNext(ListNode node)

Georgia Institute of Technology Loop through a linked list with ListNode Start with a reference to the head of the list Each time through the loop move the reference to the next node Stop the loop when the reference is null –Continue while the reference is not null ListNode node = null; for (node = head; node != null; node = node.getNext())

Georgia Institute of Technology Testing the Loop Does this work when head is null? Does it work when there is one node in the list? Does it work when there is more than one node in the list? SueMaryTashanull head Suenull head null

Georgia Institute of Technology Add to the front of a linked list Set the new nodes next to the node referenced by head Change head to point to the new node SueMaryTashanull head FredSueMaryTashanull Frednull head

Georgia Institute of Technology Stacks A stack holds objects with the last object put in the stack being the first one returned –Last-in-first-out structure (LIFO) Like a stack of cafeteria plates Or a Pez container Stacks are used to hold the list of operations that you might want to undo –When you click “Undo” the last thing you did is undone

Georgia Institute of Technology Teaching Stacks Have each student put a book on a stack of books –Then ask a student to take off a book from the stack Where did people put the new books? Where did people take books from?

Georgia Institute of Technology Stack AP Interface public interface Stack { /** Method that returns true if the stack is empty else false */ public boolean isEmpty(); /** Method that adds the passed object to the stack */ public void push(Object obj); /** Method that returns the top of the stack and removes the object from the stack */ public Object pop(); /** Method that returns the top of the stack but doesn't remove the object */ public Object peekTop(); }

Georgia Institute of Technology Implementing the Stack Interface You could use an array –But you don’t know how many things will be in the stack You could use a list –ArrayList or LinkedList With a stack you add things to one end –The top And remove things from one end –The top

Georgia Institute of Technology Implementing Stack with ArrayList How would you check if the stack is empty if you use an ArrayList to hold the items? –It is empty if the list.size() is zero How would you push an object on the stack? –You could add it to the 0 index but then you would have to move all other items in the array –So add it to the end of the array list.add() How would you pop an object from the top of the stack –The last item entered in an ArrayList is at the number of objects in the array - 1 Object object = list.remove(list.size() – 1) How would you peek at the top object? –Object object = list.get(list.size() – 1)

Georgia Institute of Technology Queues A queue holds objects with the first object put in the queue the first one returned –First-in-first-out structure (FIFO) Like the ticket line at the movies Or a car wash with cars moving through Use queues to track events and objects –A queue of requests for printing Handle the first one before the next one –A queue of people in line to buy tickets for a movie People at the front of the queue buy tickets first

Georgia Institute of Technology Teaching Queues Have some students form a line as if in line to buy tickets for a movie –Who should be waited on first? Who would be waited on next? –When new people come where do they enter the line?

Georgia Institute of Technology Queue AP Interface public interface Queue { /** returns true if the queue is empty else false */ public boolean isEmpty(); /** adds the object to the end of the queue */ public void enqueue(Object obj); /** removes the first object in the queue and returns it */ public Object dequeue(); /** returns the first object in the queue without removing it */ public Object peekFront(); }

Georgia Institute of Technology Implementing the Queue Interface You could use an array –But you don’t know how many objects it will need to hold You could use an ArrayList –And add new objects to the end of the array –But when you remove an object from the front (0 index) all the remaining objects would have to move down one You could use LinkedList

Georgia Institute of Technology Implementing Queue with LinkedList How would you check if the queue is empty if you use a LinkedList to hold the items? –It is empty if the list.size() is zero How would you add an object to the end of the queue? –list.addLast(object); How would you remove and return the first object in the queue? –list.remove(0); How would you peek at the first object? –list.get(0);

Georgia Institute of Technology Set A set does not preserve order –The order things are retrieved from a set is not necessarily the same order they were placed in a set Sets do not allow duplicate elements –elementA.equals(elementB) –If you try to add an element that is equal to another element of the set it won’t add it And will return false

Georgia Institute of Technology Set Classes HashSet –Uses equals and hashCode to compare objects and to check for duplicates TreeSet –Objects must implement Comparable and are sorted based on the results of compareTo > Set > SortedSet TreeSet HashSet

Georgia Institute of Technology Maps Maps hold key and value pairs –Use a key to put a value into the map –Use a key to get a value from a map –There can’t be duplicate keys –There can be duplicate values A value can be associated with different keys Used to look up associated data –Like look up a customer record from a phone number –Or like safety deposit boxes

Georgia Institute of Technology Map Interface Get the number of keys in the map public int size(); Put a value in the map for the given key –Returns the old object stored for this key public Object put(Object key, Object value); Get a value from the map for the given key public Object get(Object key); Check if the key is used in the map public boolean containsKey(Object key); Get a set of the keys used in the map public Set keySet();

Georgia Institute of Technology Map Interfaces and Classes > Map > SortedMap TreeMap HashMapHashtable

Georgia Institute of Technology Map Classes HashMap –Stores keys and values without regards to order entered –Allows null values and a null key Hashtable –Older class like HashMap –Synchronized TreeMap –Holds keys in sorted order

Georgia Institute of Technology Hashing HashMap and Hashtable use hashing on the key to find the location where the value is stored –Using the hashCode() method inherited from Object –This method is overridden for String –You should override this method in your classes Maps the key to an index in an array

Georgia Institute of Technology Hashing Procedure When you put a value in a HashMap for a key –First the hashCode method is called on the key object –This returns an int value which is mapped from 0 to the array length – 1 Often by using remainder (%) –There may be a value at that index from a different key This is called a collision

Georgia Institute of Technology Handling Collisions The array is often an array of lists –A bucket that holds more than one hash node –A good hashCode() method should result in few collisions and small lists When more than one key has the same index –The hash node is added to the list When you look for a value based on a key –If it maps to an index with a list It looks for the key using equals

Georgia Institute of Technology hashCode() Method The goal is to get a good spread of int results Use some combination of fields –Like the hashCode for some String fields added to some prime number times some other field Different keys can result in the same hashCode() result The same key object must give the same hashCode() result

Georgia Institute of Technology Trees Linked lists have nodes that hold a value and a reference to the “next” node What if you need to track more than one “next” node? –Like you want to record your ancestors You can use a tree –Each tree node has a value (a person) –And a reference to the person’s mother –And a reference to the person’s father

Georgia Institute of Technology Example Ancestor Tree Barbara Ericson Janet HundCharles Ericson Opal PetersFrancis Hund Edna Wenzel Edward Ericson root

Georgia Institute of Technology Binary Tree Each tree node has at most one parent node Each tree node can have at most 2 children The top node in the tree is called the root Tree nodes without any children nodes are called leaves root leaves Left child Right child

Georgia Institute of Technology Tree Node AP Class Has fields: value, left, and right Can get and set all fields Has a constructor that takes a value, left tree node and right tree node TreeNode private Object value private TreeNode left private TreeNode right public Object getValue() public TreeNode getLeft() public TreeNode getRight() public void setValue(Object o) public void setLeft(TreeNode n) public void setRight(TreeNode n)

Georgia Institute of Technology Trees are Recursive Each tree node is the root of a sub-tree of the original tree This allows the use of recursion –A method invokes itself On a subset of the original problem Like a subtree –There has to be an end condition That stops the recursion No more subtrees

Georgia Institute of Technology Get the Number of Nodes in a Tree If the root is null –The number of nodes is 0 If the root isn’t null –Add one to the count –Add to the count the number of nodes in the left subtree –Add to the count the number of nodes in the right subtree

Georgia Institute of Technology Get the Number of Nodes Method Some books use a class (static) method to count the number of nodes –And pass in the current node public static int getNumNodes(TreeNode node) { if (node == null) return 0; else return 1 + getNumNodes(node.getLeft()) + getNumNodes(node.getRight()); }

Georgia Institute of Technology What is wrong with this? Static methods are used when there is no current object –Or for general methods In this case there is a current tree node –And the method does operate on it –It is explicitly passed to the method So this should be an object method –And the current object should be implicitly passed

Georgia Institute of Technology Modified Get Number of Nodes public int getNumNodes() { int count = 0; // increment count count = count + 1; // add to the count the number of nodes in the left subtree if (left != null) count = count + left.getNumNodes(); // add the to count the number of nodes in the right subtree if (right != null) count = count + right.getNumNodes(); return count; }

Georgia Institute of Technology Tree class getNumNodes() public int getNumNodes() { int count = 0; // the default is no nodes // if the root isn't null get the number of nodes if (root != null) count = root.getNumNodes(); return count; }

Georgia Institute of Technology Tree Traversals In-order traversal (left-data-right) –Do the recursive call on the left subtree –Do something with the value at the node –Do the recursive call on the right subtree Pre-order traversal (data-left-right) –Do something with the value at the node –Do the recursive call on the left subtree –Do the recursive call on the right subtree Post-order traversal (left-right-data) –Do the recursive call on the left subtree –Do the recursive call on the right subtree –Do something with the value at the node

Georgia Institute of Technology Tree Traversals

Georgia Institute of Technology Binary Search Trees (BSTs) A binary tree where the value at each node –is greater than the values in all of the nodes in the left subtree –and less than the values in all of the nodes in the right subtree

Georgia Institute of Technology Binary Search Tree Orders values –Usually using the Comparable Interface Allows for quick search –For a “well filled” tree O(log n) –And quick insertion and deletion of nodes An in-order traversal of a BST will give values in ascending order Used by TreeSet and TreeMap

Georgia Institute of Technology Priority Queue Used to store items with various priorities –Like printer requests Can hold several items with the same priority Can item an object to the queue Can get the item with the highest priority –Often considered to be the “minimum” item

Georgia Institute of Technology PriorityQueue AP Interface public interface PriorityQueue { /** true if the queue is empty else false */ public boolean isEmpty(); /** add an object of the type comparable to the queue */ public void add(Object object); /** remove and return the minimum object */ public Object removeMin(); /** return the minimum object, but do not remove it */ public Object peekMin(); }

Georgia Institute of Technology How to Implement a Priority Queue? You could use an array –But you don’t know how many items will be stored in the queue –You would have to search the array for the minimum item You could use a list –But you would still need to search the list You could use a binary search tree –But if there are frequent insertions and deletions this can be slow

Georgia Institute of Technology Heaps A heap is a complete binary tree –Each level other than the last one is full of nodes –The last level must have all missing nodes grouped to the right The value at each node is less than the values –In both the left and right subtrees The minimum value is at the root

Georgia Institute of Technology Adding a Node to a Heap Add it to the first missing child reference Then move node values as required to satisfy the requirement that the each node’s value is less than the values in the left and right subtree –Called Heapify

Georgia Institute of Technology Data Structures Exercise What data structure would you use to hold a known number of students in an order? What data structure would you use to store your friends names and cell phone numbers? What data structure would you use to store orders in a fast-food restaurant? What data structure would you use to store recent commands to allow undo? What data structure would you use to store a sorted list of teachers?

Georgia Institute of Technology Summary Collection classes hold groups of objects –Collections can grow and shrink Lists hold objects in order and allow duplicate objects –Linked lists have nodes that hold a value and a reference to the next node Doubly linked list nodes also hold a reference to the previous node Sets hold objects without preserving order and do not allow duplicate objects in the set Maps associate a key object with a value object Trees have nodes that hold values and references to children nodes