Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.

Slides:



Advertisements
Similar presentations
Chapter 9: Data Structures I
Advertisements

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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
Chapter 12: Data Structures
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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).
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
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.
Week 15 – Monday.  What did we talk about last time?  Tries.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Java Software Solutions Foundations of Program Design Seventh Edition
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
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.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
Chapter 15 Lists Objectives
Chapter 12: Data Structures
Stacks and Queues.
Week 15 – Monday CS221.
Chapter 17 Object-Oriented Data Structures
Chapter 12 Collections.
Chapter 5 Queues.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Chapter 13 Collections.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Chapter 12 Collections.
Lecture 2: Stacks and Queues
Generic Programming.
Important Problem Types and Fundamental Data Structures
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Presentation transcript:

Collections (Stack, queue, tree, graph)

References as Links References can be used to create a variety of linked structures, such as a linked list.

Inserting a Node A node can be inserted into a linked list with a few reference changes:

Deleting a Node Likewise, a node can be removed from a linked list by changing the next pointer of the preceding node: current.next= current.next.next;

Other Dynamic Representations It may be convenient to implement a list as a doubly linked list, with next and previous references:

Other Dynamic Representations Another approach is to use a separate header node, with a count and references to both the front and rear of the list:

Outline Collections and Data Structures Dynamic Representations Linear Structures (Queues & Stacks) Non-Linear Structures (Trees & Graphs) The Java Collections API

Classic Data Structures some common data structures that are helpful in many situations Classic linear data structures include queues and stacks Classic nonlinear data structures include trees and graphs

Queues A queue is an abstract data type to add or remove a list of objects. A queue has a capacity and a discipline. For example a FIFO discipline: First-In, First-Out Analogy: a line of people at a bank teller’s window

Queues Classic operations for a queue –enqueue - add an item to the rear of the queue –dequeue (or serve) - remove an item from the front of the queue –empty - returns true if the queue is empty Queues often are helpful in simulations or any situation in which items get “backed up” while awaiting processing

Queuing systems 11

Queues A queue can be represented by a singly-linked list; it is most efficient if the references point from the front toward the end of the queue. A queue can be represented by an array, using the remainder operator (%) to “wrap around” when the end of the array is reached and space is available at the front of the array

Stacks A stack ADT is also linear, like a list or a queue Items are added and removed from only one end of a stack The discipline is LIFO: Last-In, First-Out Analogies: a stack of plates or a stack of books

Stacks Stacks often are drawn vertically:

Stacks Classic stack operations: –push - add an item to the top of the stack –pop - remove an item from the top of the stack –peek (or top) - retrieves the top item without removing it –empty - returns true if the stack is empty A stack can be represented by a singly-linked list, with the first node in the list being the top element on the stack A stack can also be represented by an array, with the bottom of the stack at index 0

Stacks The java.util package contains a Stack class The Stack operations operate on Object references Suppose a message has been encoded by reversing the letters of each word See Decode.java

//******************************************************************** // Decode.java Author: Lewis/Loftus // // Demonstrates the use of the Stack class. //******************************************************************** import java.util.*; public class Decode { // // Decodes a message by reversing each word in a string. // public static void main (String[] args) { Scanner scan = new Scanner (System.in); Stack word = new Stack(); String message; int index = 0; System.out.println ("Enter the coded message:"); message = scan.nextLine(); System.out.println ("The decoded message is:"); continue

while (index < message.length()) { // Push word onto stack while (index < message.length() && message.charAt(index) != ' ') { word.push (new Character(message.charAt(index))); index++; } // Print word in reverse while (!word.empty()) System.out.print (((Character)word.pop()).charValue()); System.out.print (" "); index++; } System.out.println(); }

continue while (index < message.length()) { // Push word onto stack while (index < message.length() && message.charAt(index) != ' ') { word.push (new Character(message.charAt(index))); index++; } // Print word in reverse while (!word.empty()) System.out.print (((Character)word.pop()).charValue()); System.out.print (" "); index++; } System.out.println(); } Sample Run Enter the coded message: artxE eseehc esaelp The decoded message is: Extra cheese please

Outline Collections and Data Structures Dynamic Representations Linear Structures (Queues & Stacks) Non-Linear Structures (Trees & Graphs) The Java Collections API

Trees A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy Nodes that have no children are called leaf nodes Nodes except for the root and leaf nodes are called internal nodes In a general tree, each node can have many child nodes

A General Tree internal node

Binary Trees In a binary tree, each node can have no more than two child nodes Trees are typically represented using references as dynamic links For binary trees, this requires storing only two links per node to the left and right child

Binary tree example

TreeNode example code private class TreeNode { Object item; TreeNode left; TreeNode right; TreeNode parent; TreeNode(Object element, TreeNode lptr, TreeNode rptr, TreeNode p) { item = element; left = lptr; right = rptr; parent = p; }

Graph A graph is another non-linear structure Unlike a tree, a graph does not have a root Any node in a graph can be connected to any other node by an edge Analogy: the highway system connecting cities on a map

Directed Graph Undirected Graph

A graph for webpage links

Traveling salesman The travelling salesman starts from a city in the graph. He tries to find the shortest path that takes him to each of the cities, without ever visiting the same city twice.

Digraphs In a directed graph or digraph, each edge has a specific direction. Edges with direction sometimes are called arcs Analogy: airline flights between airports

Digraphs

Representing Graphs Both graphs and digraphs can be represented using dynamic links or using arrays. As always, the representation should facilitate the intended operations and make them convenient to implement

Outline Collections and Data Structures Dynamic Representations Linear Structures (Queues & Stacks) Non-Linear Structures (Trees & Graphs) The Java Collections API

Collection Classes The Java standard library contains several classes that represent collections, often referred to as the Java Collections API Their underlying implementation is implied in the class names such as ArrayList and LinkedList Several interfaces are used to define operations on the collections, such as List, Set, SortedSet, Map, and SortedMap

Generics Java supports generic types, which are useful when defining collections A generic type is a generic class or interface that is parameterized over types A class can be defined to operate on a generic data type which is specified when the class is instantiated: LinkedList myList = new LinkedList (); By specifying the type stored in a collection, only objects of that type can be added to it Furthermore, when an object is removed, its type is already established

Generic method example public class Generic1 { // generic method printArray public static void printArray( E[] inputArray ) { for ( E element : inputArray ) { System.out.printf( "%s ", element ); } System.out.println(); } public static void main( String args[] ) { Integer[] intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println("Array integerArray contains:"); printArray(intArray); // pass an Integer array System.out.println("\nArray doubleArray contains:"); printArray(doubleArray); // pass a Double array System.out.println("\nArray characterArray contains:"); printArray(charArray); // pass a Character array }

Generic class example public class Generic2 { private ElementType item; public void add(ElementType t) { item = t; } public ElementType get() { return item; } public static void main(String[] args) { Generic2 integerBox = new Generic2 (); Generic2 stringBox = new Generic2 (); integerBox.add(new Integer(10)); stringBox.add(new String("Hello World")); System.out.println("Integer value: " + integerBox.get()); System.out.println("String value : " + stringBox.get()); }

Summary –the concept of a collection –separating the interface from the implementation –dynamic data structures –linked lists –queues and stacks –trees and graphs –generics