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.

Slides:



Advertisements
Similar presentations
Data Structures.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
Stacks, Queues, and Deques
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Stacks, Queues, and Deques
Java's Collection Framework
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Computer Science Department Data Structures and Algorithms Lecture 1.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Information and Computer Sciences University of Hawaii, Manoa
Big Java Chapter 16.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
Data structures Abstract data types Java classes for Data structures and ADTs.
Priority Queues Dr. David Matuszek
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
2 Obaid Ullah HOD Computer Science Dept. Superior University Sialkot Campus.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Using the Java Collection Libraries COMP 103 # T2
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Efficiency of in Binary Trees
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.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
JAVA Collections Framework Set Interfaces & Implementations
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
CSC 143 Queues [Chapter 7].
Introduction to Data Structure
Data Structures II AP Computer Science
structures and their relationships." - Linus Torvalds
Abstract Data Types (ADTs)
Presentation transcript:

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 a representation of data and the operations allowed on that data.

Why Abstract? Specify the operations of the data structure and leave implementation details to later Specify the operations of the data structure and leave implementation details to later in Java use an interface to specify operations in Java use an interface to specify operations many, many different ADTs many, many different ADTs picking the right one for the job is an important step in design picking the right one for the job is an important step in design "Get your data structures correct first, and the rest of the program will write itself." -Davids Johnson "Get your data structures correct first, and the rest of the program will write itself." -Davids Johnson High level languages often provide built in ADTs, High level languages often provide built in ADTs, the C++ STL, the Java standard library the C++ STL, the Java standard library

The Core Operations Every Collection ADT should provide a way to: Every Collection ADT should provide a way to: add an item add an item remove an item remove an item find, retrieve, or access an item find, retrieve, or access an item Many, many more possibilities Many, many more possibilities is the collection empty is the collection empty make the collection empty make the collection empty give me a sub set of the collection give me a sub set of the collection and on and on and on… and on and on and on… Many different ways to implement these items each with associated costs and benefits Many different ways to implement these items each with associated costs and benefits

Implementing ADTs when implementing an ADT the operations and behaviors are already specified when implementing an ADT the operations and behaviors are already specified think Java interface think Java interface Implementer’s first choice is what to use as the internal storage container for the concrete data type Implementer’s first choice is what to use as the internal storage container for the concrete data type the internal storage container is used to hold the items in the collection the internal storage container is used to hold the items in the collection often an implementation of an ADT often an implementation of an ADT initially slim pickings for choice of storage containers: arrays anyone? initially slim pickings for choice of storage containers: arrays anyone?

The Grand Tour Why study ADTs? Why study ADTs? Why reimplement some of them? Why reimplement some of them? How many of you will actually go out and create your own linked list ADT from scratch? How many of you will actually go out and create your own linked list ADT from scratch? Remember, the primary goal is to learn how to learn how to use and create ADTs Remember, the primary goal is to learn how to learn how to use and create ADTs also learn the behavior of some of the more conventional ADTs also learn the behavior of some of the more conventional ADTs

Bags and Sets Simplest ADT is a Bag Simplest ADT is a Bag items can be added, removed, accessed items can be added, removed, accessed no implied order to the items no implied order to the items duplicates allowed duplicates allowed Set Set same as a bag, except duplicate elements not allowed same as a bag, except duplicate elements not allowed union, intersection, difference, subset union, intersection, difference, subset

Lists Items have a position in this Collection Items have a position in this Collection Random access or not? Random access or not? Array Lists Array Lists internal storage container is native array internal storage container is native array Linked Lists Linked Lists public class Node {private Object data; private Node next; } first last

Collection with access only to the last element inserted Collection with access only to the last element inserted Last in first out Last in first out insert/push insert/push remove/pop remove/pop top top make empty make empty Stacks Top Data4 Data3 Data2 Data1

Queues Collection with access only to the item that has been present the longest Collection with access only to the item that has been present the longest Last in last out or first in first out Last in last out or first in first out enqueue, dequeue, front enqueue, dequeue, front priority queues and deque priority queues and deque Data4Data3Data2Data1 Front Back

Stacks and Queues in the Java Collection API No queue in the Java collections ADT No queue in the Java collections ADT Stack extends Vector (which is almost exactly like ArrayList) Stack extends Vector (which is almost exactly like ArrayList) Hmmm? Hmmm? One reason the Java Collections Library is often said to be broken One reason the Java Collections Library is often said to be broken no Queue in Collection API no Queue in Collection API

Trees Similar to a linked list Similar to a linked list public class TreeNode {private Object data; private TreeNode left; private TreeNode right; } Root

Other Types of Trees Binary Search Trees Binary Search Trees sorted values sorted values Heaps Heaps sorted via a different algorithm sorted via a different algorithm AVL and Red-Black Trees AVL and Red-Black Trees binary search trees that stay balanced binary search trees that stay balanced Splay Trees Splay Trees B Trees B Trees

HashTables Take a key, apply function Take a key, apply function f(key) = hash value f(key) = hash value store data or object based on hash value store data or object based on hash value Sorting O(N), access O(1) if a perfect hash function and enough memory for table Sorting O(N), access O(1) if a perfect hash function and enough memory for table how deal with collisions? how deal with collisions?

Other ADTs Maps Maps a.k.a. Dictionary a.k.a. Dictionary Collection of items with a key and associated values Collection of items with a key and associated values similar to hash tables, and hash tables often used to implement Maps similar to hash tables, and hash tables often used to implement Maps Graphs Graphs Nodes with unlimited connections between other nodes Nodes with unlimited connections between other nodes Sparse vectors and sparse matrices Sparse vectors and sparse matrices

The Java Collection Interface boolean isEmpty() int size() boolean add(Object x) boolean contains(Object x) boolean remove(Object x) void clear() Object[] toArray() Iterator iterator()

Interface?????

Generic Containers ADTs or Collection classes should be generic ADTs or Collection classes should be generic only write them once, hold lots or all types of data only write them once, hold lots or all types of data Java achieves genericity through inheritance and polymorphism Java achieves genericity through inheritance and polymorphism ADTs have an internal storage container ADTs have an internal storage container What is storing the stuff, What is storing the stuff, implementation vs. abstraction implementation vs. abstraction in Java, usually holds Objects. Why? in Java, usually holds Objects. Why?

Example - ArrayList