CS2468: Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone: 3442 9820 TA (Assignment.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Chapter 3 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
DATA STRUCTURE Subject Code -14B11CI211.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Introduction. 2COMPSCI Computer Science Fundamentals.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Objects and Classes Mostafa Abdallah
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Stacks (and Queues).
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
GC211Data Structure Lecture2 Sara Alhajjam.
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
CS2468: Data Structures and Data Management
Stacks.
Copyright © Aiman Hanna All rights reserved
Introduction to Data Structure
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

CS2468: Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone: TA (Assignment Marking) – Chao SHEN Phone: – Dan WANG Phone: Welcome to ask questions at ANY time. The course Website: Some Java Source code: The course Website: Text Book: Michael T. Goodrich and Roberto Tamassia, Data Structure and Algorithms in Java, John Wiley & Sons, Inc. 5 th Edition Stacks1

Topics to be covered Analysis of Algorithms – worst case time and space complexity Data Structures – stack, queue, linked list, tree, priority queue, heap, hash, and graph, Searching algorithms – binary and AVL search trees; Sorting algorithms – merge sort, quick sort, bucket sort and radix sort; (Reduce some contents) Graph – data structure, depth first search and breadth first search. (add some interesting contents). shortest path. Stacks2

Why This Course? You will be able to evaluate the quality of a program (Analysis of Algorithms: Running time and memory space ) You will be able to write fast programs You will be able to solve new problems You will be able to give non-trivial methods to solve problems. ( Your algorithm (program) will be faster than others.) Stacks3

Course Evaluations Course work: 30% Final Exam: 70% Course Work: – Three assignments, 15% – One quiz 3% – One mid term exam 9% – Lab quiz 3% Stacks4

5 OBTL: Course Intended Learning Outcomes 1.Describe the functionality of a data structure as an abstract data type; 2.Implement an abstract data type in a programming language; 3.Implement and test data structures for common structures; 4.Select an appropriate data structure from a given set of structures to solve a given problem; 5.Design and implement data storage management with simple file structures. Will be tested in quiz or assignment or midterm. For each item, you have to get 40% to pass.

Pre-requisites: CS2360 Java Programming /or CS2362 Computer Programming for Engineers & Scientists /or CS2363 Computer Programming /or CS2372 Fundamentals of Programming /or equivalent CS2360 CS2362 CS2363 CS2372 Not known java? – Spend the rest of 8-10 days to study Java. – Read textbook p1-p53 – Design a class with two integers C1 and C2 and a method f() to calculate the average value of c1 and c2. – If you cannot do that, you should worry… Stacks6

Data Structures – A systematic way of organizing and accessing data. -- No single data structure works well for ALL purposes. – Data stored – operation on data Algorithms – an effective method expressed as a finite list of well-defined instructions for calculating a function. – Algorithms are used for calculation, data processing, and automated reasoning. – In simple words an algorithm is a step-by-step procedure for calculations. Stacks7

How to describe algorithm? Nature languages – Chinese, English, etc. – Not accurate. Pseudo-code – Codes close to computer languages Omits details that are not essential for human understanding – Intended for human reading rather than machine reading Programs – C /C++ programs, Java programs. Pseudo-code is preferred – Allow a well-trained programmer to be able to implement. – Allow an expert to be able to analyze the running time. Stacks8

An Example of an Algorithm Algorithm sorting(X, n) Input array X of n integers Output array X sorted in a non- decreasing order for i  0 to n  1 do for j  i+1 to n-1 do if (X[i]>X[j]) then { s=X[i]; X[i]=X[j]; X[j]=s; } return X // after i-th round, the i-th smallest number is at i-th position. Stacks9 Algorithm sorting(X, n) Input array X of n integers Output array X sorted in a non- decreasing order for i from 0 to n  1 do for j from i+1 to n-1 do if X[i] is larger than X[j] swap(X[i], X[j]) return X // after i-th round, the i-th smallest number is at i-th position.

Variables, Primitives A variable has its value – A type is associated, E.g., int, boolean, float, long, etc. – A value is assigned to the variable The value is stored in the memory The size of the value is determined umbigously; 64 bit machine, int 8 bytes, double 8 bytes, etc – Examples int x; int y=50; char c, z=‘A’; Stacks10

11 Objects ( extension of variables ) An object has both a state and behavior. State defines the object, Behavior defines what the object does. What is the size of an object in the memory?

12 Class Class defines the type of an object the kinds of operations that it performs. A Java class uses variables to define data fields and methods to define behaviors. A class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

13 Classes

14 Class Diagram

15 © 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.3: Class specification of Person State/attributes—variables Operation/action – methods

16 © 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.7: Providing methods to access the attributes of Person

17 © 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.8: An Instance fred of the Person class

18 © 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.9: Using the access methods to set fred’s attributes

19 Constructors Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors are a special kind of methods that are invoked to construct objects.

20 Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor.  Constructors must have the same name as the class itself.  Constructors do not have a return type—not even void.  Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

21 Creating Objects Using Constructors new ClassName(); Example: new Circle(); new Circle(5.0);

22 Declaring/Creating Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Create an object Assign object reference

23 Accessing Objects Referencing the object’s data: objectRefVar.data e.g., myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) e.g., myCircle.getArea() Show a complete program C7-TestCircle1.html

