ITEC2620 Introduction to Data Structures

Similar documents
ITEC2620 Introduction to Data Structures

Associate Professor Dr. Raed Ibraheem Hamed

Data Structures G5029

ITI Introduction to Computing II

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

ITI Introduction to Computing II

ITEC2620 Introduction to Data Structures

We have the pointers reference the next node in an inorder traversal; called threads

CMSC 341 Lecture 10 Binary Search Trees

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Data Structures in Java

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

ITEC2620 Introduction to Data Structures

16. Dynamic Data Structures

Fun facts about recursion

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

void insert( Type const & ) void push_front( Type const & )

ITEC2620 Introduction to Data Structures

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

LIFO : Last In First Out

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Solution for Data Structure

Name CPTR246 Spring '17 (100 total points) Exam 3

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Stacks Stack Examples:

Lecture 3: Stacks & Queues

Data Structure Advanced

Lecture No.04. Data Structures

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CSCE 2014 Final Exam Spring Version A

CMSC 341. Binary Search Trees CMSC 341 BST

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Section 05: Midterm Review

Basic Data Structures

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS24 Week 8 Lecture 1


COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Chapter 9 STACK, QUEUE

Basic Data Structures 1 / 24

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

1 P age DS & OOPS / UNIT II

Generic BST Interface

CS350: Data Structures Stacks

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Lecture Data Structure Stack

Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

1. Stack Implementation Using 1D Array

+ Abstract Data Types

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Data Structures and Algorithms

ECE Spring 2018 Problem Set #0 Due: 1/30/18

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

CS24 Week 4 Lecture 2

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CSE 143 SAMPLE MIDTERM

Data Abstraction and Specification of ADTs

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Data Structures and Algorithms

CS 241 Data Organization Binary Trees

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Tutorial 4: Binary Trees

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Week 6. Data structures

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

ITEC1620 Object-Based Programming. Lecture 13

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Largest Online Community of VU Students

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Stacks and Queues. David Greenstein Monta Vista

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Need Backtracking! L 0 A 1 B 2 K 3 P 4 C 5 I 6 Z 7 S 8 A 9 N 10 X 11 A 12 N 13 X 14 P 15

Section 05: Solutions

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

is.centraldispatch Documentation

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Data Structure (CS301)

Stacks and Queues. Fundamentals of Computer Science.

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Lists are great, but. Stacks 2

Content: Learning Objectives

Apply to be a Meiklejohn! tinyurl.com/meikapply

Transcription:

9//07 ITEC60 Introduction to Data Structures Lecture 7a ADTs and Stacks Abstract Data Types A way to specify the functionality of an entity without worrying about its implementation Similar to a JAVA interface or an API List ADT I interface SortedList public void insert (Object item); public Object remove (); public boolean isinlist (); List ADT II How can we implement a SortedList? Arrays Linked-list Binary search tree

9//07 List ADT III Does the implementation affect the available functions? No Does the implementation affect the time to execute each function? Yes e.g. insert O(n), O(n), O(logn) Stacks I A stack is a Last-In, First-Out = LIFO buffer e.g. dinner plates at the buffet line Plates are added to the top of the stack Plates are removed from the top of the stack Stacks II Only the top element of a stack is accessible When elements are added, they are pushed onto the top When elements are removed, they are popped off of the top push() and pop() are the two defining functions of a stack Example of Stacks I Stacks are usually drawn as vertical arrays 3

9//07 Example of Stacks II Initial pop() pop() push() pop() 3 3 3 3 3 Implementation of Stacks Stacks are an ADT Functionality of stack has been specified Implementation of stack has not A stack can be implemented with any data structure that can provide access to the top element Expect efficiency (i.e. O()) Array-Based Implementation I Move a top pointer (to the first empty spot) up and down public class Stack private int size; private int top; private Object[] stackarray; Array-Based Implementation II public boolean push (Object item) // stack overflow error stack out of memory if (top = stackarray.length) return false; stackarray[top] = item; top++; return true; 3

9//07 Array-Based Implementation III Stack-based Recursion public Object pop () // stack underflow error nothing in the stack if (top == 0) return null; top--; return stackarray[top]; Computers use a control stack to manage function calls/function returns When a function is called, data is pushed onto the stack When a function finishes, data is popped off of the stack Instead of the computer s stack, we can use our own to implement recursion Stack-based Tree Traversal I Stack-based Tree Traversal II Need a keep information about where we are in the tree public class BinaryNode public BinaryNode left; public BinaryNode right; public int key; public boolean expanded;

9//07 Stack-based Tree Traversal III public void printtree (BinaryNode root) Stack treestack = new Stack(); BinaryNode current = root; // first time (expanded == false), do left, this, right // second time (expanded == true), just print root.expanded = false; Stack-based Tree Traversal IV while (current!= null) // when the stack is empty, // you ve finished the tree traversal // second time (expanded == true), // just print if (current.expanded) System.out.println(current.key); Stack-based Tree Traversal V else // stack is last in, first out buffer // want to process left first (in order), // so it goes in last, right goes first if (current.right!= null) current.right.expanded = false; treestack.push(current.right) Stack-based Tree Traversal VI // we are in the process of expanding current // this is the first time, // next time will be the second time current.expanded = true; treestack.push(current);

9//07 Stack-based Tree Traversal VII Stack Trace I if (current.left!= null) current.left.expanded = false; treestack.push(current.left) // finished expansion, get next item from stack current = (BinaryNode) treestack.pop(); Stack Trace II printtree(); Stack is initially empty root/current is not expanded Expand and populate stack Stack Trace III Expand root 3F T 6

9//07 Stack Trace IV Stack Trace V pop() 3F pop() F T F 3T F 3T F F T T Stack Trace VI Stack Trace VII pop() T Print T pop() F F 3T T 3T F F T T 7

9//07 Stack Trace VIII pop() T Print Stack Trace IX pop() 3T Print 3 3T F F T T Stack Trace X pop() F Stack Trace XI pop() T Print T T T 8

9//07 Stack Trace XII pop() T Print Stack Trace XIII First time a node is red Expand it Second time a node is red Print it Stacks and Recursion Any recursive algorithm can be implemented non-recursively Do you have to use a function that calls itself? No, use a stack to implement recursion Do you have to use a recursive algorithm? Yes, how do you traverse a binary tree without a recursive algorithm? Stacks in Use I Stacks are used to manage the control flow in a computer When a method is called, the calling method is put on the stack When a method returns, control is passed to the top method on the stack The last method to call another method is the first to get control back LIFO 9

9//07 Stacks in Use II What do you get when your JAVA program crashes? The control stack main() is at the bottom Internal JAVA methods are at the top The last method that the computer executed (that you wrote) is somewhere in between find it! Readings and Assignments Suggested Readings from Shaffer (third edition).,.,. 0