Part-B1 Stacks (p198-p213)

Abstract Data Types (ADTs) Abstract data type – an abstraction of a data structure An ADT specifies: – Data stored – Operations on the data – Error conditions associated with operations e.g. : ADT modeling a students record – The data stored are Student name, id, as1, as2,as3, exam – The operations supported are int averageAs(as1,as2,as3) Int finalMark(as1, as2,as3, exam) ) – Error conditions: Calculate the final mark for absent student Stacks25

The Stack ADT (§5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out (LIFO) scheme Main stack operations: – push(object) inserts an element – object pop() removes and returns the last inserted element Auxiliary stack operations: – object top() returns the last inserted element without removing it – integer size() returns the number of elements stored – boolean isEmpty() indicates whether no elements are stored Stacks26

Applications of Stacks Direct applications – Undo sequence in a text editor – Chain of method calls in the Java Virtual Machine Indirect applications – Auxiliary data structure for algorithms – Component of other data structures Stacks27

Parentheses Matching An expression, i.e.,[(2+3)*x+5]*2. Each “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” – correct: ( )(( )){([( )])} ((( )(( )){([( )])})) – incorrect )(( )){([( )])}( ({[ ])} ([)]) ( Stacks28

Examples Stacks29 Picture is from

Examples ( ) ( ( ) ) { ( [ ( ) ] ) } (( ( (({ ( { [ ( { ( [ ( { [ ( { ( {{

Examples ( ) ( ( ] ) { ( [ ( ) ] ) } (( ( (( Mismatch!!!

Examples ( ) ( ( ) ) { ( [ ) ] ) } (( ( (({ ( { [ ( { ( { Mismatch!!!

Examples ( ) ( ( ) ) { ( [ (( ( (({ ( { [ ( { Mismatch!!! (stack is non-empty)

Examples ( ) ( ( ) ) ) (( ( (( Mismatch!!! (pop empty stack)

Parentheses Matching Algorithm Algorithm ParenMatch( X, n ) : Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i = 0 to n - 1 do if X [ i ] is an opening grouping symbol then S. push (X [ i ] ) else if X [ i ] is a closing grouping symbol then if S. isEmpty () then return false { nothing to match with } if S. pop () does not match the type of X [ i ] then return false { wrong type } if S. isEmpty () then return true { every symbol matched } else return false { some symbols were never matched } Stacks35

Parentheses Matching Example 1 Input: () (() [()]) i X[i] OperationStackOutput 0(Push (( 1)Pop ( Test if ( and X[i] match? YES 2(Push (( 3( (( 4)Pop ( Test if ( and X[i] match? YES ( 5[Push [([ Stacks36

Parentheses Matching Example 1 Input: () (() [()]) i X[i] OperationStackOutput 6(Push (([( 7)Pop ( Test if ( and X[i] match? YES ([ 8]Pop [ Test if [ and X[i] match? YES ( 9)Pop ( Test if ( and X[i] match? YES Test if stack is Empty? YES TRUE Stacks37

Parentheses Matching Example 2 Input: ( () [] ]() i X[i] OperationStackOutput 0(Push (( 1( (( 2)Pop ( Test if ( and X[i] match? YES ( 3[Push [([ 4]Pop [ Test if [ and X[i] match? YES ( 5]Pop ( Test if ( and X[i] match ? NOFASLE Stacks38

Stack Interface in Java Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Stacks39 public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException; public void push(Object o); public Object pop() throws EmptyStackException; }

Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException Stacks40

Array-based Stack (Implementation) A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable t keeps track of the index of the top element Stacks41 S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t  t  1 return S[t + 1]

Array-based Stack (cont.) The array storing the stack elements may become full A push operation will then throw a FullStackException – Limitation of the array- based implementation – Not intrinsic to the Stack ADT Stacks42 S 012 t … Algorithm push(o) if t = S.length  1 then throw FullStackException else t  t + 1 S[t]  o

Array-based Stack (Cont.) A Stack might be empty top returns the element at the top of the Stack, but does not remove the top element. When the Stack is empty, an error occurs. Stacks43 S 012 t … Algorithm isEmpty() if t<0 then return true else return false Algorithm top() if isEmpty() then throw EmptyStackException return S[t ]